UNPKG

a-to-treejs

Version:

Convert array to tree fastly, and opposite.

2 lines (1 loc) 7.86 kB
import r from"kind-of";import t from"is-plain-obj";function n(r,t){if(null==r)return{};var n,e,a=function(r,t){if(null==r)return{};var n,e,a={},o=Object.keys(r);for(e=0;o.length>e;e++)0>t.indexOf(n=o[e])&&(a[n]=r[n]);return a}(r,t);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(r);for(e=0;o.length>e;e++)0>t.indexOf(n=o[e])&&Object.prototype.propertyIsEnumerable.call(r,n)&&(a[n]=r[n])}return a}function e(r,t){return function(r){if(Array.isArray(r))return r}(r)||function(r,t){var n=null==r?null:"undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(null==n)return;var e,a,o=[],i=!0,p=!1;try{for(n=n.call(r);!(i=(e=n.next()).done)&&(o.push(e.value),!t||o.length!==t);i=!0);}catch(r){p=!0,a=r}finally{try{i||null==n.return||n.return()}finally{if(p)throw a}}return o}(r,t)||a(r,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function a(r,t){if(r){if("string"==typeof r)return o(r,t);var n=Object.prototype.toString.call(r).slice(8,-1);return"Object"===n&&r.constructor&&(n=r.constructor.name),"Map"===n||"Set"===n?Array.from(r):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?o(r,t):void 0}}function o(r,t){(null==t||t>r.length)&&(t=r.length);for(var n=0,e=Array(t);t>n;n++)e[n]=r[n];return e}function i(r,t){var n="undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(!n){if(Array.isArray(r)||(n=a(r))||t&&r&&"number"==typeof r.length){n&&(r=n);var e=0,o=function(){};return{s:o,n:function(){return r.length>e?{done:!1,value:r[e++]}:{done:!0}},e:function(r){throw r},f:o}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var i,p=!0,c=!1;return{s:function(){n=n.call(r)},n:function(){var r=n.next();return p=r.done,r},e:function(r){c=!0,i=r},f:function(){try{p||null==n.return||n.return()}finally{if(c)throw i}}}}function p(r){return null==r||""===r}function c(r,t){for(var n,e,a,o,i,c,u=[],s=new Map,d=t.id,l=void 0===d?"id":d,f=t.parent_id,v=void 0===f?"parent_id":f,h=t.children_prop,g=void 0===h?"children":h,y=t.parent_prop,b=t.container_prop,m=b?y?function(r){var t={};return t[y]=null,t[g]=[],t[b]=r,t}:function(r){var t={};return t[b]=r,t[g]=[],t}:y?function(r){var t=r;return t[g]=[],t[y]=null,t}:function(r){return r[g]=[],r},_=0;r.length>_;_++){if(n=m(r[_]),a=r[_][v],i=s.get(e=r[_][l]),c=s.get(a),o=!p(a),!c&&o){var w=m({});s.set(a,w),c=w}o&&y&&(n[y]=c);var E=i||n;if(i){var x=i[g];Object.assign(i,n),i[g]=x}else s.set(e,n);o?c[g].push(E):u.push(E)}return s.clear(),u}function u(r,t){for(var n,a,o,c,u,s,d=new Map,l=new Map,f=t.id,v=void 0===f?"id":f,h=t.parent_id,g=void 0===h?"parent_id":h,y=t.parent_prop,b=void 0===y?"parent":y,m=t.children_prop,_=t.container_prop,w=_?m?function(r){var t={};return t[b]=null,t[m]=[],t[_]=r,t}:function(r){var t={};return t[b]=null,t[_]=r,t}:m?function(r){return r[b]=null,r[m]=[],r}:function(r){return r[b]=null,r},E=0;r.length>E;E++){if(n=w(r[E]),a=r[E][v],s=!p(o=r[E][g]),l.set(a,!1),s&&l.set(o,!0),!(u=d.get(o)||null)&&s){var x=w({});d.set(o,x),u=x}if(n[b]=u,c=d.get(a)){var j=m?c[m]:null;Object.assign(c,n),j&&(c[m]=j),m&&u&&u[m].push(c)}else d.set(a,n),m&&u&&u[m].push(n)}var O,S=[],M=i(l.entries());try{for(M.s();!(O=M.n()).done;){var A=e(O.value,2),I=A[0];!A[1]&&I&&S.push(I)}}catch(r){M.e(r)}finally{M.f()}var T=S.map((function(r){return d.get(r)}));return d.clear(),T}var s=["strict"];function d(n){var e=[];t(n)||e.push("Expected 'adapter' as a plain object, instead of ".concat(r(n)," "));var a=n.root,o=n.id,i=n.parent_id,p=n.parent_prop,c=n.children_prop,u=n.container_prop;a&&"branch"!==a&&"leaf"!==a&&e.push("Expected 'adapter.root' must be 'branch' or 'leaf', instead of ".concat(a)),o&&i&&o===i&&e.push("The 'adapter.id' equals with 'adapter.parent_id'.");var s=r(o);o&&"string"!==s&&e.push("Expected 'adapter.id' as a not empty string, instead of ".concat(o,"."));var d=r(i);i&&"string"!==d&&e.push("Expected 'adapter.parent_id' as a not empty string, instead of ".concat(i,"."));var l=r(p);p&&"string"!==l&&e.push("Expected 'adapter.parent_prop' as a not empty string, instead of ".concat(p,"."));var f=r(c);c&&"string"!==f&&e.push("Expected 'adapter.children_prop' as a not empty string, instead of ".concat(c,"."));var v=r(u);return u&&"string"!==v&&e.push("Expected 'adapter.container_prop' as a not empty string, instead of ".concat(u,".")),e}function l(r){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n="duplicated id",a="unknown parent_id",o="self.id equals oneself ancestor.id",c=new Map,u=new Map,s=new Map,d=t.id,l=void 0===d?"id":d,f=t.parent_id,v=void 0===f?"parent_id":f,h=new Map;function g(r,t){h.set(t,!0);var n=r[l];u.has(n)?u.get(n).errors.push(t):u.set(n,{node:r,errors:[t]})}h.set(n,!1),h.set(a,!1),h.set(o,!1);for(var y,b,m,_=0;r.length>_;_++)m=(y=r[_])[v],c.has(b=y[l])?g(y,n):(c.set(b,y),s.set(b,m));var w,E=i(s.entries());try{var x=function(){var r=e(w.value,2),t=r[0],n=r[1];if(!p(n)){c.has(n)||g(c.get(t),a),function r(n){p(n)||(t===n?g(c.get(t),o):r(s.get(n)))}(n)}};for(E.s();!(w=E.n()).done;)x()}catch(r){E.e(r)}finally{E.f()}var j,O=[],S=i(u.entries());try{for(S.s();!(j=S.n()).done;){var M=e(j.value,2),A=M[1];O.push(A)}}catch(r){S.e(r)}finally{S.f()}var I,T=[],k=i(h.entries());try{for(k.s();!(I=k.n()).done;){var q=e(I.value,2),K=q[0],P=q[1];P&&T.push(K)}}catch(r){k.e(r)}finally{k.f()}return c.clear(),u.clear(),s.clear(),h.clear(),{detail:O,errorKinds:T}}function f(r,t){var n=t.parent_prop,e=t.children_prop,a=void 0===e?"children":e,o=t.container_prop,i=[];return function r(t){for(var e,p,c,u=0;t.length>u;u++)e=t[u],p=o?e[o]:e,c=e[a]||[],o||(delete p[a],n&&delete p[n]),i.push(p),r(c)}(r),i}function v(r,t){var n=t.id,e=void 0===n?"id":n,a=t.parent_prop,o=void 0===a?"parent":a,i=t.children_prop,p=t.container_prop,c=[],u=new Map;function s(r){var t=p?r[p]:r,n=t[e];if(!u.has(n)){var a=r[o];p||(delete t[o],i&&delete t[i]),u.set(n,1),c.push(t),a&&s(a)}}for(var d=0;r.length>d;d++)s(r[d]);return u.clear(),c}var h=["root"];function g(){var n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},e=[];t(n)||e.push("Expected 'adapter' as a plain object, instead of ".concat(r(n)," "));var a=n.id,o=n.root,i=void 0===o?"branch":o,p=n.children_prop,c=n.parent_prop,u=n.container_prop;i&&"branch"!==i&&"leaf"!==i&&e.push("Expected 'adapter.root' must be 'branch' or 'leaf', instead of ".concat(i));var s=r(a);a&&"string"===s||e.push("Expected 'adapter.id' as a not empty string, instead of ".concat(a,"."));var d=r(c);c&&"string"!==d&&e.push("Expected 'adapter.parent_prop' as a not empty string, instead of ".concat(c,"."));var l=r(p);p&&"string"!==l&&e.push("Expected 'adapter.children_prop' as a not empty string, instead of ".concat(p,"."));var f=r(u);return u&&"string"!==f&&e.push("Expected 'adapter.container_prop' as a not empty string, instead of ".concat(u,".")),e}var y=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if("array"!==r(t))return[];var a=g(e);if(a.length>0){var o="The 'adapter' is invalid, causes: \n ".concat(a.join("\n"));throw Error(o)}var i=e.root,p=void 0===i?"branch":i,c=n(e,h);return"branch"===p?f(t,c):v(t,c)},b=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if("array"!==r(t))return[];var a=d(e);if(a.length>0){var o="The 'adapter' is invalid, causes: \n ".concat(a.join("\n"));throw Error(o)}var i=e.strict,p=void 0!==i&&i,f=n(e,s);if(p){var v=l(t,{id:e.id,parent_id:e.parent_id}),h=v.detail,g=v.errorKinds;if(h.length>0)throw console.log("Inregular data causes: \n",h),Error("The data isn't regular, causes: "+g.join(", "))}var y=e.root,b=void 0===y?"branch":y;return"branch"===b?c(t,f):u(t,f)};export{b as default,y as treeToArray};