@cdxoo/flat
Advanced tools
Comparing version 0.0.7 to 0.0.8
{ | ||
"name": "@cdxoo/flat", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"description": "does not flatten cats", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -32,22 +32,38 @@ 'use strict'; | ||
var getValue = (that, path = []) => { | ||
//console.log('getValue()') | ||
//console.log('=>', that, path); | ||
var out = that; | ||
for (var token of path) { | ||
out = out[token]; | ||
} | ||
return out; | ||
} | ||
var unflatten = (that, options = {}) => { | ||
var { | ||
delimiter = '.', | ||
handlePropertiesOnNonObjects = 'throw' | ||
handlePropertiesOnNonObjects = 'throw', | ||
extraHandling, | ||
} = options; | ||
var out = {}; | ||
var parent = undefined; | ||
var parentToken = undefined; | ||
for (var key of Object.keys(that)) { | ||
var path = key.split(delimiter); | ||
var current = out; | ||
for (var i = 0; i < path.length; i += 1) { | ||
var token = path[i]; | ||
var route = [ { token: '#ROOT#', getValue: () => out }]; | ||
// NOTE: theese "let" initializaions are important with for | ||
path.forEach((token, i) => { | ||
//for (let i = 0; i < path.length; i += 1) { | ||
//let token = path[i]; | ||
//let currentPath = [ ...path.slice(0,i), token ]; | ||
var currentPath = [ ...path.slice(0,i), token ]; | ||
var isLast = i === path.length - 1; | ||
if (current === null || current === undefined) { | ||
var [ container, parent ] = route; | ||
var precheckValue = container.getValue(); | ||
if (precheckValue === null || precheckValue === undefined) { | ||
throw new Error(inlineErrorMsg(` | ||
Cannot create property '${token}' | ||
on ${typeof current} '${current}' | ||
on ${typeof precheckValue} '${precheckValue}' | ||
(path: '${path.join('.')}') | ||
@@ -57,15 +73,30 @@ `)); | ||
if (!Object.keys(current).includes(token)) { | ||
var value = isLast ? that[key] : {}; | ||
var value = ( | ||
isLast ? that[key] : container.getValue()[token] || {} | ||
); | ||
if (typeof current !== 'object') { | ||
if (extraHandling) { | ||
({ token, value } = extraHandling({ | ||
route, | ||
token, | ||
value | ||
})); | ||
} | ||
//let containerValue = container.getValue(); | ||
var containerValue = container.getValue(); | ||
if (!Object.keys(containerValue).includes(token)) { | ||
// NOTE: arrays will be detected as object as well | ||
if (typeof containerValue !== 'object') { | ||
if (handlePropertiesOnNonObjects !== 'throw') { | ||
handlePropertiesOnNonObjects({ | ||
route, | ||
root: out, | ||
parent, | ||
parent: parent.getValue(), | ||
erroneousPath: path.slice(0, i), | ||
erroneousKey: parentToken, | ||
erroneousValue: current, | ||
erroneousKey: container.token, | ||
erroneousValue: containerValue, | ||
currentKey: token, | ||
currentValue: value | ||
currentValue: value, | ||
}) | ||
@@ -76,3 +107,3 @@ } | ||
Cannot create property '${token}' | ||
on ${typeof current} '${current}' | ||
on ${typeof containerValue} '${containerValue}' | ||
(path: '${path.join('.')}' | ||
@@ -84,9 +115,15 @@ in ${JSON.stringify(out)}) | ||
else { | ||
current[token] = value; | ||
containerValue[token] = value; | ||
} | ||
} | ||
parent = current; | ||
parentToken = token; | ||
current = current[token]; | ||
} | ||
route.unshift({ | ||
path: currentPath, | ||
token, | ||
getValue: () => { | ||
//console.log({ i }) | ||
return getValue(out, currentPath) | ||
} | ||
}); | ||
}) | ||
} | ||
@@ -93,0 +130,0 @@ return out; |
@@ -16,2 +16,6 @@ 'use strict'; | ||
[ | ||
{ bar: 42, baz: 9001 }, | ||
{ 'bar': 42, 'baz': 9001 } | ||
], | ||
[ | ||
{ foo: { bar: 42 }}, | ||
@@ -164,2 +168,3 @@ { 'foo.bar': 42 } | ||
var { | ||
route, | ||
parent, | ||
@@ -169,5 +174,5 @@ erroneousKey, | ||
currentKey, | ||
currentValue | ||
currentValue, | ||
} = bag; | ||
parent[erroneousKey] = { [currentKey]: currentValue }; | ||
@@ -219,1 +224,27 @@ }}); | ||
}) | ||
describe('unflatten array keys', () => { | ||
it('can unflatten to array via custom extra handling', () => { | ||
var out = unflatten({ | ||
'foo.[0]': 'a', | ||
'foo.[1]': 'b', | ||
'foo.[2]': 'c', | ||
}, { | ||
traverseArrays: true, | ||
extraHandling: (bag) => { | ||
var { route, token, value } = bag; | ||
var [ container, parent ] = route | ||
if ( | ||
!Array.isArray(container.getValue()) | ||
&& /\[\d+\]/.test(token) | ||
) { | ||
parent.getValue()[container.token] = [] | ||
} | ||
return { token: token.replace(/[\[\]]/g, ''), value }; | ||
} | ||
}); | ||
expect(out).to.eql({ | ||
foo: [ 'a', 'b', 'c' ] | ||
}); | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
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
13024
343