0.0.109 • Published 5 years ago

@oresoftware/deep.mixin v0.0.109

Weekly downloads
19
License
SEE LICENSE IN li...
Repository
github
Last release
5 years ago

Travis build status: Build Status

CircleCI build status: CircleCI

Deep.Mixin on NPM

Why? Because Object.assign is shallow

Design

Deep.Mixin will copy sub-properties of objects, instead of only handling top-level properties, when doing a merge. This library is ideal for merging configuration objects. But it's also a good drop-in replacement for Object.assign if you want deep merging/mixins.

  • Copies functions properly
  • Deep clones everything
  • Immutability (always returns a new object, doesn't modify any arguments)
  • Handles circular refs
  • Written in TypeScript

Limitations

  • Only accepts POJSOs as arguments - for example we cannot do deepMixin({},[])

Basic Usage

import deepMixin from '@oresoftware/deep.mixin';

const v1 = {/* some object */};
const v2 = {/* some object */};
const v3 = deepMixin(v1,v2);  
const {deepMixin} = require('@oresoftware/deep.mixin');

const v1 = {
  dog: {
    bird: 2,
    foo() {
      return 'm1';
    },
    cat: {
      fark: '3',
      snake: 5
    }
  }
};

v1.dog.foo.big = 8; // add prop to function foo

const v2 = {
  dog: {
    foo() {
      return 'm2';
    },
    cat: {
      snake: 7
    }
  }
};

console.log(Object.assign({},v1,v2));  // { dog: { foo: [Function: foo], cat: { snake: 7 } } }

console.log(deepMixin(v1,v2));  // next line

{
  dog: {
    foo: [Function: foo] { big: 8 },
    cat: { snake: 7, fark: '3' },
    bird: 2
  }
}

Other / Etc

You can reverse the order of operands, using deepMixinRight()

const v4 =  deepMixin(thirdPrecedence, secondPrec, firstPrec);

// or switch the order:
const v4 = deepMixinRight(firstPrecendence, secondPrec, thirdPrec)
0.0.109

5 years ago

0.0.108

6 years ago

0.0.107

6 years ago

0.0.106

6 years ago

0.0.105

6 years ago

0.0.104

6 years ago

0.0.102

6 years ago

0.0.1001

6 years ago