dt-shape
Version:
Build data by using shapes
2 lines (1 loc) • 1.27 kB
JavaScript
;var t=require("dt-toolbox");function e(t,e){const o=Object.entries(e),l=e instanceof Array;return t.query((e=>{const n=["root"],r={};l?e.set("root",[]):e.set("root",{}),o.forEach((([o,l])=>{let n,[s,u]=function(t){let e=null,o=null;return t.includes("!")?[o,e]=t.split("!"):e=t,[e.split("/"),o]}(o);"list"===u&&(n=[]),"fold"===u&&(n={}),"load"===u&&(n=function(t){let e;return t.forEach((t=>{let o=typeof t;"function"===o&&(o=typeof(t=t())),e=null!=o?t:null})),e}(l)),"load"!==u&&l.forEach((o=>{let l=null,r=null!=t.index(`root/${o}`);if(r)e.get(`root/${o}`).look((({key:t,value:e,flatData:o})=>{if("list"===u)n.push(e);else{if("fold"!==u)return n=o,"next";n[t]=e}}));else{const s=o.split("/"),c=s.pop();s.length>0&&(l=`root/${s.join("/")}`,r=null!=t.index(l)),e.look((({key:t,value:e,breadcrumbs:o})=>{if(r&&o!=l)return"next";t===c&&("list"===u?n.push(e):"fold"===u?n[t]=e:n=e)}))}})),"object"==typeof n&&0===Object.entries(n).length||null!=n&&(r[s.join("/")]=n)}));Object.entries(r).forEach((([t,o])=>{const l=t.split("/"),r=l.pop();let s="root";l.forEach((t=>{n.includes(t)||(e.set(t,{}),e.connect([`${s}/${t}`]),n.push(t)),s=t})),null!=o&&("object"==typeof o?(e.set(r,o),e.connect([`${s}/${r}`])):e.save(s,r,o))}))}))}e.getDTtoolbox=()=>t,module.exports=e;