Comments (11)
This is confusing me, and causing my app to crash.
I understand the choice to not allow root-level null targets (e.g., deep_assign(null, {a:1})
. However, I would expect to be able to assign to null values inside an object without throwing an error:
deep_assign( {a:1, b:null}, {b:99} )
// error
The library will reject assignment to any key that has a null value, regardless how deep it is in the structure.
from deep-assign.
I'm currently using lodash's _.merge()
for a similar use case.
from deep-assign.
"Note that Object.assign() does not throw on null or undefined source values."
It says source values, not target. This module behaves the same as Object.assign. Target must be an object, but sources can be anything.
Object.assign(null, {});
//=> Uncaught TypeError: Cannot convert undefined or null to object(…)
from deep-assign.
I'm seeing this in node v5.3.0:
> var deep_assign = require('deep-assign');
undefined
> deep_assign({foo:null},{foo:'bar'})
TypeError: Cannot convert undefined or null to object (foo)
> Object.assign({foo:null},{foo:'bar'})
{ foo: 'bar' }
from deep-assign.
@whizkid77 In my opinion that is the expected behavior, because we Recursively Object.assign():
deep_assign({foo:null}, {foo:'bar'})
// should be similar to
{foo: deep_assign(null, 'bar')}
// which have to be the same as
{foo: Object.assign(null, 'bar')}
from deep-assign.
Ah that's another way of looking at it. Either way is fine with me, but it should just explicitly be noted that this is the case in the readme. However, it would mean that deep-assign
cannot be a drop-in replacement for Object.assign
, and therefore limiting its usefulness (to me, anyway).
from deep-assign.
deep-assign
cannot be a drop-in replacement for Object.assign
Maybe sindresorhus/object-assign is what you seeking for.
Unfortunately, we can not do both: a (non-recursive) drop-in replacement for Object.assign and recursively Object.assign 😉
To sum it up, the current behavior is:
deep_assign({foo: 'foo'}, {foo: 'bar'})
//=> {foo: 'bar'}
deep_assign({ }, {foo: 'bar'})
//=> {foo: 'bar'}
deep_assign({foo: null}, {foo: 'bar'})
//=> TypeError: Cannot convert undefined or null to object (foo)
deep_assign({foo: undefined}, {foo: 'bar'})
//=> TypeError: Cannot convert undefined or null to object (foo)
from deep-assign.
For the first time, it confused me as well, but if you strictly see the definition of Object.assign
and recursion, you can see that @schnittstabil is right.
I'm afraid it prevents deep-assign from being a successful module because it's hard to understand the point at first. As I see most of the people look at deep assign as a deep-extend
function which is incorrect.
from deep-assign.
@jbellsey As Object.assign
does not allow that, I see currently no justification to do so in deep-assign
.
However, I'm always looking for real world examples, which allow us to reconsider the current behavior. May you share your use case with us – just a tiny snippet, e.g. a use case of your library function?
from deep-assign.
Fair enough; I will fork or find another solution.
My situation is this: the API returns a user record, which I cache locally. I poll the server for new data, and merge that into the local user record.
And (you saw this coming) the server sends fields as null
when there's no data.
// first poll:
user = {
device_features: {
locked: null, // here comes trouble...
pin: '1234'
},
settings: {
email: '[email protected]'
}
};
// poll for new device status, which should be merged into the user record:
new_device_data = {
locked: true, // in some cases, this is an object; any non-null value fails
pin: '1234' // this field is sometimes not included; we only get changed values
};
// here's where we error:
deepAssign(user.device_features, new_device_data);
This breaks my mental model. It makes sense to error if the target itself is null
, but not if a property somewhere inside the target is null
. That, to me, is a very abstract notion of applied recursion...recursive algorithms always allow for a special case at the margin.
Again, your library, your call.
from deep-assign.
@jbellsey Thanks for sharing, things are complicated as that is only one use case, so that would take some time – as an interim solution you may give merge-options a shot – but there a countless similar packages out there.
from deep-assign.
Related Issues (20)
- Check "support symbols as targets" fails on ava 0.5.0 HOT 1
- Overwriting array properties. HOT 4
- Simplify HOT 1
- Deep assigning objects with different property types HOT 1
- Does not work with sealed objects HOT 2
- What is the difference deep assigning with JSON.parse? HOT 1
- Assigning an array shorter than previous one results in merged array HOT 2
- deep assign and functions HOT 2
- property with null value in source object not overwriting to same property in target object HOT 6
- Source object get manipulated HOT 3
- it mutates *some* of the sources HOT 5
- Why should this module not be used? HOT 3
- regular expressions are being ignored HOT 2
- Can you add a param(value) which is equal undefined during assigning HOT 1
- Set not being overwritten HOT 3
- Deep-copying arrays HOT 17
- Incorrectly assign object with function as value HOT 2
- Regression in 2.0.0 HOT 11
- test fails: support symbols as targets HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from deep-assign.