-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from blackflux/dev
[Gally]: master <- dev
- Loading branch information
Showing
12 changed files
with
269 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const objectScan = require('object-scan'); | ||
const last = require('../util/last'); | ||
const mkChild = require('../util/mk-child'); | ||
|
||
module.exports = (obj, needles = []) => { | ||
const hasDoubleStar = needles.includes('**'); | ||
const breakLength = hasDoubleStar ? 0 : 1; | ||
return objectScan(hasDoubleStar ? needles : ['**', ...needles], { | ||
reverse: false, | ||
breakFn: ({ | ||
isMatch, property, value, context, getMatchedBy | ||
}) => { | ||
if (!isMatch) { | ||
return property !== undefined; | ||
} | ||
const ref = last(context); | ||
const doBreak = getMatchedBy().length > breakLength; | ||
const v = doBreak ? value : mkChild(value); | ||
if (Array.isArray(ref)) { | ||
ref.push(v); | ||
context.push(last(ref)); | ||
} else { | ||
ref[property] = v; | ||
context.push(ref[property]); | ||
} | ||
return doBreak; | ||
}, | ||
filterFn: ({ context }) => { | ||
context.pop(); | ||
} | ||
})(obj, [mkChild(obj)])[0]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
const align = require('./core/align'); | ||
const clone = require('./core/clone'); | ||
const contains = require('./core/contains'); | ||
const Merge = require('./core/merge'); | ||
|
||
module.exports = { | ||
align, | ||
clone, | ||
contains, | ||
Merge | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
const expect = require('chai').expect; | ||
const { describe } = require('node-tdd'); | ||
const objectScan = require('object-scan'); | ||
const sampleSize = require('lodash.samplesize'); | ||
const cloneDeep = require('lodash.clonedeep'); | ||
const clone = require('../../src/core/clone'); | ||
const genData = require('./gen-data'); | ||
|
||
describe('Testing clone', { timeout: 100000 }, () => { | ||
it('Batch test (deep)', ({ fixture }) => { | ||
const refDiff = fixture('ref-diff'); | ||
const asRefDiff = fixture('as-ref-diff'); | ||
for (let x = 0; x < 5000; x += 1) { | ||
const data = genData(); | ||
const dataX = cloneDeep(data); | ||
const cloned = clone(data); | ||
expect(dataX).to.deep.equal(data); | ||
expect(data).to.deep.equal(cloned); | ||
expect(refDiff(data, cloned)).to.deep.equal(asRefDiff(data)); | ||
} | ||
}); | ||
|
||
it('Batch test (shallow)', ({ fixture }) => { | ||
const refDiff = fixture('ref-diff'); | ||
const asRefDiff = fixture('as-ref-diff'); | ||
for (let x = 0; x < 5000; x += 1) { | ||
const data = genData(); | ||
const dataX = cloneDeep(data); | ||
const cloned = clone(data, ['**']); | ||
expect(dataX).to.deep.equal(data); | ||
expect(data).to.deep.equal(cloned); | ||
expect(refDiff(data, cloned)).to.deep.equal(asRefDiff(data, ['**'])); | ||
} | ||
}); | ||
|
||
it('Batch test (random shallow)', ({ fixture }) => { | ||
const refDiff = fixture('ref-diff'); | ||
const asRefDiff = fixture('as-ref-diff'); | ||
for (let x = 0; x < 5000; x += 1) { | ||
const data = genData(); | ||
const dataX = cloneDeep(data); | ||
const allKeys = objectScan(['**'], { joined: true })(data); | ||
const selectedKeys = sampleSize(allKeys, Math.floor(Math.random() * allKeys.length) + 1); | ||
const cloned = clone(data, selectedKeys); | ||
expect(dataX).to.deep.equal(data); | ||
expect(data).to.deep.equal(cloned); | ||
expect(refDiff(data, cloned)).to.deep.equal(asRefDiff(data, selectedKeys)); | ||
} | ||
}); | ||
|
||
it('Batch test (random exclude)', ({ fixture }) => { | ||
const cloneWithout = fixture('clone-without'); | ||
for (let x = 0; x < 5000; x += 1) { | ||
const data = genData(); | ||
const dataX = cloneDeep(data); | ||
const allKeys = objectScan(['**'], { joined: true })(data); | ||
const selectedKeys = sampleSize(allKeys, Math.floor(Math.random() * allKeys.length) + 1); | ||
const excludeKeys = selectedKeys.map((k) => `!${k}`); | ||
const cloned = clone(data, excludeKeys); | ||
expect(dataX).to.deep.equal(data); | ||
expect(cloned).to.deep.equal(cloneWithout(data, selectedKeys)); | ||
} | ||
}); | ||
|
||
it('Test simple', () => { | ||
const data = { | ||
a: 1, | ||
b: { x: 2, y: { /* complex object */ } }, | ||
c: [{ /* complex object */ }, { z: 3 }] | ||
}; | ||
const cloned = clone(data, ['b.y', 'c[0]']); | ||
expect(data).to.deep.equal(cloned); | ||
expect(data).to.not.equal(cloned); | ||
expect(data.b).to.not.equal(cloned.b); | ||
expect(data.y).to.equal(cloned.y); | ||
expect(data.c[0]).to.equal(cloned.c[0]); | ||
expect(data.c[1]).to.not.equal(cloned.c[1]); | ||
}); | ||
|
||
it('Test shallow clone', () => { | ||
const data = { a: {} }; | ||
const cloned = clone(data, ['**']); | ||
expect(data).to.deep.equal(cloned); | ||
expect(data).to.not.equal(cloned); | ||
expect(data.a).to.equal(cloned.a); | ||
}); | ||
|
||
it('Test exclude', () => { | ||
const data = { a: {} }; | ||
const cloned = clone(data, ['!a']); | ||
expect(cloned).to.deep.equal({}); | ||
}); | ||
|
||
it('Test complex exclude one', ({ fixture }) => { | ||
const cloneWithout = fixture('clone-without'); | ||
const data = { C: { A: undefined, C: [] }, B: [] }; | ||
const cloned = clone(data, ['!C', '!C.A', '!B']); | ||
const excluded = cloneWithout(data, ['C', 'C.A', 'B']); | ||
expect(cloned).to.deep.equal(excluded); | ||
}); | ||
|
||
it('Test complex exclude two', ({ fixture }) => { | ||
const cloneWithout = fixture('clone-without'); | ||
const data = { B: {}, C: {} }; | ||
const cloned = clone(data, ['!C']); | ||
const excluded = cloneWithout(data, ['C']); | ||
expect(cloned).to.deep.equal(excluded); | ||
}); | ||
|
||
it('Test complex exclude three', ({ fixture }) => { | ||
const cloneWithout = fixture('clone-without'); | ||
const data = { B: [2, []] }; | ||
const cloned = clone(data, ['!B[0]']); | ||
const excluded = cloneWithout(data, ['B[0]']); | ||
expect(cloned).to.deep.equal(excluded); | ||
}); | ||
|
||
it('Test exclude, shallow and deep clone', () => { | ||
const data = { a: {}, b: {}, c: [{}, {}] }; | ||
const cloned = clone(data, ['b', '!c[0]', 'c[1]']); | ||
expect(cloned).to.deep.equal({ a: {}, b: {}, c: [{}] }); | ||
expect(data.a).to.not.equal(cloned.a); | ||
expect(data.b).to.equal(cloned.b); | ||
expect(data.c).to.not.equal(cloned.c); | ||
expect(data.c[1]).to.equal(cloned.c[0]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const clonedeep = require('lodash.clonedeep'); | ||
const objectScan = require('object-scan'); | ||
|
||
module.exports = (obj_, needles = []) => { | ||
if (!(obj_ instanceof Object)) { | ||
return true; | ||
} | ||
const obj = clonedeep(obj_); | ||
const hasDoubleStar = needles.includes('**'); | ||
const breakLength = hasDoubleStar ? 0 : 1; | ||
objectScan(hasDoubleStar ? needles : ['**', ...needles], { | ||
breakFn: ({ | ||
isMatch, parent, property, isLeaf, matchedBy | ||
}) => { | ||
if (!isMatch) { | ||
return false; | ||
} | ||
if (matchedBy.length > breakLength || isLeaf) { | ||
// eslint-disable-next-line no-param-reassign | ||
parent[property] = true; | ||
return true; | ||
} | ||
return false; | ||
} | ||
})(obj); | ||
return obj; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const clonedeep = require('lodash.clonedeep'); | ||
const objectScan = require('object-scan'); | ||
|
||
module.exports = (obj_, needles) => { | ||
if (!(obj_ instanceof Object)) { | ||
return obj_; | ||
} | ||
const obj = clonedeep(obj_); | ||
objectScan(needles, { | ||
breakFn: ({ isMatch, parent, property }) => { | ||
if (!isMatch) { | ||
return false; | ||
} | ||
if (Array.isArray(parent)) { | ||
parent.splice(property, 1); | ||
} else { | ||
// eslint-disable-next-line no-param-reassign | ||
delete parent[property]; | ||
} | ||
return true; | ||
} | ||
})(obj); | ||
return obj; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const refDiff = (o1, o2) => { | ||
if (o1 === o2) { | ||
return true; | ||
} | ||
if (Array.isArray(o1)) { | ||
return Object.entries(o1) | ||
.map(([k, v]) => refDiff(v, o2[k])); | ||
} | ||
return Object.entries(o1) | ||
.reduce((prev, [k, v]) => { | ||
// eslint-disable-next-line no-param-reassign | ||
prev[k] = refDiff(v, o2[k]); | ||
return prev; | ||
}, {}); | ||
}; | ||
|
||
module.exports = refDiff; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4495,6 +4495,11 @@ [email protected]: | |
resolved "https://registry.yarnpkg.com/lodash.mergewith/-/lodash.mergewith-4.6.2.tgz#617121f89ac55f59047c7aec1ccd6654c6590f55" | ||
integrity sha512-GK3g5RPZWTRSeLSpgP8Xhra+pnjBC56q9FZYe1d5RN3TJ35dbkGy3YqBSMbyCrlbi+CM9Z3Jk5yTL7RCsqboyQ== | ||
|
||
[email protected]: | ||
version "4.2.0" | ||
resolved "https://registry.yarnpkg.com/lodash.samplesize/-/lodash.samplesize-4.2.0.tgz#460762fbb2b342290517499e90d51586db465ff9" | ||
integrity sha1-Rgdi+7KzQikFF0mekNUVhttGX/k= | ||
|
||
[email protected], lodash.set@^4.3.2: | ||
version "4.3.2" | ||
resolved "https://registry.yarnpkg.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23" | ||
|