0.0.5-c • Published 5 years ago

extend-assign v0.0.5-c

Weekly downloads
4
License
MIT
Repository
github
Last release
5 years ago

extend-assign

Build Status NPM version Downloads Coverage Status

Extend Object.assign(),it can support filter, deep copy, protect property.

Install

npm i extend-assign --save

Examples

Copy the values of all enumerable properties from source objects to a target object

const assign = require('extend-assign');
assign({c: 3}, {a: 1, b: 2});

// {a:1, b: 2, c: 3}

Deep copy

const assign = require('extend-assign');
assign({a: {c: 3}}, {a: {a: 1, b: 2}}, true);

// {a: { a: 1, b: 2, c: 3}}

Filter you specify properties from source objects

const assign = require('extend-assign')
assign({c: 3}, {a: 1, b: 2}, false, {
    filter: ['a', 'c']
});

// {b: 2, c: 3}


assign({a: {c: 3}}, {a: {a: 1, b: 2}}, true, {
    filter: ['a.a']
});
// {a: { b: 2, c: 3}}


assign({a: {c: 3}}, {a: {a: 1, b: 2}, b: 2}, true, {
    filter: ['b'],
    filterGlobal: true
});
// {a: { a: 1, c: 3}}

assign({a: {c: 3}}, {a: {a: 1, 'a.b': 2, b: 2}, b: 2}, true, {
    filter: ['a.b'],
    filterGlobal: true
});

// {a: { a: 1, b: 2, c: 3, b: 2}}

Protect you specify properties from source objects, protected properties aren't writable and enumerable

const assign = require('extend-assign')
 function Person(name) {
    assign(this, {name},true, {
        protect: ['name']
    });
 }
 let A = new Person('A');
 A.name = 'B';
 console.log(A.name); 'A'

API

assgin(target,...sources)

  • target \|| target object

  • sources \ An array of source objects or arrays.

  • deep \ When true, the copy is deep copy; Default: false

  • options \

    • filter \|\

      • \ An array of properties will be filtered.
      • \

        a custom filter function, the function has an argument(key), when the function return false, will filter the key.

    • filterGlobal \

      When true,for example, 'xxx.xx' will be regarded as a key; Default: false.

    • protect \|\

      • \ An array of properties will be protected.
      • \

        a custom filter function, the function has an argument(key), when the function return false, will protect the key.

    • protectGlobal \

      When true,for example, 'xxx.xx' will be regarded as a key; Default: false.

test

npm run test
0.0.5-c

5 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2-a

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago