Skip to content

Commit

Permalink
Merge pull request #1 from TechnologyAdvice/fix/deep-assign
Browse files Browse the repository at this point in the history
fix(deep-assign): add deepAssign to objects in strategy create's
  • Loading branch information
ksafranski authored May 21, 2017
2 parents 887105b + f94abe3 commit 52e36d3
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 7 deletions.
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

0 comments on commit 52e36d3

Please sign in to comment.