object-treeify
Version:
Stringify Object as tree structure
1 lines • 2.41 kB
JavaScript
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,{A:()=>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.A;export{n as default};