Array unnest bugfix, fixes #50
This commit is contained in:
parent
e58b80c2a9
commit
483f214bec
2 changed files with 22 additions and 7 deletions
|
@ -33,14 +33,26 @@ const extend = exports.extend = (...args) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const traverse = exports.traverse = (config, root, breadcrumbs, op) => {
|
const traverse = exports.traverse = (config, root, breadcrumbs, op) => {
|
||||||
if (a.type(config)() !== 'object') return config
|
if (a.type(config)() == 'object') {
|
||||||
const result = {}
|
const result = {}
|
||||||
for (const [key, val] of Object.entries(config)) {
|
for (const [key, val] of Object.entries(config)) {
|
||||||
breadcrumbs.push(key)
|
breadcrumbs.push(key)
|
||||||
op(result, key, traverse(val, root, breadcrumbs, op), root, breadcrumbs)
|
op(result, key, traverse(val, root, breadcrumbs, op), root, breadcrumbs)
|
||||||
breadcrumbs.pop()
|
breadcrumbs.pop()
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
} else if (a.type(config)() == 'array') {
|
||||||
|
const result = []
|
||||||
|
let index = 0
|
||||||
|
for (const val of config) {
|
||||||
|
breadcrumbs.push(`[${index}]`)
|
||||||
|
result[index] = traverse(val, root, breadcrumbs, op)
|
||||||
|
breadcrumbs.pop()
|
||||||
|
index++
|
||||||
|
}
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
return result
|
return config
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.unnest = config => traverse(config, config, [], (target, key, val) => {
|
exports.unnest = config => traverse(config, config, [], (target, key, val) => {
|
||||||
|
|
|
@ -7,6 +7,9 @@ describe('Prepare', function() {
|
||||||
d: 2,
|
d: 2,
|
||||||
'e.f': 3
|
'e.f': 3
|
||||||
}}).should.deep.equal({a: {b: {c: {d: 2, e: {f: 3}}}}})
|
}}).should.deep.equal({a: {b: {c: {d: 2, e: {f: 3}}}}})
|
||||||
|
p.unnest({'root': [{
|
||||||
|
'a.b': 1
|
||||||
|
}]}).should.deep.equal({root: [{a: {b: 1}}]})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('extend', function() {
|
it('extend', function() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue