sindresorhus / deep-assign Goto Github PK
View Code? Open in Web Editor NEW[DEPRECATED] Recursive Object.assign()
License: MIT License
[DEPRECATED] Recursive Object.assign()
License: MIT License
The github description says [DON'T USE THIS MODULE PLEASE]
but the readme doesn't say why not, and looking at the open issues doesn't make it clear either.
It would be good to put a sentence or two in the readme that says what's wrong with the module.
I have to assign an object to another that could have prop of different kind.
In this example:
var assign = require('deep-assign');
assign({ prop: 'test1' }, { prop: ['u'] });
deep-assign try to set string characters to elements of the ['u']
array, and it throws
TypeError: Cannot assign to read only property '0' of [object String]
.
You can test it on tonicdev
I was instead expecting to obtain as result { prop: ['u'] }. That is, I was expecting the whole target prop
to be overwritten by the source prop
, being it a string and not an object.
Is this by design? If not , I could write a PR to implement the expectation I explain.
Node version: 7.3.0
var deepAssign = require('deep-assign')
var o1 = {a: {b: 2}, d: {e: 5}}
var o2 = {a: {c: 3}, d: {f: 6}}
var o3 = deepAssign({}, o1, o2)
console.log(o3) // { a: { b: 2, c: 3 }, d: { e: 5, f: 6 } }
console.log(o1) // { a: { b: 2, c: 3 }, d: { e: 5, f: 6 } }
Source object should be kept intact, like Object.assign
.
Instead of provider: function... - received provider: {}
console.log(assign({data:{test:1}}, {data:{provider:function() {}, test:2}}));
{ data: { test: 2, provider: {} } }
See ES6 Code below:
class Model {
constructor(props) {
this.init();
Object.seal(this);
if (props) {
deepAssign(this, props);
}
}
init() {
Object.defineProperties(this, {
one: {
value: null,
writable: true,
enumerable: true
},
two: {
value: {},
writable: true,
enumerable: true
}
});
Object.defineProperties(this.two, {
three: {
value: null,
writable: true,
enumerable: true
},
four: {
value: null,
writable: true,
enumerable: true
}
});
}
}
console.log(new Model({one: 'foo', two: {three: 'bar', four: 'baz', five: 'shouldn\'t appear'}, six: 'won\'t appear'}));
I have encountered this problem when I tried to remove an object from an array and re-assign it to an object.
I have broken down the problem into this simple example:
const deepAssign = require('deep-assign');
const result = deepAssign({}, {
a: [1, 2]
}, {
a: [3]
});
console.log(result.a) // => [3, 2]
I expected result.a
to be [3]
instead.
The native Object.assign
from v8 yields a different result:
const result = Object.assign({}, {
a: [1, 2]
}, {
a: [3]
});
console.log(result.a) // => [3]
This leads me to believe that this is a bug in deep-assign
.
regular expressions are ignored.
console.log(deepAssign({
str: "a", reg: /a/
}, {
str: "b", reg: /b/
})); // results in { str: 'b', reg: /a/ } instead of { str: 'b', reg: /b/ }
Is this the correct behaviour...?
let a = {x:{}}
let b = {x: {y: 1}}
let c = {x: {y: 2}}
deepAssign({}, a, b, c)
// { x: { y: 2 } }
console.log(a)
// { x: { y: 2 } } <-- mutated!
console.log(b)
// { x: { y: 1 } }
console.log(c)
// { x: { y: 2 } }
It's quite a dangerous thing to mutate the sources, but if that's what this lib wants to be, shouldn't it mutate b
too?
Hello,
When i try using deep-assign on an object referencing functions i get strange results.
Here is my example:
var deepAssign = require('deep-assign');
function test1() {
console.log('test1')
};
function test2() {
console.log('test2')
};
var classic = Object.assign(
{
a:test1,
},{
a:test2
}
);
var deep = deepAssign(
{
a:test1,
},{
a: test2,
}
);
console.log('classic assign' ,classic); // should be { a: test2 } and is { a: test2 }
console.log('deep assign', deep); // should be { a: test2 } but is { a: test1 }
console.log('is property a equal in classic and deep?', classic.a === deep.a) // should be true but is false
You can test this code here: https://runkit.com/belgac/582475d188bb01001414cdc5
Is this normal or should this be fixed?
const assign = require('deep-assign')
const foo = {
keys: new Set(['123456'])
}
const bar = assign({}, {keys: new Set()}, foo)
console.log('foo.keys.size = ', foo.keys.size)
console.log('bar.keys.size = ', bar.keys.size)
console.log('foo.keys.has(123456) = ', foo.keys.has('123456'))
console.log('bar.keys.has(123456) = ', bar.keys.has('123456'))
Yields:
foo.keys.size = 1
bar.keys.size = 0
foo.keys.has(123456) = true
bar.keys.has(123456) = false
Hi Dev,
Following is not working correct:
var deepAssign = require("deep-assign");
deepAssign({}, {Lat: null, Lon: null}, {Lat: null, Lon: 2})
output is:
{Lon: 2}
It must be:
{Lat: null, Lon 2}
Due to this weird issue, my form validation is failing as Lat property is totally missing from output.
If I use Object.assign, it works correctly.
Can you plz guide me what is wrong?
let deepClone = JSON.parse(JSON.stringify(objectToBeCloned))
The speed maybe? About speed: http://stackoverflow.com/a/5344074/115363
Or JSON is just for simple objects?
โ support symbols as targets
t.is(target.sym.constructor, Symbol)
| |
#function# #function#
Is this lib meant to deep-copy arrays? I expected it to, but it doesn't. Here's a failing test.
test('deep-copies arrays', t => {
// original object with an array
var original = {arr: ['one', 'two', 'three']};
// deep-duplicate it
var dupe = fn({}, original);
// change an array element in the original
original.arr[0] = 'xxxxxxx';
// the duplicate should not be affected
t.is(dupe.arr[0], 'one');
t.end();
});
now:
deepAssign({a: {b: 0}}, {a: {b: 1, c: 2}}, {a: {c: 3}}); //=> {a: {b: 1, c: 3}} deepAssign({a: {b: 0}}, {a: {b: 1, c: 2}}, {a: {c: 3, b: undefined}}); //=> {a: {b: 1, c: 3}}
if (b === ' ' || 0 || false)
deepAssign({a: {b: 0}}, {a: {b: 1, c: 2}}, {a: {c: 3, b: '' }});
//=> {a: {b: 1, c: 3}}
not
//=> {a: {b: '' , c: 3}}
we can add any param regard as undefined
Thank you!
Is this intended behavior?
deepAssign({a: [1]}, {a: []}) => Object {a: Array[1]}
Based on the behavior of Object.assign
(this module is just a recursive version of it correct?), I would have assumed it should have returned Object {a: Array[0]}
Just a thought, but this couldn't this be simplified a lot more with ES6?
Here's what I was able to come up with: https://gist.github.com/Salakar/1d7137de9cb8b704e48a
But if you use ava 0.3.0 the check is pass successfully.
Is this problem on ava side? If so, then I think we need to specify ava version in package.json, create issue in ava repo and refer to this issue.
This commit b55d7b6 seems to cause problem in XO.
I've added a failing test: efa61ab
It seems at deep level it no longer creates a new object but reuses the existing one.
@schnittstabil Could you take a look?
Object.assign does not throw TypeErrors, but the polyfill does. Is it possible to make the behaviors consistent?
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
"Note that Object.assign() does not throw on null or undefined source values."
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.