iking-utils-pro
Version:
金合前端工具库
2 lines (1 loc) • 3.15 kB
JavaScript
;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 s={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 s=0===r?1/0:r;try{return t.flat(s)}catch(i){console.error("数组扁平化失败:",i);const n=[];function o(t,r){for(const e of t)Array.isArray(e)&&r<s?o(e,r+1):n.push(e)}return o(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 s=Number(r);return t[s<0?t.length+s:s]=e}})},listToString:(t=[],r="name",s=", ",i="--")=>e.isArray(t)?"string"!=typeof r||"string"!=typeof s?(console.warn("key and split must be strings"),t):t?.length?t.map((t=>t[r])).join(s):i:(console.warn("list must be array"),i),listGroupByKey:(t,r,e=!1)=>{if(!Array.isArray(t)||"string"!=typeof r)throw new Error("Invalid input");const s={};for(const e of t){const t=e[r];s[t]||(s[t]=[]),s[t].push(e)}let i=[];return Object.keys(s).forEach((t=>{i=[...i,...s[t]]})),e?i:s},dedupe:(t,r)=>{const e=new Set;return t.filter((t=>{const s=t[r];return!e.has(s)&&e.add(s)}))}};module.exports=s;