object-treeify
Advanced tools
Comparing version 3.0.0 to 3.1.0
@@ -11,5 +11,6 @@ import assert from './assert.js'; | ||
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'); | ||
@@ -21,2 +22,6 @@ 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; | ||
@@ -37,2 +42,3 @@ }; | ||
const node = lookup[key.length - 1][key[key.length - 1]]; | ||
const isCircular = ctx.breakCircularWith !== null && lookup.includes(node); | ||
@@ -44,6 +50,7 @@ neighbours[key.length - 1] = keys.length !== 0 && keys[keys.length - 1].length === key.length; | ||
key[key.length - 1], | ||
['boolean', 'string', 'number'].includes(typeof node) ? `: ${node}` : '' | ||
['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))); | ||
@@ -50,0 +57,0 @@ lookup[key.length] = node; |
@@ -1,1 +0,1 @@ | ||
var e={};(()=>{e.d=(o,t)=>{for(var n in t){if(e.o(t,n)&&!e.o(o,n)){Object.defineProperty(o,n,{enumerable:true,get:t[n]})}}}})();(()=>{e.o=(e,o)=>Object.prototype.hasOwnProperty.call(e,o)})();if(typeof e!=="undefined")e.ab=new URL(".",import.meta.url).pathname.slice(import.meta.url.match(/^file:\/\/\/\w:/)?1:0,-1)+"/";var o={};e.d(o,{Z:()=>lib});const assert=(e,o)=>{if(!e){throw new Error(o)}};const buildCtx=e=>{const o={joined:true,spacerNoNeighbour:" ",spacerNeighbour:"│ ",keyNoNeighbour:"└─ ",keyNeighbour:"├─ ",sortFn:null,...e};assert(Object.keys(o).length===6,"Unexpected Option(s) provided");assert(typeof o.joined==="boolean",'Option "joined" has invalid format');assert(typeof o.spacerNoNeighbour==="string",'Option "spacerNoNeighbour" has invalid format');assert(typeof o.spacerNeighbour==="string",'Option "spacerNeighbour" has invalid format');assert(typeof o.keyNoNeighbour==="string",'Option "keyNoNeighbour" has invalid format');assert(typeof o.keyNeighbour==="string",'Option "keyNeighbour" has invalid format');assert(typeof o.sortFn==="function"||o.sortFn===null,'Option "sortFn" has invalid format');return o};const lib=(e,o={})=>{const t=buildCtx(o);const n=[];const sort=e=>t.sortFn===null?e.reverse():e.sort(((e,o)=>t.sortFn(o,e)));const r=[];const i=sort(Object.keys(e)).map((e=>[e]));const s=[e];while(i.length!==0){const e=i.pop();const o=s[e.length-1][e[e.length-1]];r[e.length-1]=i.length!==0&&i[i.length-1].length===e.length;n.push([r.slice(0,e.length-1).map((e=>e?t.spacerNeighbour:t.spacerNoNeighbour)).join(""),r[e.length-1]?t.keyNeighbour:t.keyNoNeighbour,e[e.length-1],["boolean","string","number"].includes(typeof o)?`: ${o}`:""].join(""));if(o instanceof Object&&!Array.isArray(o)){i.push(...sort(Object.keys(o)).map((o=>e.concat(o))));s[e.length]=o}}return t.joined===true?n.join("\n"):n};var t=o.Z;export{t as default}; | ||
var e={};(()=>{e.d=(r,t)=>{for(var o in t){if(e.o(t,o)&&!e.o(r,o)){Object.defineProperty(r,o,{enumerable:true,get:t[o]})}}}})();(()=>{e.o=(e,r)=>Object.prototype.hasOwnProperty.call(e,r)})();if(typeof e!=="undefined")e.ab=new URL(".",import.meta.url).pathname.slice(import.meta.url.match(/^file:\/\/\/\w:/)?1:0,-1)+"/";var r={};e.d(r,{Z:()=>lib});const assert=(e,r)=>{if(!e){throw new Error(r)}};const buildCtx=e=>{const r={joined:true,spacerNoNeighbour:" ",spacerNeighbour:"│ ",keyNoNeighbour:"└─ ",keyNeighbour:"├─ ",sortFn:null,breakCircularWith:" (circular ref.)",...e};assert(Object.keys(r).length===7,"Unexpected Option(s) provided");assert(typeof r.joined==="boolean",'Option "joined" has invalid format');assert(typeof r.spacerNoNeighbour==="string",'Option "spacerNoNeighbour" has invalid format');assert(typeof r.spacerNeighbour==="string",'Option "spacerNeighbour" has invalid format');assert(typeof r.keyNoNeighbour==="string",'Option "keyNoNeighbour" has invalid format');assert(typeof r.keyNeighbour==="string",'Option "keyNeighbour" has invalid format');assert(typeof r.sortFn==="function"||r.sortFn===null,'Option "sortFn" has invalid format');assert(typeof r.breakCircularWith==="string"||r.breakCircularWith===null,'Option "breakCircularWith" has invalid format');return r};const lib=(e,r={})=>{const t=buildCtx(r);const o=[];const sort=e=>t.sortFn===null?e.reverse():e.sort(((e,r)=>t.sortFn(r,e)));const n=[];const i=sort(Object.keys(e)).map((e=>[e]));const a=[e];while(i.length!==0){const e=i.pop();const r=a[e.length-1][e[e.length-1]];const s=t.breakCircularWith!==null&&a.includes(r);n[e.length-1]=i.length!==0&&i[i.length-1].length===e.length;o.push([n.slice(0,e.length-1).map((e=>e?t.spacerNeighbour:t.spacerNoNeighbour)).join(""),n[e.length-1]?t.keyNeighbour:t.keyNoNeighbour,e[e.length-1],["boolean","string","number"].includes(typeof r)?`: ${r}`:"",s?t.breakCircularWith:""].join(""));if(r instanceof Object&&!Array.isArray(r)&&!s){i.push(...sort(Object.keys(r)).map((r=>e.concat(r))));a[e.length]=r}}return t.joined===true?o.join("\n"):o};var t=r.Z;export{t as default}; |
{ | ||
"name": "object-treeify", | ||
"type": "module", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "Stringify Object as tree structure", | ||
@@ -49,11 +49,12 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"@babel/core": "7.18.5", | ||
"@babel/core": "7.18.6", | ||
"@babel/eslint-parser": "7.18.2", | ||
"@babel/register": "7.17.7", | ||
"@babel/register": "7.18.6", | ||
"@blackflux/eslint-plugin-rules": "2.1.0", | ||
"@blackflux/robo-config-plugin": "7.8.7", | ||
"@blackflux/robo-config-plugin": "7.9.4", | ||
"@vercel/ncc": "0.34.0", | ||
"c8": "7.11.3", | ||
"chai": "4.3.6", | ||
"coveralls": "3.1.1", | ||
"eslint": "8.17.0", | ||
"eslint": "8.19.0", | ||
"eslint-config-airbnb-base": "15.0.0", | ||
@@ -66,3 +67,3 @@ "eslint-plugin-import": "2.26.0", | ||
"smart-fs": "3.0.1", | ||
"c8": "7.11.0" | ||
"treeify": "1.1.0" | ||
}, | ||
@@ -69,0 +70,0 @@ "licenses": [ |
@@ -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. |
SPDX disjunction
LicenseSPDX disjunction for an artifact's license information
Found 1 instance in 1 package
SPDX disjunction
LicenseSPDX disjunction for an artifact's license information
Found 1 instance in 1 package
12117
56
129
18