Skip to content
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

Merged
merged 5 commits into from
May 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
]
},
"dependencies": {
"halcyon": "^0.19.1"
"halcyon": "^0.19.1",
"lodash.merge": "^4.6.0"
}
}
5 changes: 4 additions & 1 deletion src/plugins/array.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const merge = require('lodash.merge')
const { without } = require('halcyon')

module.exports = {
Expand Down Expand Up @@ -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' ? merge({}, x) : x)
// Apply modifications and return
return without(mods.remove, [ ...data ]).concat([ ...mods.add ])
return without(mods.remove, [ ...assign(data) ]).concat([ ...assign(mods.add) ])
}
}
4 changes: 3 additions & 1 deletion src/plugins/json.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const merge = require('lodash.merge')

module.exports = {
/**
* @property {String} plugin name
Expand All @@ -24,6 +26,6 @@ module.exports = {
*/
create: (data, mods = {}) => {
if (typeof mods !== 'object') throw new Error(`Must supply a valid object`)
return JSON.stringify(Object.assign({}, data, mods))
return JSON.stringify(merge({}, data, mods))
}
}
4 changes: 3 additions & 1 deletion src/plugins/object.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const merge = require('lodash.merge')

module.exports = {
/**
* @property {String} plugin name
Expand All @@ -21,6 +23,6 @@ module.exports = {
*/
create: (data, mods = {}) => {
if (typeof mods !== 'object') throw new Error(`Must supply a valid object`)
return Object.assign({}, data, mods)
return merge({}, data, mods)
}
}
6 changes: 3 additions & 3 deletions test/src/plugins/array.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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[1]).to.not.equal(actual[1])
expect(original[1]).to.deep.equal(actual[1])
})
it('throws if mods argument is not an object', () => {
expect(() => array.create([ 'foo' ], 'bar')).to.throw(/Must supply a valid object/)
Expand Down
1 change: 1 addition & 0 deletions test/src/plugins/object.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('plugin/object', () => {
const actual = object.create(original, { foo: 'biz' })
expect(original).to.deep.equal({ foo: 'bar' })
expect(actual).to.deep.equal({ foo: 'biz' })
expect(actual).to.not.equal(original)
})
it('throws if mods argument is not an object', () => {
expect(() => object.create({ foo: 'bar' }, 'biz')).to.throw(/Must supply a valid object/)
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,10 @@ lodash.keys@^3.0.0:
lodash.isarguments "^3.0.0"
lodash.isarray "^3.0.0"

lodash.merge@^4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.0.tgz#69884ba144ac33fe699737a6086deffadd0f89c5"

lodash@^4.0.0, lodash@^4.3.0:
version "4.17.4"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"
Expand Down