object-treeify
Advanced tools
Comparing version 2.0.1 to 2.1.1
@@ -14,5 +14,6 @@ "use strict"; | ||
sortFn: null, | ||
breakCircularWith: ' (circular ref.)', | ||
...opts | ||
}; | ||
assert(Object.keys(ctx).length === 6, 'Unexpected Option(s) provided'); | ||
assert(Object.keys(ctx).length === 7, 'Unexpected Option(s) provided'); | ||
assert(typeof ctx.joined === 'boolean', 'Option "joined" has invalid format'); | ||
@@ -24,2 +25,3 @@ assert(typeof ctx.spacerNoNeighbour === 'string', 'Option "spacerNoNeighbour" has invalid format'); | ||
assert(typeof ctx.sortFn === 'function' || ctx.sortFn === null, 'Option "sortFn" has invalid format'); | ||
assert(typeof ctx.breakCircularWith === 'string' || ctx.breakCircularWith === null, 'Option "breakCircularWith" has invalid format'); | ||
return ctx; | ||
@@ -41,6 +43,7 @@ }; | ||
const node = lookup[key.length - 1][key[key.length - 1]]; | ||
const isCircular = ctx.breakCircularWith !== null && lookup.includes(node); | ||
neighbours[key.length - 1] = keys.length !== 0 && keys[keys.length - 1].length === key.length; | ||
result.push([neighbours.slice(0, key.length - 1).map(n => n ? ctx.spacerNeighbour : ctx.spacerNoNeighbour).join(''), neighbours[key.length - 1] ? ctx.keyNeighbour : ctx.keyNoNeighbour, key[key.length - 1], ['boolean', 'string', 'number'].includes(typeof node) ? `: ${node}` : ''].join('')); | ||
result.push([neighbours.slice(0, key.length - 1).map(n => n ? ctx.spacerNeighbour : ctx.spacerNoNeighbour).join(''), neighbours[key.length - 1] ? ctx.keyNeighbour : ctx.keyNoNeighbour, key[key.length - 1], ['boolean', 'string', 'number'].includes(typeof node) ? `: ${node}` : '', isCircular ? ctx.breakCircularWith : ''].join('')); | ||
if (node instanceof Object && !Array.isArray(node)) { | ||
if (node instanceof Object && !Array.isArray(node) && !isCircular) { | ||
keys.push(...sort(Object.keys(node)).map(k => key.concat(k))); | ||
@@ -47,0 +50,0 @@ lookup[key.length] = node; |
{ | ||
"name": "object-treeify", | ||
"version": "2.0.1", | ||
"version": "2.1.1", | ||
"description": "Stringify Object as tree structure", | ||
@@ -64,3 +64,4 @@ "main": "lib/index.js", | ||
"nyc": "15.1.0", | ||
"semantic-release": "17.4.7" | ||
"semantic-release": "17.4.7", | ||
"treeify": "1.1.0" | ||
}, | ||
@@ -67,0 +68,0 @@ "nyc": { |
@@ -73,2 +73,3 @@ # object-treeify | ||
- Tests to verify correctness | ||
- Handles circular references | ||
@@ -119,4 +120,11 @@ ## Options | ||
#### breakCircularWith | ||
Type: `string` or `null`<br> | ||
Default: ` (circular ref.)` | ||
When `string`, circular references are broken with that string, at a minor performance cost. | ||
## Examples | ||
More examples can be found in the tests. |
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
10215
44
129
19