Nascent Personality Manifestation

npm

Need private packages and team management tools?Check out npm Orgs. »

controlled-merge-with-attribute

1.1.0 • Public • Published

controlled-merge-with-attribute

Note: this is a fork of the controlled-merge node module, cretated as the original author is currently travelling and unable to accept pull requests. It's fully intended that this module will be deprecated once the PR has been accepted. Because of this, I have not changed any of the documentation. Any reference to controlled-merge should be replaced with controlled-merge-with-attribute.

controlled-merge

controlled-merge is a utility function to merge a number of javascript objects into a singular object. It has an optional onConflict functionality that allows you to specify how to handle conflicts of the merge.

merge([onConflict], [objects]);

  • onConflict(objectOne, objectTwo, attr)
    • Optional function to define what happens when non objects are detected and conflict. By default, it will assign the later merge object over the earlier one. The name of the conflicting attribute will be present as the third parameter.
  • objects
    • Either a singular array of all objects passed in, or manually passed in arguments.

Install

npm install controlled-merge

Example

var merge = require('controlled-merge');

//Input
var x =
  {
    'test': 'This is a test string'
  };
var y =
  {
    'also': 'is a test',
    'value': 1
  };
var z =
  {
    'value': 3
  };
var conflictingKeys = [];
  
var results = merge(
  function(obj1, obj2, attr){
    conflictingKeys.push(attr);
    return obj1 > obj2 ? obj1 : obj2;
  },
  x, y, z
);

//results =
{
  'test': 'This is a test string',
  'also': 'is a test',
  'value': 3
}

//conflictingKeys = 
[
  'value'
]

Why?

I needed to combine JSONs that represented role-based permissions for CanThey - the idea being that a user can have several roles and I needed to calculate resulting permissions.

install

npm i controlled-merge-with-attribute

Downloadsweekly downloads

13

version

1.1.0

license

MIT

last publish

collaborators

  • avatar
Report a vulnerability