Nested key support
This commit is contained in:
parent
2b8adbd740
commit
519c34bc60
5 changed files with 33 additions and 1 deletions
2
package-lock.json
generated
2
package-lock.json
generated
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "ergogen",
|
||||
"version": "0.1.0",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
|
|
|
@ -62,6 +62,7 @@ try {
|
|||
} catch (err) {
|
||||
throw new Error(`Malformed input within "${args.c}": ${err}`)
|
||||
}
|
||||
config = u.expand_nested_keys(config)
|
||||
|
||||
// points
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
module.exports = {
|
||||
utils: require('./utils'),
|
||||
points: require('./points'),
|
||||
outlines: require('./outlines'),
|
||||
cases: require('./cases'),
|
||||
|
|
23
src/utils.js
23
src/utils.js
|
@ -2,6 +2,29 @@ const m = require('makerjs')
|
|||
|
||||
exports.deepcopy = (value) => JSON.parse(JSON.stringify(value))
|
||||
|
||||
const deep_assign = exports.deep_assign = (obj, key, val) => {
|
||||
const levels = key.split('.')
|
||||
const last = levels.pop()
|
||||
let step = obj
|
||||
for (const level of levels) {
|
||||
step[level] = step[level] || {}
|
||||
step = step[level]
|
||||
}
|
||||
step[last] = val
|
||||
return obj
|
||||
}
|
||||
|
||||
const expand_nested_keys = exports.expand_nested_keys = (config) => {
|
||||
if (typeof config == 'object') {
|
||||
const result = {}
|
||||
for (const [key, val] of Object.entries(config)) {
|
||||
deep_assign(result, key, expand_nested_keys(val))
|
||||
}
|
||||
return result
|
||||
}
|
||||
return config
|
||||
}
|
||||
|
||||
const eq = exports.eq = (a=[], b=[]) => {
|
||||
return a[0] === b[0] && a[1] === b[1]
|
||||
}
|
||||
|
|
7
test/utils.js
Normal file
7
test/utils.js
Normal file
|
@ -0,0 +1,7 @@
|
|||
const u = require('../src/utils')
|
||||
|
||||
describe('Utils', function() {
|
||||
it('deep_assign', function() {
|
||||
u.deep_assign({}, 'a.b.c', 1).should.deep.equal({a: {b: {c: 1}}})
|
||||
})
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue