Array unnest bugfix, fixes #50

This commit is contained in:
Bán Dénes 2021-12-12 13:23:33 +01:00
parent e58b80c2a9
commit 483f214bec
2 changed files with 22 additions and 7 deletions

View file

@ -33,14 +33,26 @@ const extend = exports.extend = (...args) => {
}
const traverse = exports.traverse = (config, root, breadcrumbs, op) => {
if (a.type(config)() !== 'object') return config
const result = {}
for (const [key, val] of Object.entries(config)) {
breadcrumbs.push(key)
op(result, key, traverse(val, root, breadcrumbs, op), root, breadcrumbs)
breadcrumbs.pop()
if (a.type(config)() == 'object') {
const result = {}
for (const [key, val] of Object.entries(config)) {
breadcrumbs.push(key)
op(result, key, traverse(val, root, breadcrumbs, op), root, breadcrumbs)
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) => {

View file

@ -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() {