@cdxoo/flat
Advanced tools
Comparing version 0.0.2 to 0.0.3
{ | ||
"name": "@cdxoo/flat", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "does not flatten cats", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -5,3 +5,7 @@ 'use strict'; | ||
var flatten = (that, options = {}) => { | ||
var { delimiter = '.' } = options; | ||
var { | ||
delimiter = '.', | ||
maxDepth = 0, | ||
} = options; | ||
var out = {}; | ||
@@ -11,5 +15,12 @@ | ||
var { isLeaf, path, value } = context; | ||
if (isLeaf) { | ||
out[path.join(delimiter)] = value; | ||
if (maxDepth) { | ||
if (path.length === maxDepth) { | ||
out[path.join(delimiter)] = value; | ||
} | ||
} | ||
else { | ||
if (isLeaf) { | ||
out[path.join(delimiter)] = value; | ||
} | ||
} | ||
}, options) | ||
@@ -16,0 +27,0 @@ |
@@ -49,1 +49,26 @@ 'use strict'; | ||
}) | ||
describe('flatten() maxDepth', () => { | ||
var pairs = [ | ||
[ | ||
3, | ||
{ foo: { bar: { baz: { quux: 42 }}}}, | ||
{ 'foo.bar.baz': { quux: 42 }} | ||
] | ||
]; | ||
it('flatten', () => { | ||
for (var it of pairs) { | ||
var [ maxDepth, raw, flat ] = it; | ||
expect(flatten(raw, { maxDepth })).to.deep.eql(flat); | ||
} | ||
}); | ||
it('unflatten', () => { | ||
for (var it of pairs) { | ||
var [ maxDepth, raw, flat ] = it; | ||
expect(unflatten(flat)).to.deep.eql(raw); | ||
} | ||
}) | ||
}) |
4499
107