@cdxoo/flat
Advanced tools
Comparing version 0.0.6 to 0.0.7
{ | ||
"name": "@cdxoo/flat", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "does not flatten cats", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -47,2 +47,11 @@ 'use strict'; | ||
var isLast = i === path.length - 1; | ||
if (current === null || current === undefined) { | ||
throw new Error(inlineErrorMsg(` | ||
Cannot create property '${token}' | ||
on ${typeof current} '${current}' | ||
(path: '${path.join('.')}') | ||
`)); | ||
} | ||
if (!Object.keys(current).includes(token)) { | ||
@@ -54,3 +63,5 @@ var value = isLast ? that[key] : {}; | ||
handlePropertiesOnNonObjects({ | ||
root: out, | ||
parent, | ||
erroneousPath: path.slice(0, i), | ||
erroneousKey: parentToken, | ||
@@ -63,3 +74,3 @@ erroneousValue: current, | ||
else { | ||
throw new Error(` | ||
throw new Error(inlineErrorMsg(` | ||
Cannot create property '${token}' | ||
@@ -69,3 +80,3 @@ on ${typeof current} '${current}' | ||
in ${JSON.stringify(out)}) | ||
`.replace(/(^\s+|\s+$)/g, '').replace(/\s+/g, ' ')) | ||
`)) | ||
} | ||
@@ -85,2 +96,6 @@ } | ||
var inlineErrorMsg = (str) => ( | ||
str.replace(/(^\s+|\s+$)/g, '').replace(/\s+/g, ' ') | ||
); | ||
flatten.flatten = flatten; | ||
@@ -87,0 +102,0 @@ flatten.unflatten = unflatten; |
@@ -121,3 +121,3 @@ 'use strict'; | ||
describe('unflatten mixed in non-objects', () => { | ||
it('throws useful error by default', () => { | ||
it('throws useful error by default (simple case)', () => { | ||
var error = undefined; | ||
@@ -128,3 +128,3 @@ try { | ||
'foo.bar.baz': true | ||
}, { handleNonObjectProperties: ''}); | ||
}); | ||
} catch (e) { | ||
@@ -139,4 +139,22 @@ error = e; | ||
}) | ||
it('throws useful error by default (wierd case)', () => { | ||
var error; | ||
try { | ||
unflatten({ | ||
'quux.x': 42, | ||
'foo.bar': null, | ||
'foo.bar.0.baz': true | ||
}); | ||
} catch (e) { | ||
error = e; | ||
} | ||
expect(error.message).to.eql(` | ||
Cannot create property '0' on object 'null' | ||
(path: 'foo.bar.0.baz') | ||
`.replace(/(^\s+|\s+$)/g, '').replace(/\s+/g, ' ')) | ||
}) | ||
it('user can pass custom handler', () => { | ||
it('user can handle simple mixing', () => { | ||
var out = unflatten({ | ||
@@ -163,2 +181,39 @@ 'quux.x': 42, | ||
}) | ||
it('user can handle weird mixing', () => { | ||
var out = unflatten({ | ||
'quux.x': 42, | ||
'foo.bar': 'none', | ||
'foo.bar.0.baz': true | ||
}, { handlePropertiesOnNonObjects: (bag) => { | ||
var { | ||
root, | ||
parent, | ||
erroneousPath, | ||
erroneousKey, | ||
erroneousValue, | ||
currentKey, | ||
currentValue | ||
} = bag; | ||
if (typeof parent !== 'object') { | ||
var current = root; | ||
for (var [ ix, token ] of erroneousPath.entries()) { | ||
if (typeof current[token] !== 'object') { | ||
current[token] = {}; | ||
} | ||
current = current[token]; | ||
} | ||
current[currentKey] = currentValue; | ||
} | ||
else { | ||
parent[erroneousKey] = { [currentKey]: currentValue }; | ||
} | ||
}}); | ||
expect(out).to.eql({ | ||
quux: { x: 42 }, | ||
foo: { bar: { '0': { baz: true }}}, | ||
}) | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10509
281