UNPKG

iking-utils-pro

Version:

金合前端工具库

2 lines (1 loc) 4.1 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const t={id:"id",children:"children",pid:"parentId",sort:"sort",needSort:!1},e=e=>({...t,...e});function r(t,e="",n){Array.isArray(t)?(t=t.sort(((t,e)=>t[n.sort]-e[n.sort])),t?.forEach(((t,i)=>{t.__nodekey=Number(`${e}${i+1}`),t.children&&(t.children=t.children.sort(((t,e)=>t[n.sort]-e[n.sort])),r(t.children,t.__nodekey,n))}))):t.__nodekey=Number(e)}function n(t,e){return Object.prototype.toString.call(t)===`[object ${e}]`}function i(t){return Object.prototype.toString.call(t)}class s{static isJSON(t){try{return!!JSON.parse(t)}catch(t){return!1}}static getTypeLong(t){return i(t)}static getType(t){const e=i(t);return e.substring(8,e.length-1).toLocaleLowerCase()}static isDef(t){return void 0!==t}static isUnDef(t){return!this.isDef(t)}static isObject(t){return null!==t&&n(t,"Object")}static isEmpty(t){return this.isArray(t)||this.isString(t)?0===t.length:t instanceof Map||t instanceof Set?0===t.size:!!this.isObject(t)&&0===Object.keys(t).length}static isDate(t){return n(t,"Date")}static isNull(t){return null===t}static isNullAndUnDef(t){return this.isUnDef(t)&&this.isNull(t)}static isNullOrUnDef(t){return this.isUnDef(t)||this.isNull(t)}static isNumber(t){return n(t,"Number")}static isPromise(t){return n(t,"Promise")&&this.isObject(t)&&this.isFunction(t.then)&&this.isFunction(t.catch)}static isString(t){return n(t,"String")}static isFunction(t){return"function"==typeof t}static isBoolean(t){return n(t,"Boolean")}static isRegExp(t){return n(t,"RegExp")}static isArray(t){return t&&Array.isArray(t)}static isWindow(t){return"undefined"!=typeof window&&n(t,"Window")}static isElement(t){return this.isObject(t)&&!!t.tagName}static isMap(t){return n(t,"Map")}static isServer="undefined"==typeof window;static isClient=!this.isServer;static isUrl(t){return/(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/.test(t)}}const o=(t,e="children",r)=>{const n=s.isString(e)?e:"children",i=s.isFunction(e)?e:r??null;if(i&&i(t),t[n]){(s.isArray(t[n])?t[n]:[t[n]]).forEach((t=>{o(t,r)}))}};function c(t,{children:e="children",conversion:r}){const n=Array.isArray(t[e])&&t[e].length>0,i=r(t)||{};return n?{...i,[e]:t[e].map((t=>c(t,{children:e,conversion:r})))}:{...i}}exports.descendant=function t(e,r={},n=[]){const{childrenField:i="children"}=r;if(!e||!e[i])return n;const s=e[i];if(s&&s.length)for(let e of s)n.push(e),t(e,r,n);return n},exports.filter=function(t,r,n={}){const i=(n=e(n)).children;return function t(e){return e.map((t=>({...t}))).filter((e=>(e[i]=e[i]&&t(e[i]),r(e)||e[i]&&e[i].length)))}(t)},exports.findNode=function(t,r,n={}){n=e(n);const{children:i}=n,s=[...t];for(const t of s){if(r(t))return t;t[i]&&s.push(...t[i])}return null},exports.findNodeAll=function(t,r,n={}){n=e(n);const{children:i}=n,s=[...t],o=[];for(const t of s)r(t)&&o.push(t),t[i]&&s.push(...t[i]);return o},exports.findPath=function(t,r,n={}){n=e(n);const i=[],s=[...t],o=new Set,{children:c}=n;for(;s.length;){const t=s[0];if(o.has(t))i.pop(),s.shift();else if(o.add(t),t[c]&&s.unshift(...t[c]),i.push(t),r(t))return i}return null},exports.findPathAll=function(t,r,n={}){n=e(n);const i=[],s=[...t],o=[],c=new Set,{children:u}=n;for(;s.length;){const t=s[0];c.has(t)?(i.pop(),s.shift()):(c.add(t),t[u]&&s.unshift(...t[u]),i.push(t),r(t)&&o.push([...i]))}return o},exports.forEach=function(t,r,n={}){n=e(n);const i=[...t],{children:s}=n;for(let t=0;t<i.length;t++){if(r(i[t]))return;s&&i[t][s]&&i.splice(t+1,0,...i[t][s])}},exports.listToTree=function(t,n={}){const i=e(n),s=new Map,o=[],{id:c,children:u,pid:l}=i;for(const e of t)s.set(e[c],e);for(const e of t){const t=s.get(e[l]);t&&(t[u]??=[]),(t?t.children:o).push(e)}return i.needSort&&o?.length&&r(o,"",i),o},exports.treeMap=function(t,e){return t.map((t=>c(t,e)))},exports.treeMapEach=c,exports.treeToList=function(t,r={}){r=e(r);const{children:n}=r,i=[...t];for(let t=0;t<i.length;t++)i[t][n]&&i.splice(t+1,0,...i[t][n]);return i},exports.treeTraverse=o;