UNPKG

iking-utils-pro

Version:

金合前端工具库

2 lines (1 loc) 3.14 kB
function t(t,r){return Object.prototype.toString.call(t)===`[object ${r}]`}function r(t){return Object.prototype.toString.call(t)}class e{static isJSON(t){try{return!!JSON.parse(t)}catch(t){return!1}}static getTypeLong(t){return r(t)}static getType(t){const e=r(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(r){return null!==r&&t(r,"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(r){return t(r,"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(r){return t(r,"Number")}static isPromise(r){return t(r,"Promise")&&this.isObject(r)&&this.isFunction(r.then)&&this.isFunction(r.catch)}static isString(r){return t(r,"String")}static isFunction(t){return"function"==typeof t}static isBoolean(r){return t(r,"Boolean")}static isRegExp(r){return t(r,"RegExp")}static isArray(t){return t&&Array.isArray(t)}static isWindow(r){return"undefined"!=typeof window&&t(r,"Window")}static isElement(t){return this.isObject(t)&&!!t.tagName}static isMap(r){return t(r,"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)}}var i={flatArr:(t,r=0)=>{if(!Array.isArray(t))throw new Error("第一个参数必须是Array类型");if(!e.isNumber(r)||r<0)throw new Error("第二个参数必须是非负数");if(0===t.length)return[];const i=0===r?1/0:r;try{return t.flat(i)}catch(s){console.error("数组扁平化失败:",s);const n=[];function a(t,r){for(const e of t)Array.isArray(e)&&r<i?a(e,r+1):n.push(e)}return a(t,0),n}},realArr:t=>{if(!Array.isArray(t))throw new Error("参数不是Array类型");return t.filter((t=>!(!t&&0!==t)&&(!(e.isArray(t)&&!t.length)&&!(e.isObject(t)&&!Object.keys(t).length))))},proxyArr:t=>{if(!e.isArray(t))throw new TypeError("参数不是Array类型");return new Proxy(t,{get:(t,r)=>{const e=Number(r);return t[e<0?t.length+e:e]}})},proxyArrAll:t=>{if(!e.isArray(t))throw new TypeError("参数不是Array类型");return new Proxy(t,{get:(t,r)=>{const e=Number(r);return t[e<0?t.length+e:e]},set:(t,r,e)=>{const i=Number(r);return t[i<0?t.length+i:i]=e}})},listToString:(t=[],r="name",i=", ",s="--")=>e.isArray(t)?"string"!=typeof r||"string"!=typeof i?(console.warn("key and split must be strings"),t):t?.length?t.map((t=>t[r])).join(i):s:(console.warn("list must be array"),s),listGroupByKey:(t,r,e=!1)=>{if(!Array.isArray(t)||"string"!=typeof r)throw new Error("Invalid input");const i={};for(const e of t){const t=e[r];i[t]||(i[t]=[]),i[t].push(e)}let s=[];return Object.keys(i).forEach((t=>{s=[...s,...i[t]]})),e?s:i},dedupe:(t,r)=>{const e=new Set;return t.filter((t=>{const i=t[r];return!e.has(i)&&e.add(i)}))}};export{i as default};