Giter Club home page Giter Club logo

assign-deep's Introduction

assign-deep Donate NPM version NPM monthly downloads NPM total downloads Linux Build Status

Deeply assign the values of all enumerable-own-properties and symbols from one or more source objects to a target object. Returns the target object.

Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your ❤️ and support.

Install

Install with npm:

$ npm install --save assign-deep

Heads up!

Please update to version 1.0.1 or later, a critical bug was fixed in that version.

Behavior

  • This follows the same behavior as Object.assign(), and thus does not deep clone values.
  • The first argument is the "target" object.
  • To shallow clone, pass an empty object as the first argument.
  • One or more additional ("source") objects may be passed.
  • When multiple objects are passed, properties in later objects will overwrite same-named properties in earlier objects. Thus, properties in the target object will be overwritten by same-named properties in other objects.
  • Only enumerable and own properties are copied.
  • String and Symbol properties are copied.
  • Sparse arguments are skipped, so this does not throw on null or undefined source values.
  • Like Object.assign(), [[Get]] is used on source objects and [[Set]] is used on the target, so it will invoke getters and setters. Therefore it assigns properties versus just copying or defining new properties. Note that this should not be used for merging new properties into a prototype if the merge sources contain getters and you do not want [[Get]] to be used on the getters. For copying property definitions and their enumerability into prototypes Object.getOwnPropertyDescriptor() and Object.defineProperty() should be used instead.

Usage

const assign = require('assign-deep');

const config = {
  admin: true,
  author: {
    name: { first: 'Joe' }
  }
};

const locals = {
  admin: false,
  author: {
    name: { last: 'Smith' },
    username: 'joesmith'
  }
};

console.log(assign(config, locals));
// {
//   admin: false,
//   author: {
//     name: { first: 'Joe', last: 'Smith' },
//     username: 'joesmith'
//   }
// }

About

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Running Tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test
Building docs

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

Related projects

You might also be interested in these projects:

Contributors

Commits Contributor
31 jonschlinkert
14 doowb

Author

Jon Schlinkert

License

Copyright © 2019, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.8.0, on June 19, 2019.

assign-deep's People

Contributors

doowb avatar jonschlinkert avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

assign-deep's Issues

Breaks on arrays

assign({ a: [1] }, { a: [2] }); // => { a: [2] }

I was expecting a to be the combination of both arrays, but instead it is the value of the latter object.

crashes on this object

const deepAssign = require('assign-deep')

const a = {
  b: {}
}

a.b = a

console.log(deepAssign({}, a))

Shouldn't assign-deep mutate dates?

Hello! Thanks for the useful library.

Recently I've noticed that it does mutate Dates:

var assignDeep = require("assign-deep");

const a = {
    date: new Date(),
    anyObj: {}
};

const b = assignDeep({}, a);

console.log(
    a.anyObj === b.anyObj, // Gives false as expected.
    a.date === b.date // Gives true. Should it be false?
); 

My idea is that date objects must not be mutated as well as any other objects. Not to be unfounded, a.date.setTime(X) will change both a.date and b.date in the example above. What do you think? I can process with the pull request if you agree.

Thank you!

Changelog

A changelog would be useful either as a file in the repo or as notes in the releases section of github.

For example I can't tell what has changed between 0.4.7 and 1.0.0 and if I should upgrade. What has changed in the 1.0.0 release, any breaking changes or differences in how it works?

Multiple arguments overwrites values in arguments

Using Object.assign with an empty target, only the target is changed, not the inputs/arguments. Usign assign-deep, the inputs also get overwritten. Is this expected behaviour? I did not expect this.

var assignDeep = require("assign-deep")

var o1 = { a: 1, b: 1, c: {a: 1} };
var o2 = { b: 2, c: {a:2} };
var o3 = { c: {a:3} };

