iking-utils-pro
Version:
金合前端工具库
2 lines (1 loc) • 3.25 kB
JavaScript
;function t(t,e){return Object.prototype.toString.call(t)===`[object ${e}]`}function e(t){return Object.prototype.toString.call(t)}Object.defineProperty(exports,"__esModule",{value:!0});class r{static isJSON(t){try{return!!JSON.parse(t)}catch(t){return!1}}static getTypeLong(t){return e(t)}static getType(t){const r=e(t);return r.substring(8,r.length-1).toLocaleLowerCase()}static isDef(t){return void 0!==t}static isUnDef(t){return!this.isDef(t)}static isObject(e){return null!==e&&t(e,"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(e){return t(e,"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(e){return t(e,"Number")}static isPromise(e){return t(e,"Promise")&&this.isObject(e)&&this.isFunction(e.then)&&this.isFunction(e.catch)}static isString(e){return t(e,"String")}static isFunction(t){return"function"==typeof t}static isBoolean(e){return t(e,"Boolean")}static isRegExp(e){return t(e,"RegExp")}static isArray(t){return t&&Array.isArray(t)}static isWindow(e){return"undefined"!=typeof window&&t(e,"Window")}static isElement(t){return this.isObject(t)&&!!t.tagName}static isMap(e){return t(e,"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 s=t=>{if(!r.isObject(t))throw new Error("param must be an object");return t[Symbol.iterator]=n,t};function n(){let t=-1,e=Object.entries(this),r=e.length;return{next:()=>(++t,r>t?{value:e[t],done:!1}:{value:e[t],done:!0})}}exports.deepMerge=function(t={},e={}){if(!r.isObject(t)||!r.isObject(e))return console.warn("deepMerge: 两个参数都必须是对象"),e;const s=new WeakMap;return function t(e,n){if(s.has(n))return console.warn("deepMerge: 检测到循环引用,跳过合并"),e;s.set(n,!0);const i=Array.isArray(n)?[]:{};for(const t in e)e.hasOwnProperty(t)&&(i[t]=e[t]);for(const s in n)n.hasOwnProperty(s)&&(r.isObject(n[s])&&r.isObject(e[s])?i[s]=t(e[s],n[s]):i[s]=n[s]);return i}(t,e)},exports.forof=t=>s(t),exports.forofObj=s,exports.openWindow=function(t,e){if("string"!=typeof t)return void console.error("openWindow: url必须是字符串");const{target:r="_blank",noopener:s=!0,noreferrer:n=!0}=e||{},i=[];s&&i.push("noopener=yes"),n&&i.push("noreferrer=yes"),window.open(t,r,i.join(","))},exports.setKeyValue=(t,e)=>{if(!r.isObject(t)||!r.isObject(e))throw new Error("参数异常");Object.keys(t).forEach((r=>{Reflect.has(e,r)&&(t[r]=e[r])}))},exports.setObjToUrlParams=function(t,e){if("string"!=typeof t)return console.error("setObjToUrlParams: baseUrl必须是字符串"),"";if(!r.isObject(e))return console.error("setObjToUrlParams: obj必须是对象"),t;let s="";for(const t in e)if(e.hasOwnProperty(t)){const r=null===e[t]||void 0===e[t]?"":e[t];s+=t+"="+encodeURIComponent(r)+"&"}return s=s.replace(/&$/,""),/\?$/.test(t)?t+s:t.replace(/\/?$/,"?")+s};