Giter Site home page Giter Site logo

Comments (5)

schnittstabil avatar schnittstabil commented on July 19, 2024

deep-assign suffers from this behavior for some time – unfortunately, the problem is that, simply recursive Object.assign() does not work as hoped.

The problem is that functions etc. are objects too, and we cannot clone them. Using your example:

const a = {x: () => 42}
const b = {x: {y: 1}}
const result = deepAssign({}, a, b);

console.log(result.x()) // should print 42
console.log(result.x.y) // should print 1

Sorry, I'm not aware of any good solution to this, at the moment a.x is mutated by deep-assign.

If we take a look at the Object.assign Spec, then we see that Object.assign does not distinguish functions etc. from plain objects; therefore there is no (sufficient) reason for deep-assign to do so.

There are many merge tools doing this in a different way, for example:

from deep-assign.

schnittstabil avatar schnittstabil commented on July 19, 2024

shouldn't it mutate b too?

Well, I do not know. Did you expect that?

from deep-assign.

educastellano avatar educastellano commented on July 19, 2024

Well no, I wasn't expecting that 😅 Just some coherence, either all or none mutated.

Thanks for the answer @schnittstabil ! :)
I found assign-deep which seems to work well 👍

from deep-assign.

schnittstabil avatar schnittstabil commented on July 19, 2024

@educastellano Thanks for the link, I was not aware of that project. The crucial difference of deep-assign and assign-deep can be seen at assign-deep/index.js line 60 using kind-of:

var typeOf = require('kind-of');
// …
function isObject(obj) {
  return typeOf(obj) === 'object' || typeOf(obj) === 'function';
}

The problem I see here is:
The distinction between objects and non-objects is neither based on the behavior of Object.assign nor on what an object in JavaScript is. Therefore it should be is easy to find some unexpected behavior of assign-deep too.

I do not want to go into details of assign-deep, but as far as I can see, it probably would not meet your requirements – assign-deep also mutates sources:

const src1 = {
	b: new Buffer (1),
	p: Promise.resolve(42)
};

const src2 = {
	b: {c: 666},
	p: {q: 666},
};

assignDeep({}, src1, src2);

console.log(src1.b.c) // => 666

// but:
console.log(src1.p.q) // => undefined

from deep-assign.

educastellano avatar educastellano commented on July 19, 2024

Good to know... It doesn't seem very appropiate to use kind-of for that library. isObject() should say if the value is really an object (not buffer or boolean in case of new Boolean(),...) . /cc @jonschlinkert

from deep-assign.

Related Issues (20)

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.