console.log(assignDeep({}, o1, o2, o3));
// Expected output: Object {a: 1, b: 2, c: Object {a: 3}}
console.log('o1', o1);
// Expected output: Object {a: 1, b: 1, c: Object {a: 1}}
// Actual output: Object {a: 1, b: 1, c: Object {a: 3}}

Is there a way to stop it

Practical application is a local configuration loading the default configuration if it is doesn't exist (if it is deeper). In the local configuration, I was to stop it from loading the deeper default configuration. Is this possible?

For example:
var local = a: { b: false };
var default = a: { b: { c: { d, e}}}, x: { y, z};
console.log(assign(local, default));

desire result is a {b: false}, x: {y, z}

Functions aren't getting overridden?

I have some function definitions specified in objects and would like them to get overridden when objects are being combined using assign-deep, but it seems to work for all object keys except those referring to function definitions.

a = {
  string: 'a'
  func: a => a,
}
b = {
  string: 'b'
  func: b => b,
}
c = assignDeep({}, a, b)

console.log(c.string) // prints 'b' as expected
console.log(c.func.toString()) // prints 'a => a', not as expected

cann't assign to an empty object

for example:

var assign = require('assign-deep');

var res1 = assign({}, {b: {c: 1}});

var res2 = Object.assign({}, {b: {c: 1}});

console.log(res1); //{}
console.log(res2); //{b:{c:1}}

is this a bug?

Webpack compilation is not compatible

When using webpack to package the files, libraries, it converts every require to a function.
So,

var lazy = require('lazy-cache')(require);
lazy('is-extendable', 'isObject');

is converted to

var lazy = __webpack_require__(385)(__webpack_require__(386));

where it says no arguments passed to the last require and hence breaks with an error

"lazy-cache Cannot find module 'is-extandable' ./index.js"

v1.0.0 breaks create-react-app build

I'm not sure if this is the fault of assign-deep or create-react-app, but upgrading assign-deep from 0.4.7 to 1.0.0 broke my build. I didn't upgrade for any reason other than there being a new version available. I have reverted back.

Reproduction (on Windows):

  • Have create-react-app available (I'm using v1.1.4).
  • Run commands:
    create-react-app assign-deep-test
    cd assign-deep-test
    yarn add [email protected]
    
  • Edit assign-deep-test\src\App.js to have import assign from 'assign-deep';.
  • Run command:
    yarn build
    

And this error appears:

$ react-scripts build
Creating an optimized production build...
Failed to compile.

Failed to minify the code from this file:

        ./node_modules/assign-deep/index.js:12

Read more here: http://bit.ly/2tRViJ9

error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

In my actual project (uses code splitting), I get a different error:

Failed to compile.

Failed to minify the bundle. Error: static/js/main.2adb6dc3.js from UglifyJs
Unexpected token: punc (.) [static/js/main.2adb6dc3.js:139360,41]

Type definitions

I've just decided to use this cool lib. I found some similar libs and I think this one is better but there's one small point - type definitions.

Can you add jsDoc comment to the function, or add this lib to https://definitelytyped.org/?
If you do that, your lib will be the best.

Throw error when string or number passed accidentally?

Would it make sense for assign-deep to throw an error when a string or number is passed?

const element = 'string'
console.log(assignDeep({a: 1}, element, {b: 2, a: 999}))

This prints { a: 999, b: 2 }, as expected. But for someone who passed in the string, it's almost certainly an error in their code because they wouldn't be passing a string or number in the first place. It would be helpful to get an error right away.

Difference between this and mixin-deep?

I've tested a few use cases against assign-deep and another project of yours mixin-deep, and they both seem to do what I want.

  • Recursive
  • Don't merge arrays but overwrites them with the last array found for a property
  • Overwrite keys with null and undefined rather than ignoring them
  • Don't make any changes to object1 and object2 when an empty object {} passed as first argument (e.g. assign({}, object1, object2) and mixin({}, object1, object2))

How are these 2 projects different, can you give some use cases with inputs that produce different outputs? Just want to make sure I'm using the right package for when I have to deal with more complex object structures where things may behave differently.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.