Array unnest bugfix, fixes #50
This commit is contained in:
parent
e58b80c2a9
commit
483f214bec
2 changed files with 22 additions and 7 deletions
|
@ -33,7 +33,7 @@ const extend = exports.extend = (...args) => {
|
|||
}
|
||||
|
||||
const traverse = exports.traverse = (config, root, breadcrumbs, op) => {
|
||||
if (a.type(config)() !== 'object') return config
|
||||
if (a.type(config)() == 'object') {
|
||||
const result = {}
|
||||
for (const [key, val] of Object.entries(config)) {
|
||||
breadcrumbs.push(key)
|
||||
|
@ -41,6 +41,18 @@ const traverse = exports.traverse = (config, root, breadcrumbs, op) => {
|
|||
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 config
|
||||
}
|
||||
|
||||
exports.unnest = config => traverse(config, config, [], (target, key, val) => {
|
||||
|
|
|
@ -7,6 +7,9 @@ describe('Prepare', function() {
|
|||
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() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue