UNPKG

iking-utils-pro

Version:

金合前端工具库

2 lines (1 loc) 3.15 kB
"use strict";var t=require("cookiejs"),e=require("localforage");function r(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var s=r(t),i=r(e);function n(t,e){return Object.prototype.toString.call(t)===`[object ${e}]`}function o(t){return Object.prototype.toString.call(t)}class a{static isJSON(t){try{return!!JSON.parse(t)}catch(t){return!1}}static getTypeLong(t){return o(t)}static getType(t){const e=o(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)}}var c={local:{has:t=>Object.prototype.hasOwnProperty.call(localStorage,t),setItem:(t,e,r=0)=>{const s=(new Date).getTime(),i=a.isDate(r)?s+1e3*r:0;localStorage.setItem(t,JSON.stringify({value:e,overTime:i}))},getItem:t=>{const e=localStorage.getItem(t);if(!e)return null;const r=JSON.parse(e);return(new Date).getTime()>r.overTime&&0!==r.overTime?(localStorage.removeItem(t),null):r.value},removeItem:t=>{t&&localStorage.removeItem(t)},clear:t=>{t?localStorage.removeItem(t):localStorage.clear()},size:()=>{const t=Object.keys(localStorage);let e=0;for(let r=0;r<t.length;r++){const s=t[r],i=localStorage.getItem(s)||"";e+=2*(s.length+i.length)}return Math.round(e/1024)}},session:{has:t=>Object.prototype.hasOwnProperty.call(sessionStorage,t),setItem:(t,e,r=0)=>{const s=(new Date).getTime(),i=r?s+1e3*r:0;sessionStorage.setItem(t,JSON.stringify({value:e,overTime:i}))},getItem:t=>{const e=sessionStorage.getItem(t);if(!e)return null;const r=JSON.parse(e);return(new Date).getTime()>r.overTime&&0!==r.overTime?(sessionStorage.removeItem(t),null):r.value},removeItem:t=>{t&&sessionStorage.removeItem(t)},clear:t=>{t?sessionStorage.removeItem(t):sessionStorage.clear()},size:()=>{const t=Object.keys(sessionStorage);let e=0;for(let r=0;r<t.length;r++){const s=t[r],i=sessionStorage.getItem(s)||"";e+=2*(s.length+i.length)}return Math.round(e/1024)}},forage:i.default,cookie:s.default};module.exports=c;