1.0.3 • Published 4 years ago

@walls/merge v1.0.3

Weekly downloads
2
License
ISC
Repository
-
Last release
4 years ago

Assign or Clone Objects

This module is used to either merge or clone any object in javascript.

Assign

Use this to merge 2 objects and not clone the first object. All properties from 2nd object will be copied to the first object.

merge.assign(target,  source,  {
    item:  (target,  source)  =>  {
	    for (const  item  of  source) {
		    let  exists  =  false;
		    for (const  check  of  target) {
			    if (check.a  ===  item.a) {
				    merge.assign(check,  item);
				    exists  =  true;
			    }
			   }
		    if (!exists) {
			    target.push(item);
		    }
	    }
	    return  target;
    }
});

Clone

Clone any object into a new object. Optionally merge 2 objects while cloning them. The options for the clone are used when merging the 2 objects. Not when cloning.

merge.clone(target,  source,  {});

Options

The options object should be structured the same as the source element and give a function that is used to merge the 2 objects. Return the new value from the callback.

type Callback = (target: object, source: object, options: Options) => object;

Written with StackEdit.