-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(deep-assign): add deepAssign to objects in strategy create's #1
Changes from 2 commits
a94a1ed
ff26d14
1c18db9
fe1b687
f94abe3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,7 @@ | |
] | ||
}, | ||
"dependencies": { | ||
"deep-assign": "^2.0.0", | ||
"halcyon": "^0.19.1" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
const deepAssign = require('deep-assign') | ||
const { without } = require('halcyon') | ||
|
||
module.exports = { | ||
|
@@ -30,7 +31,9 @@ module.exports = { | |
if (mods.remove && !Array.isArray(mods.remove)) throw new Error(`Remove property must be an array`) | ||
mods.add = mods.add || [] | ||
mods.remove = mods.remove || [] | ||
// Deep assign nested objects | ||
const assign = (arr) => arr.map((x) => typeof x === 'object' ? deepAssign({}, x) : x) | ||
// Apply modifications and return | ||
return without(mods.remove, [ ...data ]).concat([ ...mods.add ]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think what you want is: without(mods.remove, [...data.map(assign)].concat([...mods.add.map(assign)]) You're already shallow-cloning the array with |
||
return without(mods.remove, [ ...assign(data) ]).concat([ ...assign(mods.add) ]) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,13 +17,13 @@ describe('plugins/array', () => { | |
}) | ||
describe('create', () => { | ||
it('returns a new instance of the array with mods applied', () => { | ||
const original = [ 'foo', 'bar' ] | ||
const original = [ 'foo', { fizz: 'buzz' }, 'bar' ] | ||
const actual = array.create(original, { | ||
add: [ 'baz' ], | ||
remove: [ 'bar' ] | ||
}) | ||
expect(original).to.deep.equal([ 'foo', 'bar' ]) | ||
expect(actual).to.deep.equal([ 'foo', 'baz' ]) | ||
expect(original).to.deep.equal([ 'foo', { fizz: 'buzz' }, 'bar' ]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May want to perform two comparisons here, one to check that references are broken, and the other to confirm deep equality: const obj = { fizz: 'buzz' }
const original = [ 'foo', { fizz: 'buzz' }, 'bar' ]
// compare references
expect(original[1]).to.not.equal(actual[1])
// compare equality
expect(original[1]).to.deep.equal(actual[1]) This way you're ensuring the feature fundamentally offered by |
||
expect(actual).to.deep.equal([ 'foo', { fizz: 'buzz' }, 'baz' ]) | ||
}) | ||
it('throws if mods argument is not an object', () => { | ||
expect(() => array.create([ 'foo' ], 'bar')).to.throw(/Must supply a valid object/) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sindresorhus/deep-assign#26
Not sure what the deal is here. Perhaps consider using https://www.npmjs.com/package/lodash.merge, which performs a deep merge w/o pulling in the entirety of lodash.