-
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 #10 from blackflux/dev
[Gally]: master <- dev
- Loading branch information
Showing
10 changed files
with
335 additions
and
6 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,67 @@ | ||
const objectScan = require('object-scan'); | ||
|
||
module.exports = (logic_ = {}) => { | ||
const logic = { '**': null, ...logic_ }; | ||
const last = (arr) => arr[arr.length - 1]; | ||
const mkChild = (ref) => { | ||
if (!(ref instanceof Object)) { | ||
return ref; | ||
} | ||
return Array.isArray(ref) ? [] : {}; | ||
}; | ||
const populate = (obj, key, fn, force = false) => { | ||
if (force === true || !(key in obj)) { | ||
// eslint-disable-next-line no-param-reassign | ||
obj[key] = fn(); | ||
return true; | ||
} | ||
return false; | ||
}; | ||
|
||
const scanner = objectScan(Object.keys(logic), { | ||
reverse: false, | ||
breakFn: ({ | ||
isMatch, property, value, matchedBy, context | ||
}) => { | ||
if (!isMatch) return; | ||
const { stack, groups, path } = context; | ||
const current = last(stack); | ||
const bestNeedle = last(matchedBy); | ||
const groupBy = typeof logic[bestNeedle] === 'function' | ||
? logic[bestNeedle](value) | ||
: logic[bestNeedle]; | ||
|
||
if (!Array.isArray(current) || groupBy === null) { | ||
if (Array.isArray(current)) { | ||
current.push(mkChild(value)); | ||
stack.push(last(current)); | ||
} else { | ||
populate(current, property, () => mkChild(value), !(value instanceof Object)); | ||
stack.push(current[property]); | ||
} | ||
} else { | ||
const groupId = `${bestNeedle}.${groupBy}: ${path.join('.')}`; | ||
populate(groups, groupId, () => ({})); | ||
const groupEntryId = value[groupBy]; | ||
if (populate(groups[groupId], groupEntryId, () => mkChild(value))) { | ||
current.push(groups[groupId][groupEntryId]); | ||
} | ||
path.push(`${groupBy}=${groupEntryId}`); | ||
stack.push(groups[groupId][groupEntryId]); | ||
} | ||
}, | ||
filterFn: ({ matchedBy, context }) => { | ||
const { stack, path } = context; | ||
stack.pop(); | ||
if (logic[last(matchedBy)] !== null) { | ||
path.pop(); | ||
} | ||
} | ||
}); | ||
return (...args) => { | ||
const result = mkChild(args[0]); | ||
const groups = {}; | ||
args.forEach((arg) => scanner(arg, { stack: [result], groups, path: [] })); | ||
return result; | ||
}; | ||
}; |
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,7 +1,9 @@ | ||
const align = require('./core/align'); | ||
const contain = require('./core/contain'); | ||
const Merge = require('./core/merge'); | ||
|
||
module.exports = { | ||
align, | ||
contain | ||
contain, | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const expect = require('chai').expect; | ||
const { describe } = require('node-tdd'); | ||
const Merge = require('../../src/core/merge'); | ||
|
||
describe('Testing Merge', () => { | ||
it('Testing SO question: https://stackoverflow.com/questions/65822248', ({ fixture }) => { | ||
const json1 = fixture('json1'); | ||
const json2 = fixture('json2'); | ||
const merge = Merge({ | ||
'[*]': 'id', | ||
'[*].addresses[*]': 'type' | ||
}); | ||
expect(merge(json1, json2)).to.deep.equal(fixture('result')); | ||
}); | ||
|
||
it('Testing array concat', () => { | ||
const d1 = [{ a: 1 }]; | ||
const d2 = [{ a: 2 }]; | ||
expect(Merge()(d1, d2)).to.deep.equal([{ a: 1 }, { a: 2 }]); | ||
}); | ||
|
||
it('Testing array merge', () => { | ||
const d1 = [{ a: 1, b: 1, c: 3 }]; | ||
const d2 = [{ a: 1, b: 2, d: 4 }]; | ||
expect(Merge({ '[*]': 'a' })(d1, d2)).to.deep.equal([{ ...d1[0], ...d2[0] }]); | ||
}); | ||
|
||
it('Testing merge by sum', () => { | ||
const d1 = [[1, 2, 3], [2, 4], [1, 2]]; | ||
const d2 = [[3, 3], [1, 5], [3, 2]]; | ||
expect(Merge({ | ||
'[*]': (o) => o.reduce((a, b) => a + b, 0) | ||
})(d1, d2)).to.deep.equal([ | ||
[1, 2, 3, 2, 4, 3, 3, 1, 5], | ||
[1, 2], | ||
[3, 2] | ||
]); | ||
}); | ||
}); |
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,30 @@ | ||
[ | ||
{ | ||
"id": 1, | ||
"name": "aaa", | ||
"addresses": [ | ||
{ | ||
"type": "office", | ||
"city": "office city" | ||
}, | ||
{ | ||
"type": "home1", | ||
"city": "home1 city" | ||
} | ||
] | ||
}, | ||
{ | ||
"id": 2, | ||
"name": "bbb", | ||
"addresses": [ | ||
{ | ||
"type": "office", | ||
"city": "office city" | ||
}, | ||
{ | ||
"type": "home1", | ||
"city": "home1 city" | ||
} | ||
] | ||
} | ||
] |
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,30 @@ | ||
[ | ||
{ | ||
"id": 1, | ||
"name": "aaa1", | ||
"addresses": [ | ||
{ | ||
"type": "home1", | ||
"city": "home1 new city" | ||
}, | ||
{ | ||
"type": "home2", | ||
"city": "home2 city" | ||
} | ||
] | ||
}, | ||
{ | ||
"id": 3, | ||
"name": "ccc", | ||
"addresses": [ | ||
{ | ||
"type": "home1", | ||
"city": "home1 city" | ||
}, | ||
{ | ||
"type": "home2", | ||
"city": "home2 city" | ||
} | ||
] | ||
} | ||
] |
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,48 @@ | ||
[ | ||
{ | ||
"id": 1, | ||
"name": "aaa1", | ||
"addresses": [ | ||
{ | ||
"type": "office", | ||
"city": "office city" | ||
}, | ||
{ | ||
"type": "home1", | ||
"city": "home1 new city" | ||
}, | ||
{ | ||
"type": "home2", | ||
"city": "home2 city" | ||
} | ||
] | ||
}, | ||
{ | ||
"id": 2, | ||
"name": "bbb", | ||
"addresses": [ | ||
{ | ||
"type": "office", | ||
"city": "office city" | ||
}, | ||
{ | ||
"type": "home1", | ||
"city": "home1 city" | ||
} | ||
] | ||
}, | ||
{ | ||
"id": 3, | ||
"name": "ccc", | ||
"addresses": [ | ||
{ | ||
"type": "home1", | ||
"city": "home1 city" | ||
}, | ||
{ | ||
"type": "home2", | ||
"city": "home2 city" | ||
} | ||
] | ||
} | ||
] |
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
Oops, something went wrong.