object-treeify
Advanced tools
Comparing version 3.2.1 to 3.3.0
@@ -10,3 +10,4 @@ import assert from './assert.js'; | ||
keyNeighbour: '├─ ', | ||
renderFn: (node) => (['boolean', 'string', 'number'].includes(typeof node) ? `: ${node}` : ''), | ||
separator: ': ', | ||
renderFn: (node) => (['boolean', 'string', 'number'].includes(typeof node) ? node : undefined), | ||
sortFn: null, | ||
@@ -16,3 +17,3 @@ breakCircularWith: ' (circular ref.)', | ||
}; | ||
assert(Object.keys(ctx).length === 8, 'Unexpected Option(s) provided'); | ||
assert(Object.keys(ctx).length === 9, 'Unexpected Option(s) provided'); | ||
assert(typeof ctx.joined === 'boolean', 'Option "joined" has invalid format'); | ||
@@ -23,2 +24,3 @@ assert(typeof ctx.spacerNoNeighbour === 'string', 'Option "spacerNoNeighbour" has invalid format'); | ||
assert(typeof ctx.keyNeighbour === 'string', 'Option "keyNeighbour" has invalid format'); | ||
assert(typeof ctx.separator === 'string', 'Option "separator" has invalid format'); | ||
assert(typeof ctx.renderFn === 'function', 'Option "renderFn" has invalid format'); | ||
@@ -37,2 +39,7 @@ assert(typeof ctx.sortFn === 'function' || ctx.sortFn === null, 'Option "sortFn" has invalid format'); | ||
const rootRendered = ctx.renderFn(tree); | ||
if (rootRendered !== undefined) { | ||
result.push(String(rootRendered)); | ||
} | ||
const sort = (input) => (ctx.sortFn === null ? input.reverse() : input.sort((a, b) => ctx.sortFn(b, a))); | ||
@@ -49,2 +56,3 @@ | ||
neighbours[key.length - 1] = keys.length !== 0 && keys[keys.length - 1].length === key.length; | ||
const nodeRendered = ctx.renderFn(node); | ||
result.push([ | ||
@@ -54,3 +62,3 @@ neighbours.slice(0, key.length - 1).map((n) => (n ? ctx.spacerNeighbour : ctx.spacerNoNeighbour)).join(''), | ||
key[key.length - 1], | ||
ctx.renderFn(node), | ||
nodeRendered === undefined ? '' : `${ctx.separator}${node}`, | ||
isCircular ? ctx.breakCircularWith : '' | ||
@@ -57,0 +65,0 @@ ].join('')); |
@@ -1,1 +0,1 @@ | ||
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:"├─ ",renderFn:e=>["boolean","string","number"].includes(typeof e)?`: ${e}`:"",sortFn:null,breakCircularWith:" (circular ref.)",...e};assert(Object.keys(r).length===8,"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.renderFn==="function",'Option "renderFn" 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],t.renderFn(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}; | ||
var e={};(()=>{e.d=(r,n)=>{for(var t in n){if(e.o(n,t)&&!e.o(r,t)){Object.defineProperty(r,t,{enumerable:true,get:n[t]})}}}})();(()=>{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:"├─ ",separator:": ",renderFn:e=>["boolean","string","number"].includes(typeof e)?e:undefined,sortFn:null,breakCircularWith:" (circular ref.)",...e};assert(Object.keys(r).length===9,"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.separator==="string",'Option "separator" has invalid format');assert(typeof r.renderFn==="function",'Option "renderFn" 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 n=buildCtx(r);const t=[];const o=n.renderFn(e);if(o!==undefined){t.push(String(o))}const sort=e=>n.sortFn===null?e.reverse():e.sort(((e,r)=>n.sortFn(r,e)));const i=[];const a=sort(Object.keys(e)).map((e=>[e]));const s=[e];while(a.length!==0){const e=a.pop();const r=s[e.length-1][e[e.length-1]];const o=n.breakCircularWith!==null&&s.includes(r);i[e.length-1]=a.length!==0&&a[a.length-1].length===e.length;const l=n.renderFn(r);t.push([i.slice(0,e.length-1).map((e=>e?n.spacerNeighbour:n.spacerNoNeighbour)).join(""),i[e.length-1]?n.keyNeighbour:n.keyNoNeighbour,e[e.length-1],l===undefined?"":`${n.separator}${r}`,o?n.breakCircularWith:""].join(""));if(r instanceof Object&&!Array.isArray(r)&&!o){a.push(...sort(Object.keys(r)).map((r=>e.concat(r))));s[e.length]=r}}return n.joined===true?t.join("\n"):t};var n=r.Z;export{n as default}; |
{ | ||
"name": "object-treeify", | ||
"type": "module", | ||
"version": "3.2.1", | ||
"version": "3.3.0", | ||
"description": "Stringify Object as tree structure", | ||
@@ -6,0 +6,0 @@ "main": "lib/index.js", |
@@ -87,3 +87,3 @@ # object-treeify | ||
Type: `string`<br> | ||
Default: ` ` | ||
Default: ` ` | ||
@@ -95,3 +95,3 @@ Prefix for depth level when no further neighbour is present. | ||
Type: `string`<br> | ||
Default: `│ ` | ||
Default: `│ ` | ||
@@ -103,3 +103,3 @@ Prefix for depth level when a further neighbour is present. | ||
Type: `string`<br> | ||
Default: `└─ ` | ||
Default: `└─ ` | ||
@@ -111,12 +111,19 @@ Prefix for key when no further neighbour is present. | ||
Type: `string`<br> | ||
Default: `├─ ` | ||
Default: `├─ ` | ||
Prefix for key when a further neighbour is present. | ||
#### separator | ||
Type: `string`<br> | ||
Default: `: ` | ||
Used to separate node key from node value. | ||
#### renderFn | ||
Type: `function`<br> | ||
Default: ``(node) => (['boolean', 'string', 'number'].includes(typeof node) ? `: ${node}` : '')`` | ||
Default: `(node) => (['boolean', 'string', 'number'].includes(typeof node) ? node : undefined)` | ||
Can be used to overwrite the node rendering logic. | ||
Can be used to overwrite the node rendering logic. Node is rendered if result is not equal `undefined`. | ||
@@ -123,0 +130,0 @@ #### sortFn |
13211
65
143