@trenskow/merge

0.1.20 • Public • Published

@trenskow/merge

A small library for doing recursive Object.assign.

Usage

Below is an example of how to use the package.

import merge from '@trenskow/merge';

const myFirstValue = { first: 1, shared: { value: 1 } };
const mySecondValue = { second: 2, shared: { value: 2 } };
const myThirdValue = { third: 3, shared: { value: 3 } };

const myValue = merge(myFirstValue, mySecondValue, myThirdValue);

/*
	myValue is {
		first: 1,
		second: 2,
		third: 3,
		shared: {
			value: 3
		}
	}
*/

How it works

Input values are compared in pairs, so if more than two values are provided, the first two are compared – then the result is compared to the third – and that result is compared to the fourth, etc...

Rules

The rules for merging the input values is as below.

  1. If one of the values is not an object?
    • The second value survives.
  2. If both values are an array?
    • An new array is created with the items from the first array and then the values of the second array.
      • The new array survives.
  3. Both values are now regarded as objects.
    • An new object with the following keys are created.
      • The properties unique of the first object.
      • The properties which is set on both objects are merged.
      • The keys unique of the second object.
      • The new object survives.

License

See license in LICENSE.

Readme

Keywords

Package Sidebar

Install

npm i @trenskow/merge

Weekly Downloads

3

Version

0.1.20

License

BSD-2-Clause

Unpacked Size

10 kB

Total Files

7

Last publish

Collaborators

  • trenskow