@fast-china/utils 1.0.3 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.global.js +197 -142
- package/dist/index.global.js.map +1 -1
- package/dist/index.global.min.js +5 -5
- package/dist/index.global.min.js.map +1 -1
- package/es/array/index.d.ts +19 -0
- package/es/array/index.mjs +33 -0
- package/es/array/index.mjs.map +1 -0
- package/es/base64/index.d.ts +9 -0
- package/es/base64/index.mjs +6 -0
- package/es/base64/index.mjs.map +1 -1
- package/es/click/index.d.ts +3 -0
- package/es/click/index.mjs.map +1 -1
- package/es/color/index.d.ts +3 -0
- package/es/color/index.mjs +5 -4
- package/es/color/index.mjs.map +1 -1
- package/es/date/index.d.ts +17 -0
- package/es/date/index.mjs +17 -0
- package/es/date/index.mjs.map +1 -1
- package/es/env/index.d.ts +25 -0
- package/es/env/index.mjs +38 -0
- package/es/env/index.mjs.map +1 -0
- package/es/identity/index.d.ts +8 -0
- package/es/identity/index.mjs +33 -0
- package/es/identity/index.mjs.map +1 -0
- package/es/index.d.ts +3 -0
- package/es/index.mjs +6 -0
- package/es/index.mjs.map +1 -1
- package/es/object/index.d.ts +0 -12
- package/es/object/index.mjs +0 -97
- package/es/object/index.mjs.map +1 -1
- package/es/storage/index.mjs +3 -2
- package/es/storage/index.mjs.map +1 -1
- package/es/string/index.d.ts +21 -0
- package/es/string/index.mjs +37 -0
- package/es/string/index.mjs.map +1 -1
- package/es/vue/useRender.mjs +2 -1
- package/es/vue/useRender.mjs.map +1 -1
- package/lib/array/index.d.ts +19 -0
- package/lib/array/index.js +2 -0
- package/lib/array/index.js.map +1 -0
- package/lib/base64/index.d.ts +9 -0
- package/lib/base64/index.js.map +1 -1
- package/lib/click/index.d.ts +3 -0
- package/lib/click/index.js.map +1 -1
- package/lib/color/index.d.ts +3 -0
- package/lib/color/index.js +1 -1
- package/lib/color/index.js.map +1 -1
- package/lib/date/index.d.ts +17 -0
- package/lib/date/index.js.map +1 -1
- package/lib/env/index.d.ts +25 -0
- package/lib/env/index.js +2 -0
- package/lib/env/index.js.map +1 -0
- package/lib/identity/index.d.ts +8 -0
- package/lib/identity/index.js +2 -0
- package/lib/identity/index.js.map +1 -0
- package/lib/index.d.ts +3 -0
- package/lib/index.js +1 -1
- package/lib/object/index.d.ts +0 -12
- package/lib/object/index.js +1 -1
- package/lib/object/index.js.map +1 -1
- package/lib/storage/index.js +1 -1
- package/lib/storage/index.js.map +1 -1
- package/lib/string/index.d.ts +21 -0
- package/lib/string/index.js +1 -1
- package/lib/string/index.js.map +1 -1
- package/lib/vue/useRender.js +1 -1
- package/lib/vue/useRender.js.map +1 -1
- package/package.json +8 -8
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 环境工具类
|
|
3
|
+
*/
|
|
4
|
+
export declare const envUtil: {
|
|
5
|
+
/**
|
|
6
|
+
* 是否为 Uni 环境
|
|
7
|
+
* @returns
|
|
8
|
+
*/
|
|
9
|
+
isUni(): boolean;
|
|
10
|
+
/**
|
|
11
|
+
* 是否为 Web 环境(浏览器环境)
|
|
12
|
+
* @returns
|
|
13
|
+
*/
|
|
14
|
+
isWeb(): boolean;
|
|
15
|
+
/**
|
|
16
|
+
* 是否为手机设备
|
|
17
|
+
* @returns
|
|
18
|
+
*/
|
|
19
|
+
isMobile(): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* 是否为平板设备
|
|
22
|
+
* @returns
|
|
23
|
+
*/
|
|
24
|
+
isIpad(): boolean;
|
|
25
|
+
};
|
package/lib/env/index.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e={isUni:()=>"undefined"!=typeof uni,isWeb:()=>"undefined"!=typeof window&&void 0!==window.document,isMobile(){if(!this.isWeb())return!1;const e=navigator.userAgent||"";return/Mobile|iPhone|Android.*Mobile|Windows Phone/i.test(e)},isIpad(){if(!this.isWeb())return!1;const e=navigator.userAgent||"";return/iPad|Android(?!.*Mobile)|Tablet/i.test(e)}};exports.envUtil=e;
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../../../src/env/index.ts"],"sourcesContent":["/**\n * 环境工具类\n */\nexport const envUtil = {\n\t/**\n\t * 是否为 Uni 环境\n\t * @returns\n\t */\n\tisUni(): boolean {\n\t\treturn typeof uni !== \"undefined\";\n\t},\n\t/**\n\t * 是否为 Web 环境(浏览器环境)\n\t * @returns\n\t */\n\tisWeb(): boolean {\n\t\treturn typeof window !== \"undefined\" && typeof window.document !== \"undefined\";\n\t},\n\t/**\n\t * 是否为手机设备\n\t * @returns\n\t */\n\tisMobile(): boolean {\n\t\tif (!this.isWeb()) return false;\n\t\tconst ua = navigator.userAgent || \"\";\n\t\t// Windows Phone 也归类为手机\n\t\treturn /Mobile|iPhone|Android.*Mobile|Windows Phone/i.test(ua);\n\t},\n\t/**\n\t * 是否为平板设备\n\t * @returns\n\t */\n\tisIpad(): boolean {\n\t\tif (!this.isWeb()) return false;\n\t\tconst ua = navigator.userAgent || \"\";\n\t\t// iPad 或 Android 平板通常不带 Mobile 字符串\n\t\treturn /iPad|Android(?!.*Mobile)|Tablet/i.test(ua);\n\t},\n};\n"],"names":["envUtil","isUni","uni","isWeb","window","document","isMobile","this","ua","navigator","userAgent","test","isIpad"],"mappings":"gFAGO,MAAMA,EAAU,CAKtBC,MAAiB,IACM,oBAARC,IAMfC,MAAiB,IACS,oBAAXC,aAAqD,IAApBA,OAAOC,SAMvD,QAAAC,GACC,IAAKC,KAAKJ,QAAgB,OAAA,EACpB,MAAAK,EAAKC,UAAUC,WAAa,GAE3B,MAAA,+CAA+CC,KAAKH,EAC5D,EAKA,MAAAI,GACC,IAAKL,KAAKJ,QAAgB,OAAA,EACpB,MAAAK,EAAKC,UAAUC,WAAa,GAE3B,MAAA,mCAAmCC,KAAKH,EAAE"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("vue"),t=require("../storage/index.js"),c=require("../string/index.js"),i=e.reactive({cacheKey:"__DEVICE_ID",deviceId:""}),r=/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/,d=()=>{if(i.deviceId&&r.test(i.deviceId))return t.Local.set(i.cacheKey,i.deviceId),i.deviceId;let e=t.Local.get(i.cacheKey);return e&&r.test(e)?(i.deviceId=e,e):(e=c.stringUtil.generateUUID(),t.Local.set(i.cacheKey,e),i.deviceId=e,e)};exports.useIdentity=()=>({...i,makeIdentity:d});
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../../../src/identity/index.ts"],"sourcesContent":["import { reactive } from \"vue\";\nimport { Local } from \"../storage\";\nimport { stringUtil } from \"../string\";\n\nconst state = reactive({\n\tcacheKey: \"__DEVICE_ID\",\n\tdeviceId: \"\",\n});\n\nconst uuidRegExp = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/;\n\n/**\n * 生成设备Id\n */\nconst makeIdentity = (): string => {\n\t// 判断是否存在\n\tif (state.deviceId && uuidRegExp.test(state.deviceId)) {\n\t\tLocal.set(state.cacheKey, state.deviceId);\n\t\treturn state.deviceId;\n\t}\n\t// 生成浏览器唯一 UUID\n\tlet deviceID = Local.get(state.cacheKey);\n\tif (deviceID && uuidRegExp.test(deviceID)) {\n\t\tstate.deviceId = deviceID;\n\t\treturn deviceID;\n\t}\n\tdeviceID = stringUtil.generateUUID();\n\tLocal.set(state.cacheKey, deviceID);\n\tstate.deviceId = deviceID;\n\treturn deviceID;\n};\n\nexport const useIdentity = (): typeof state & { makeIdentity: () => string } => {\n\treturn {\n\t\t...state,\n\t\tmakeIdentity,\n\t};\n};\n"],"names":["state","reactive","cacheKey","deviceId","uuidRegExp","makeIdentity","test","Local","set","deviceID","get","stringUtil","generateUUID"],"mappings":"wKAIMA,EAAQC,EAAAA,SAAS,CACtBC,SAAU,cACVC,SAAU,KAGLC,EAAa,gFAKbC,EAAe,KAEpB,GAAIL,EAAMG,UAAYC,EAAWE,KAAKN,EAAMG,UAE3C,OADAI,EAAAA,MAAMC,IAAIR,EAAME,SAAUF,EAAMG,UACzBH,EAAMG,SAGd,IAAIM,EAAWF,EAAAA,MAAMG,IAAIV,EAAME,UAC/B,OAAIO,GAAYL,EAAWE,KAAKG,IAC/BT,EAAMG,SAAWM,EACVA,IAEGE,EAAAA,aAAWC,eAChBL,EAAAA,MAAAC,IAAIR,EAAME,SAAUO,GAC1BT,EAAMG,SAAWM,EACVA,EAAA,sBAGmB,KACnB,IACHT,EACHK"}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
export * from './array';
|
|
1
2
|
export * from './base64';
|
|
2
3
|
export * from './click';
|
|
3
4
|
export * from './color';
|
|
@@ -5,7 +6,9 @@ export * from './console';
|
|
|
5
6
|
export * from './crypto';
|
|
6
7
|
export * from './date';
|
|
7
8
|
export * from './dom';
|
|
9
|
+
export * from './env';
|
|
8
10
|
export * from './error';
|
|
11
|
+
export * from './identity';
|
|
9
12
|
export * from './object';
|
|
10
13
|
export * from './storage';
|
|
11
14
|
export * from './string';
|
package/lib/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./array/index.js"),r=require("./base64/index.js"),s=require("./click/index.js"),t=require("./color/index.js"),o=require("./console/index.js"),i=require("./crypto/index.js"),n=require("./date/index.js");require("./dom/index.js");const l=require("./env/index.js"),x=require("./error/index.js"),p=require("./identity/index.js"),u=require("./object/index.js"),c=require("./storage/index.js"),a=require("./string/index.js");require("./vue/index.js");const d=require("./dom/style.js"),j=require("./vue/expose.js"),q=require("./vue/func.js"),U=require("./vue/install.js"),E=require("./vue/props.js"),y=require("./vue/slots.js"),v=require("./vue/useRender.js"),I=require("./vue/with.js");exports.arrayUtil=e.arrayUtil,exports.base64Util=r.base64Util,exports.clickUtil=s.clickUtil,exports.colorUtil=t.colorUtil,exports.consoleDebug=o.consoleDebug,exports.consoleError=o.consoleError,exports.consoleLog=o.consoleLog,exports.consoleWarn=o.consoleWarn,exports.throwError=o.throwError,exports.cryptoUtil=i.cryptoUtil,exports.dateUtil=n.dateUtil,exports.envUtil=l.envUtil,exports.FastError=x.FastError,exports.useIdentity=p.useIdentity,exports.objectUtil=u.objectUtil,exports.CACHE_EXPIRE_SUFFIX=c.CACHE_EXPIRE_SUFFIX,exports.CACHE_PREFIX=c.CACHE_PREFIX,exports.Local=c.Local,exports.Session=c.Session,exports.useStorage=c.useStorage,exports.stringUtil=a.stringUtil,exports.addUnit=d.addUnit,exports.useExpose=j.useExpose,exports.execFunction=q.execFunction,exports.withInstall=U.withInstall,exports.withInstallDirective=U.withInstallDirective,exports.withNoopInstall=U.withNoopInstall,exports.definePropType=E.definePropType,exports.useProps=E.useProps,exports.makeSlots=y.makeSlots,exports.useRender=v.useRender,exports.withDefineType=I.withDefineType;
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/lib/object/index.d.ts
CHANGED
|
@@ -6,16 +6,4 @@ export declare const objectUtil: {
|
|
|
6
6
|
* 对象URL参数化
|
|
7
7
|
*/
|
|
8
8
|
objectToQueryString(obj: any): string;
|
|
9
|
-
/**
|
|
10
|
-
* 是否存在重复值
|
|
11
|
-
*/
|
|
12
|
-
hasDuplicateProperty<T>(arr: T[], prop: keyof T): boolean;
|
|
13
|
-
/**
|
|
14
|
-
* 是否存在非重复值
|
|
15
|
-
*/
|
|
16
|
-
hasDifferentProperty<T>(arr: T[], prop: keyof T): boolean;
|
|
17
|
-
/**
|
|
18
|
-
* 时间处理翻译
|
|
19
|
-
*/
|
|
20
|
-
dateTimeFix(date: string | Date | null | undefined): string;
|
|
21
9
|
};
|
package/lib/object/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e={objectToQueryString(e){let t="";for(const o in e)Object.prototype.hasOwnProperty.call(e,o)&&(""!==t&&(t+="&"),t+=`${encodeURIComponent(o)}=${encodeURIComponent(e[o])}`);return t}};exports.objectUtil=e;
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/lib/object/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../../../src/object/index.ts"],"sourcesContent":["/**\n * 对象工具类\n */\nexport const objectUtil = {\n\t/**\n\t * 对象URL参数化\n\t */\n\tobjectToQueryString(obj: any): string {\n\t\tlet params = \"\";\n\t\tfor (const key in obj) {\n\t\t\tif (Object.prototype.hasOwnProperty.call(obj, key)) {\n\t\t\t\tif (params !== \"\") {\n\t\t\t\t\tparams += \"&\";\n\t\t\t\t}\n\t\t\t\tparams += `${encodeURIComponent(key)}=${encodeURIComponent(obj[key])}`;\n\t\t\t}\n\t\t}\n\t\treturn params;\n\t},\n
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../../../src/object/index.ts"],"sourcesContent":["/**\n * 对象工具类\n */\nexport const objectUtil = {\n\t/**\n\t * 对象URL参数化\n\t */\n\tobjectToQueryString(obj: any): string {\n\t\tlet params = \"\";\n\t\tfor (const key in obj) {\n\t\t\tif (Object.prototype.hasOwnProperty.call(obj, key)) {\n\t\t\t\tif (params !== \"\") {\n\t\t\t\t\tparams += \"&\";\n\t\t\t\t}\n\t\t\t\tparams += `${encodeURIComponent(key)}=${encodeURIComponent(obj[key])}`;\n\t\t\t}\n\t\t}\n\t\treturn params;\n\t},\n};\n"],"names":["objectUtil","objectToQueryString","obj","params","key","Object","prototype","hasOwnProperty","call","encodeURIComponent"],"mappings":"gFAGO,MAAMA,EAAa,CAIzB,mBAAAC,CAAoBC,GACnB,IAAIC,EAAS,GACb,IAAA,MAAWC,KAAOF,EACbG,OAAOC,UAAUC,eAAeC,KAAKN,EAAKE,KAC9B,KAAXD,IACOA,GAAA,KAEDA,GAAA,GAAGM,mBAAmBL,MAAQK,mBAAmBP,EAAIE,OAG1D,OAAAD,CAAA"}
|
package/lib/storage/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("vue"),r=require("../base64/index.js"),o=require("../console/index.js"),i=e.reactive({prefix:"fast__",expireSuffix:"__Expire",crypto:!1}),
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("vue"),r=require("../base64/index.js"),o=require("../console/index.js"),i=require("../error/index.js"),t=e.reactive({prefix:"fast__",expireSuffix:"__Expire",crypto:!1}),n=e.computed((()=>t.prefix)),s=e.computed((()=>t.expireSuffix)),f={set(e,r){"undefined"!=typeof uni?uni.setStorageSync(e,r):window.localStorage.setItem(e,r)},get:e=>"undefined"!=typeof uni?uni.getStorageSync(e):window.localStorage.getItem(e),remove(e){"undefined"!=typeof uni?uni.removeStorageSync(e):window.localStorage.removeItem(e)},clear(){"undefined"!=typeof uni?uni.clearStorageSync():window.localStorage.clear()},keys:()=>"undefined"!=typeof uni?uni.getStorageInfoSync().keys:window.localStorage},c={set(e,n,s,c){try{if(c??(c=t.crypto),s){if(isNaN(s)||s<1)throw new i.FastError("有效期应为一个有效数值");const r={time:Date.now(),expire:s},o=JSON.stringify(r);f.set(`${t.prefix}${e}${t.expireSuffix}`,o)}let o=JSON.stringify(n);c&&(o=r.base64Util.toBase64(o)),f.set(`${t.prefix}${e}`,o)}catch(p){o.consoleError("Local",p)}},get(e,i){try{i??(i=t.crypto);let o=f.get(`${t.prefix}${e}`);if(o){i&&(o=r.base64Util.base64ToStr(o));const n=f.get(`${t.prefix}${e}${t.expireSuffix}`);if(n){const r=JSON.parse(n);if(Date.now()>r.time+60*r.expire*1e3)return f.remove(`${t.prefix}${e}`),f.remove(`${t.prefix}${e}${t.expireSuffix}`),null}try{return JSON.parse(o)}catch{return o}}return null}catch(n){o.consoleError("Local",n)}},remove(e){try{f.remove(`${t.prefix}${e}`),f.remove(`${t.prefix}${e}${t.expireSuffix}`)}catch(r){o.consoleError("Local",r)}},removeByPrefix(e){try{for(const r in f.keys)-1!==r.indexOf(`${t.prefix}${e}`)&&f.remove(r)}catch(r){o.consoleError("Local",r)}},clear(){try{f.clear()}catch(e){o.consoleError("Local",e)}}},p={set(e,n,s,f){if("undefined"==typeof uni)try{if(f??(f=t.crypto),s){if(isNaN(s)||s<1)throw new i.FastError("有效期应为一个有效数值");const r={time:Date.now(),expire:s},o=JSON.stringify(r);window.sessionStorage.setItem(`${t.prefix}${e}${t.expireSuffix}`,o)}let o=JSON.stringify(n);f&&(o=r.base64Util.toBase64(o)),window.sessionStorage.setItem(`${t.prefix}${e}`,o)}catch(c){o.consoleError("Session",c)}else o.consoleError("Session","UniApp 环境下 [Session] 不可用。")},get(e,i){if("undefined"==typeof uni)try{i??(i=t.crypto);let o=window.sessionStorage.getItem(`${t.prefix}${e}`);if(o){i&&(o=r.base64Util.base64ToStr(o));const n=window.sessionStorage.getItem(`${t.prefix}${e}${t.expireSuffix}`);if(n){const r=JSON.parse(n);if(Date.now()>r.time+60*r.expire*1e3)return window.sessionStorage.removeItem(`${t.prefix}${e}`),window.sessionStorage.removeItem(`${t.prefix}${e}${t.expireSuffix}`),null}try{return JSON.parse(o)}catch{return o}}return null}catch(n){o.consoleError("Session",n)}else o.consoleError("Session","UniApp 环境下 [Session] 不可用。")},remove(e){if("undefined"==typeof uni)try{window.sessionStorage.removeItem(`${t.prefix}${e}`),window.sessionStorage.removeItem(`${t.prefix}${e}${t.expireSuffix}`)}catch(r){o.consoleError("Session",r)}else o.consoleError("Session","UniApp 环境下 [Session] 不可用。")},removeByPrefix(e){if("undefined"==typeof uni)try{for(const r in window.sessionStorage)-1!==r.indexOf(`${t.prefix}${e}`)&&window.sessionStorage.removeItem(r)}catch(r){o.consoleError("Session",r)}else o.consoleError("Session","UniApp 环境下 [Session] 不可用。")},clear(){if("undefined"==typeof uni)try{window.sessionStorage.clear()}catch(e){o.consoleError("Session",e)}else o.consoleError("Session","UniApp 环境下 [Session] 不可用。")}};exports.CACHE_EXPIRE_SUFFIX=s,exports.CACHE_PREFIX=n,exports.Local=c,exports.Session=p,exports.useStorage=()=>({prefix(e){t.prefix=e},expireSuffix(e){t.expireSuffix=e},crypto(e){t.crypto=e}});
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/lib/storage/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../../../src/storage/index.ts"],"sourcesContent":["import { computed, reactive } from \"vue\";\nimport { base64Util } from \"../base64\";\nimport { consoleError } from \"../console\";\n\nconst state = reactive({\n\tprefix: \"fast__\",\n\texpireSuffix: \"__Expire\",\n\tcrypto: false,\n});\n\n/**\n * 本地缓存前缀 Key\n */\nexport const CACHE_PREFIX = computed(() => state.prefix);\n\n/**\n * 本地缓存过期值后缀 Key\n */\nexport const CACHE_EXPIRE_SUFFIX = computed(() => state.expireSuffix);\n\n// eslint-disable-next-line @typescript-eslint/explicit-function-return-type\nexport const useStorage = () => {\n\treturn {\n\t\t/**\n\t\t * 设置缓存前缀 Key\n\t\t * @param key\n\t\t */\n\t\tprefix(key: string): void {\n\t\t\tstate.prefix = key;\n\t\t},\n\t\t/**\n\t\t * 缓存过期值后缀 Key\n\t\t * @param key\n\t\t */\n\t\texpireSuffix(key: string): void {\n\t\t\tstate.expireSuffix = key;\n\t\t},\n\t\t/**\n\t\t * 设置缓存是否加密\n\t\t * @param crypto\n\t\t */\n\t\tcrypto(crypto: boolean): void {\n\t\t\tstate.crypto = crypto;\n\t\t},\n\t};\n};\n\nconst storage = {\n\tset(key: string, val: any): void {\n\t\tif (typeof uni !== \"undefined\") {\n\t\t\tuni.setStorageSync(key, val);\n\t\t} else {\n\t\t\twindow.localStorage.setItem(key, val);\n\t\t}\n\t},\n\tget(key: string): string | any | null {\n\t\tif (typeof uni !== \"undefined\") {\n\t\t\treturn uni.getStorageSync(key);\n\t\t} else {\n\t\t\treturn window.localStorage.getItem(key);\n\t\t}\n\t},\n\tremove(key: string): void {\n\t\tif (typeof uni !== \"undefined\") {\n\t\t\tuni.removeStorageSync(key);\n\t\t} else {\n\t\t\twindow.localStorage.removeItem(key);\n\t\t}\n\t},\n\tclear(): void {\n\t\tif (typeof uni !== \"undefined\") {\n\t\t\tuni.clearStorageSync();\n\t\t} else {\n\t\t\twindow.localStorage.clear();\n\t\t}\n\t},\n\tkeys(): string[] | Storage {\n\t\tif (typeof uni !== \"undefined\") {\n\t\t\treturn uni.getStorageInfoSync().keys;\n\t\t} else {\n\t\t\treturn window.localStorage;\n\t\t}\n\t},\n};\n\n/**\n * window.localStorage\n * - 如果是 UniApp 环境则使用的是 uni.xxxStorage\n */\nexport const Local = {\n\t/**\n\t * 设置\n\t * @param key 缓存的Key\n\t * @param val 缓存值\n\t * @param expire 过期时间,单位分钟\n\t * @param encrypt 是否对缓存的数据加密\n\t */\n\tset(key: string, val: any, expire?: number, encrypt?: boolean): void {\n\t\ttry {\n\t\t\tencrypt ??= state.crypto;\n\t\t\t// 判断是否存在缓存过期时间\n\t\t\tif (expire) {\n\t\t\t\tif (isNaN(expire) || expire < 1) {\n\t\t\t\t\tthrow new Error(\"有效期应为一个有效数值\");\n\t\t\t\t}\n\t\t\t\t// 设置过期时间的缓存\n\t\t\t\tconst expireData = {\n\t\t\t\t\ttime: Date.now(),\n\t\t\t\t\texpire,\n\t\t\t\t};\n\t\t\t\tconst expireJson = JSON.stringify(expireData);\n\t\t\t\tstorage.set(`${state.prefix}${key}${state.expireSuffix}`, expireJson);\n\t\t\t}\n\t\t\tlet valJson = JSON.stringify(val);\n\t\t\tif (encrypt) {\n\t\t\t\tvalJson = base64Util.toBase64(valJson);\n\t\t\t}\n\t\t\tstorage.set(`${state.prefix}${key}`, valJson);\n\t\t} catch (error) {\n\t\t\tconsoleError(\"Local\", error);\n\t\t}\n\t},\n\t/**\n\t * 获取\n\t * @param key 缓存的Key\n\t * @param decrypt 是否对缓存的数据解密\n\t * @returns {T} 传入的对象类型,默认为 string\n\t */\n\tget<T = string>(key: string, decrypt?: boolean): T {\n\t\ttry {\n\t\t\tdecrypt ??= state.crypto;\n\t\t\t// 获取缓存 JSON 字符串\n\t\t\tlet valJson = storage.get(`${state.prefix}${key}`);\n\n\t\t\tif (valJson) {\n\t\t\t\t// 判断是否解密\n\t\t\t\tif (decrypt) {\n\t\t\t\t\tvalJson = base64Util.base64ToStr(valJson);\n\t\t\t\t}\n\t\t\t\t// 尝试获取缓存过期时间的 JSON 字符串\n\t\t\t\tconst expireJson = storage.get(`${state.prefix}${key}${state.expireSuffix}`);\n\t\t\t\t// 判断是否存在过期时间\n\t\t\t\tif (expireJson) {\n\t\t\t\t\tconst expireData = JSON.parse(expireJson);\n\t\t\t\t\tif (Date.now() > expireData.time + expireData.expire * 60 * 1000) {\n\t\t\t\t\t\t// 过期了,删除对应的缓存数据\n\t\t\t\t\t\tstorage.remove(`${state.prefix}${key}`);\n\t\t\t\t\t\tstorage.remove(`${state.prefix}${key}${state.expireSuffix}`);\n\t\t\t\t\t\treturn null;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\ttry {\n\t\t\t\t\treturn JSON.parse(valJson) as T;\n\t\t\t\t} catch {\n\t\t\t\t\treturn valJson as T;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn null;\n\t\t} catch (error) {\n\t\t\tconsoleError(\"Local\", error);\n\t\t}\n\t},\n\t/**\n\t * 移除\n\t * @param key 缓存的Key\n\t */\n\tremove(key: string): void {\n\t\ttry {\n\t\t\tstorage.remove(`${state.prefix}${key}`);\n\t\t\tstorage.remove(`${state.prefix}${key}${state.expireSuffix}`);\n\t\t} catch (error) {\n\t\t\tconsoleError(\"Local\", error);\n\t\t}\n\t},\n\t/**\n\t * 根据前缀移除\n\t * @param key 缓存的Key\n\t */\n\tremoveByPrefix(key: string): void {\n\t\ttry {\n\t\t\tfor (const itemKey in storage.keys) {\n\t\t\t\tif (itemKey.indexOf(`${state.prefix}${key}`) !== -1) {\n\t\t\t\t\tstorage.remove(itemKey);\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tconsoleError(\"Local\", error);\n\t\t}\n\t},\n\t/**\n\t * 移除全部\n\t */\n\tclear(): void {\n\t\ttry {\n\t\t\tstorage.clear();\n\t\t} catch (error) {\n\t\t\tconsoleError(\"Local\", error);\n\t\t}\n\t},\n};\n\n/**\n * window.sessionStorage\n * - UniApp 环境下不可用\n */\nexport const Session = {\n\t/**\n\t * 设置会话缓存\n\t * @param key 缓存的Key\n\t * @param val 缓存值\n\t * @param expire 过期时间,单位分钟\n\t * @param encrypt 是否对缓存的数据加密\n\t */\n\tset(key: string, val: any, expire?: number, encrypt?: boolean): void {\n\t\tif (typeof uni !== \"undefined\") {\n\t\t\tconsoleError(\"Session\", \"UniApp 环境下 [Session] 不可用。\");\n\t\t\treturn;\n\t\t}\n\t\ttry {\n\t\t\tencrypt ??= state.crypto;\n\t\t\t// 判断是否存在缓存过期时间\n\t\t\tif (expire) {\n\t\t\t\tif (isNaN(expire) || expire < 1) {\n\t\t\t\t\tthrow new Error(\"有效期应为一个有效数值\");\n\t\t\t\t}\n\t\t\t\t// 设置过期时间的缓存\n\t\t\t\tconst expireData = {\n\t\t\t\t\ttime: Date.now(),\n\t\t\t\t\texpire,\n\t\t\t\t};\n\t\t\t\tconst expireJson = JSON.stringify(expireData);\n\t\t\t\twindow.sessionStorage.setItem(`${state.prefix}${key}${state.expireSuffix}`, expireJson);\n\t\t\t}\n\t\t\tlet valJson = JSON.stringify(val);\n\t\t\tif (encrypt) {\n\t\t\t\tvalJson = base64Util.toBase64(valJson);\n\t\t\t}\n\t\t\twindow.sessionStorage.setItem(`${state.prefix}${key}`, valJson);\n\t\t} catch (error) {\n\t\t\tconsoleError(\"Session\", error);\n\t\t}\n\t},\n\t/**\n\t * 获取会话缓存\n\t * @param key 缓存的Key\n\t * @param decrypt 是否对缓存的数据解密\n\t * @returns {T} 传入的对象类型,默认为 string\n\t */\n\tget<T = string>(key: string, decrypt?: boolean): T {\n\t\tif (typeof uni !== \"undefined\") {\n\t\t\tconsoleError(\"Session\", \"UniApp 环境下 [Session] 不可用。\");\n\t\t\treturn;\n\t\t}\n\t\ttry {\n\t\t\tdecrypt ??= state.crypto;\n\t\t\t// 获取缓存 JSON 字符串\n\t\t\tlet valJson = window.sessionStorage.getItem(`${state.prefix}${key}`);\n\t\t\tif (valJson) {\n\t\t\t\t// 判断是否解密\n\t\t\t\tif (decrypt) {\n\t\t\t\t\tvalJson = base64Util.base64ToStr(valJson);\n\t\t\t\t}\n\t\t\t\t// 尝试获取缓存过期时间的 JSON 字符串\n\t\t\t\tconst expireJson = window.sessionStorage.getItem(`${state.prefix}${key}${state.expireSuffix}`);\n\t\t\t\t// 判断是否存在过期时间\n\t\t\t\tif (expireJson) {\n\t\t\t\t\tconst expireData = JSON.parse(expireJson);\n\t\t\t\t\tif (Date.now() > expireData.time + expireData.expire * 60 * 1000) {\n\t\t\t\t\t\t// 过期了,删除对应的缓存数据\n\t\t\t\t\t\twindow.sessionStorage.removeItem(`${state.prefix}${key}`);\n\t\t\t\t\t\twindow.sessionStorage.removeItem(`${state.prefix}${key}${state.expireSuffix}`);\n\t\t\t\t\t\treturn null;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\ttry {\n\t\t\t\t\treturn JSON.parse(valJson) as T;\n\t\t\t\t} catch {\n\t\t\t\t\treturn valJson as T;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn null;\n\t\t} catch (error) {\n\t\t\tconsoleError(\"Session\", error);\n\t\t}\n\t},\n\t/**\n\t * 移除会话缓存\n\t * @param key 缓存的Key\n\t */\n\tremove(key: string): void {\n\t\tif (typeof uni !== \"undefined\") {\n\t\t\tconsoleError(\"Session\", \"UniApp 环境下 [Session] 不可用。\");\n\t\t\treturn;\n\t\t}\n\t\ttry {\n\t\t\twindow.sessionStorage.removeItem(`${state.prefix}${key}`);\n\t\t\twindow.sessionStorage.removeItem(`${state.prefix}${key}${state.expireSuffix}`);\n\t\t} catch (error) {\n\t\t\tconsoleError(\"Session\", error);\n\t\t}\n\t},\n\t/**\n\t * 根据前缀移除会话缓存\n\t * @param key 缓存的Key\n\t */\n\tremoveByPrefix(key: string): void {\n\t\tif (typeof uni !== \"undefined\") {\n\t\t\tconsoleError(\"Session\", \"UniApp 环境下 [Session] 不可用。\");\n\t\t\treturn;\n\t\t}\n\t\ttry {\n\t\t\tfor (const itemKey in window.sessionStorage) {\n\t\t\t\tif (itemKey.indexOf(`${state.prefix}${key}`) !== -1) {\n\t\t\t\t\twindow.sessionStorage.removeItem(itemKey);\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tconsoleError(\"Session\", error);\n\t\t}\n\t},\n\t/**\n\t * 移除全部会话缓存\n\t */\n\tclear(): void {\n\t\tif (typeof uni !== \"undefined\") {\n\t\t\tconsoleError(\"Session\", \"UniApp 环境下 [Session] 不可用。\");\n\t\t\treturn;\n\t\t}\n\t\ttry {\n\t\t\twindow.sessionStorage.clear();\n\t\t} catch (error) {\n\t\t\tconsoleError(\"Session\", error);\n\t\t}\n\t},\n};\n"],"names":["state","reactive","prefix","expireSuffix","crypto","CACHE_PREFIX","computed","CACHE_EXPIRE_SUFFIX","storage","set","key","val","uni","setStorageSync","window","localStorage","setItem","get","getStorageSync","getItem","remove","removeStorageSync","removeItem","clear","clearStorageSync","keys","getStorageInfoSync","Local","expire","encrypt","isNaN","Error","expireData","time","Date","now","expireJson","JSON","stringify","valJson","base64Util","toBase64","error","index","consoleError","decrypt","base64ToStr","parse","removeByPrefix","itemKey","indexOf","Session","sessionStorage"],"mappings":"wKAIMA,EAAQC,EAAAA,SAAS,CACtBC,OAAQ,SACRC,aAAc,WACdC,QAAQ,IAMIC,EAAeC,EAAAA,UAAS,IAAMN,EAAME,SAKpCK,EAAsBD,EAAAA,UAAS,IAAMN,EAAMG,eA6BlDK,EAAU,CACf,GAAAC,CAAIC,EAAaC,GACG,oBAARC,IACNA,IAAAC,eAAeH,EAAKC,GAEjBG,OAAAC,aAAaC,QAAQN,EAAKC,EAEnC,EACAM,IAAIP,GACgB,oBAARE,IACHA,IAAIM,eAAeR,GAEnBI,OAAOC,aAAaI,QAAQT,GAGrC,MAAAU,CAAOV,GACa,oBAARE,IACVA,IAAIS,kBAAkBX,GAEfI,OAAAC,aAAaO,WAAWZ,EAEjC,EACA,KAAAa,GACoB,oBAARX,IACVA,IAAIY,mBAEJV,OAAOC,aAAaQ,OAEtB,EACAE,KAA2B,IACP,oBAARb,IACHA,IAAIc,qBAAqBD,KAEzBX,OAAOC,cASJY,EAAQ,CAQpB,GAAAlB,CAAIC,EAAaC,EAAUiB,EAAiBC,GACvC,IAGH,GAFAA,IAAAA,EAAY7B,EAAMI,QAEdwB,EAAQ,CACX,GAAIE,MAAMF,IAAWA,EAAS,EACvB,MAAA,IAAIG,MAAM,eAGjB,MAAMC,EAAa,CAClBC,KAAMC,KAAKC,MACXP,UAEKQ,EAAaC,KAAKC,UAAUN,GAC1BxB,EAAAC,IAAI,GAAGT,EAAME,SAASQ,IAAMV,EAAMG,eAAgBiC,EAAU,CAEjE,IAAAG,EAAUF,KAAKC,UAAU3B,GACzBkB,IACOW,EAAAA,EAAAA,WAAWC,SAASF,IAE/B/B,EAAQC,IAAI,GAAGT,EAAME,SAASQ,IAAO6B,SAC7BG,GACRC,EAAAC,aAAa,QAASF,EAAK,CAE7B,EAOA,GAAAzB,CAAgBP,EAAamC,GACxB,IACHA,IAAA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AAMG,sBACvDuC,GACRC,EAAAC,aAAa,UAAWF,EAAK,MAP7BC,EAAAC,aAAa,UAAW,4BAS1B,EAKA,cAAAI,CAAetC,GACV,GAAe,oBAARE,IAIP,IACQ,IAAA,MAAAqC,KAAWnC,OAAOsC,gBACyB,IAAjDH,EAAQC,QAAQ,GAAGlD,EAAME,SAASQ,MAC9BI,OAAAsC,eAAe9B,WAAW2B,SAG3BP,GACRC,EAAAC,aAAa,UAAWF,EAAK,MAV7BC,EAAAC,aAAa,UAAW,4BAY1B,EAIA,KAAArB,GACK,GAAe,oBAARX,IAIP,IACHE,OAAOsC,eAAe7B,cACdmB,GACRC,EAAAC,aAAa,UAAWF,EAAK,MAN7BC,EAAAC,aAAa,UAAW,4BAOzB,6GAvTwB,KAClB,CAKN,MAAA1C,CAAOQ,GACNV,EAAME,OAASQ,CAChB,EAKA,YAAAP,CAAaO,GACZV,EAAMG,aAAeO,CACtB,EAKA,MAAAN,CAAOA,GACNJ,EAAMI,OAASA,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../../../src/storage/index.ts"],"sourcesContent":["import { computed, reactive } from \"vue\";\nimport { base64Util } from \"../base64\";\nimport { consoleError } from \"../console\";\nimport { FastError } from \"../error\";\n\nconst state = reactive({\n\tprefix: \"fast__\",\n\texpireSuffix: \"__Expire\",\n\tcrypto: false,\n});\n\n/**\n * 本地缓存前缀 Key\n */\nexport const CACHE_PREFIX = computed(() => state.prefix);\n\n/**\n * 本地缓存过期值后缀 Key\n */\nexport const CACHE_EXPIRE_SUFFIX = computed(() => state.expireSuffix);\n\n// eslint-disable-next-line @typescript-eslint/explicit-function-return-type\nexport const useStorage = () => {\n\treturn {\n\t\t/**\n\t\t * 设置缓存前缀 Key\n\t\t * @param key\n\t\t */\n\t\tprefix(key: string): void {\n\t\t\tstate.prefix = key;\n\t\t},\n\t\t/**\n\t\t * 缓存过期值后缀 Key\n\t\t * @param key\n\t\t */\n\t\texpireSuffix(key: string): void {\n\t\t\tstate.expireSuffix = key;\n\t\t},\n\t\t/**\n\t\t * 设置缓存是否加密\n\t\t * @param crypto\n\t\t */\n\t\tcrypto(crypto: boolean): void {\n\t\t\tstate.crypto = crypto;\n\t\t},\n\t};\n};\n\nconst storage = {\n\tset(key: string, val: any): void {\n\t\tif (typeof uni !== \"undefined\") {\n\t\t\tuni.setStorageSync(key, val);\n\t\t} else {\n\t\t\twindow.localStorage.setItem(key, val);\n\t\t}\n\t},\n\tget(key: string): string | any | null {\n\t\tif (typeof uni !== \"undefined\") {\n\t\t\treturn uni.getStorageSync(key);\n\t\t} else {\n\t\t\treturn window.localStorage.getItem(key);\n\t\t}\n\t},\n\tremove(key: string): void {\n\t\tif (typeof uni !== \"undefined\") {\n\t\t\tuni.removeStorageSync(key);\n\t\t} else {\n\t\t\twindow.localStorage.removeItem(key);\n\t\t}\n\t},\n\tclear(): void {\n\t\tif (typeof uni !== \"undefined\") {\n\t\t\tuni.clearStorageSync();\n\t\t} else {\n\t\t\twindow.localStorage.clear();\n\t\t}\n\t},\n\tkeys(): string[] | Storage {\n\t\tif (typeof uni !== \"undefined\") {\n\t\t\treturn uni.getStorageInfoSync().keys;\n\t\t} else {\n\t\t\treturn window.localStorage;\n\t\t}\n\t},\n};\n\n/**\n * window.localStorage\n * - 如果是 UniApp 环境则使用的是 uni.xxxStorage\n */\nexport const Local = {\n\t/**\n\t * 设置\n\t * @param key 缓存的Key\n\t * @param val 缓存值\n\t * @param expire 过期时间,单位分钟\n\t * @param encrypt 是否对缓存的数据加密\n\t */\n\tset(key: string, val: any, expire?: number, encrypt?: boolean): void {\n\t\ttry {\n\t\t\tencrypt ??= state.crypto;\n\t\t\t// 判断是否存在缓存过期时间\n\t\t\tif (expire) {\n\t\t\t\tif (isNaN(expire) || expire < 1) {\n\t\t\t\t\tthrow new FastError(\"有效期应为一个有效数值\");\n\t\t\t\t}\n\t\t\t\t// 设置过期时间的缓存\n\t\t\t\tconst expireData = {\n\t\t\t\t\ttime: Date.now(),\n\t\t\t\t\texpire,\n\t\t\t\t};\n\t\t\t\tconst expireJson = JSON.stringify(expireData);\n\t\t\t\tstorage.set(`${state.prefix}${key}${state.expireSuffix}`, expireJson);\n\t\t\t}\n\t\t\tlet valJson = JSON.stringify(val);\n\t\t\tif (encrypt) {\n\t\t\t\tvalJson = base64Util.toBase64(valJson);\n\t\t\t}\n\t\t\tstorage.set(`${state.prefix}${key}`, valJson);\n\t\t} catch (error) {\n\t\t\tconsoleError(\"Local\", error);\n\t\t}\n\t},\n\t/**\n\t * 获取\n\t * @param key 缓存的Key\n\t * @param decrypt 是否对缓存的数据解密\n\t * @returns {T} 传入的对象类型,默认为 string\n\t */\n\tget<T = string>(key: string, decrypt?: boolean): T {\n\t\ttry {\n\t\t\tdecrypt ??= state.crypto;\n\t\t\t// 获取缓存 JSON 字符串\n\t\t\tlet valJson = storage.get(`${state.prefix}${key}`);\n\n\t\t\tif (valJson) {\n\t\t\t\t// 判断是否解密\n\t\t\t\tif (decrypt) {\n\t\t\t\t\tvalJson = base64Util.base64ToStr(valJson);\n\t\t\t\t}\n\t\t\t\t// 尝试获取缓存过期时间的 JSON 字符串\n\t\t\t\tconst expireJson = storage.get(`${state.prefix}${key}${state.expireSuffix}`);\n\t\t\t\t// 判断是否存在过期时间\n\t\t\t\tif (expireJson) {\n\t\t\t\t\tconst expireData = JSON.parse(expireJson);\n\t\t\t\t\tif (Date.now() > expireData.time + expireData.expire * 60 * 1000) {\n\t\t\t\t\t\t// 过期了,删除对应的缓存数据\n\t\t\t\t\t\tstorage.remove(`${state.prefix}${key}`);\n\t\t\t\t\t\tstorage.remove(`${state.prefix}${key}${state.expireSuffix}`);\n\t\t\t\t\t\treturn null;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\ttry {\n\t\t\t\t\treturn JSON.parse(valJson) as T;\n\t\t\t\t} catch {\n\t\t\t\t\treturn valJson as T;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn null;\n\t\t} catch (error) {\n\t\t\tconsoleError(\"Local\", error);\n\t\t}\n\t},\n\t/**\n\t * 移除\n\t * @param key 缓存的Key\n\t */\n\tremove(key: string): void {\n\t\ttry {\n\t\t\tstorage.remove(`${state.prefix}${key}`);\n\t\t\tstorage.remove(`${state.prefix}${key}${state.expireSuffix}`);\n\t\t} catch (error) {\n\t\t\tconsoleError(\"Local\", error);\n\t\t}\n\t},\n\t/**\n\t * 根据前缀移除\n\t * @param key 缓存的Key\n\t */\n\tremoveByPrefix(key: string): void {\n\t\ttry {\n\t\t\tfor (const itemKey in storage.keys) {\n\t\t\t\tif (itemKey.indexOf(`${state.prefix}${key}`) !== -1) {\n\t\t\t\t\tstorage.remove(itemKey);\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tconsoleError(\"Local\", error);\n\t\t}\n\t},\n\t/**\n\t * 移除全部\n\t */\n\tclear(): void {\n\t\ttry {\n\t\t\tstorage.clear();\n\t\t} catch (error) {\n\t\t\tconsoleError(\"Local\", error);\n\t\t}\n\t},\n};\n\n/**\n * window.sessionStorage\n * - UniApp 环境下不可用\n */\nexport const Session = {\n\t/**\n\t * 设置会话缓存\n\t * @param key 缓存的Key\n\t * @param val 缓存值\n\t * @param expire 过期时间,单位分钟\n\t * @param encrypt 是否对缓存的数据加密\n\t */\n\tset(key: string, val: any, expire?: number, encrypt?: boolean): void {\n\t\tif (typeof uni !== \"undefined\") {\n\t\t\tconsoleError(\"Session\", \"UniApp 环境下 [Session] 不可用。\");\n\t\t\treturn;\n\t\t}\n\t\ttry {\n\t\t\tencrypt ??= state.crypto;\n\t\t\t// 判断是否存在缓存过期时间\n\t\t\tif (expire) {\n\t\t\t\tif (isNaN(expire) || expire < 1) {\n\t\t\t\t\tthrow new FastError(\"有效期应为一个有效数值\");\n\t\t\t\t}\n\t\t\t\t// 设置过期时间的缓存\n\t\t\t\tconst expireData = {\n\t\t\t\t\ttime: Date.now(),\n\t\t\t\t\texpire,\n\t\t\t\t};\n\t\t\t\tconst expireJson = JSON.stringify(expireData);\n\t\t\t\twindow.sessionStorage.setItem(`${state.prefix}${key}${state.expireSuffix}`, expireJson);\n\t\t\t}\n\t\t\tlet valJson = JSON.stringify(val);\n\t\t\tif (encrypt) {\n\t\t\t\tvalJson = base64Util.toBase64(valJson);\n\t\t\t}\n\t\t\twindow.sessionStorage.setItem(`${state.prefix}${key}`, valJson);\n\t\t} catch (error) {\n\t\t\tconsoleError(\"Session\", error);\n\t\t}\n\t},\n\t/**\n\t * 获取会话缓存\n\t * @param key 缓存的Key\n\t * @param decrypt 是否对缓存的数据解密\n\t * @returns {T} 传入的对象类型,默认为 string\n\t */\n\tget<T = string>(key: string, decrypt?: boolean): T {\n\t\tif (typeof uni !== \"undefined\") {\n\t\t\tconsoleError(\"Session\", \"UniApp 环境下 [Session] 不可用。\");\n\t\t\treturn;\n\t\t}\n\t\ttry {\n\t\t\tdecrypt ??= state.crypto;\n\t\t\t// 获取缓存 JSON 字符串\n\t\t\tlet valJson = window.sessionStorage.getItem(`${state.prefix}${key}`);\n\t\t\tif (valJson) {\n\t\t\t\t// 判断是否解密\n\t\t\t\tif (decrypt) {\n\t\t\t\t\tvalJson = base64Util.base64ToStr(valJson);\n\t\t\t\t}\n\t\t\t\t// 尝试获取缓存过期时间的 JSON 字符串\n\t\t\t\tconst expireJson = window.sessionStorage.getItem(`${state.prefix}${key}${state.expireSuffix}`);\n\t\t\t\t// 判断是否存在过期时间\n\t\t\t\tif (expireJson) {\n\t\t\t\t\tconst expireData = JSON.parse(expireJson);\n\t\t\t\t\tif (Date.now() > expireData.time + expireData.expire * 60 * 1000) {\n\t\t\t\t\t\t// 过期了,删除对应的缓存数据\n\t\t\t\t\t\twindow.sessionStorage.removeItem(`${state.prefix}${key}`);\n\t\t\t\t\t\twindow.sessionStorage.removeItem(`${state.prefix}${key}${state.expireSuffix}`);\n\t\t\t\t\t\treturn null;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\ttry {\n\t\t\t\t\treturn JSON.parse(valJson) as T;\n\t\t\t\t} catch {\n\t\t\t\t\treturn valJson as T;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn null;\n\t\t} catch (error) {\n\t\t\tconsoleError(\"Session\", error);\n\t\t}\n\t},\n\t/**\n\t * 移除会话缓存\n\t * @param key 缓存的Key\n\t */\n\tremove(key: string): void {\n\t\tif (typeof uni !== \"undefined\") {\n\t\t\tconsoleError(\"Session\", \"UniApp 环境下 [Session] 不可用。\");\n\t\t\treturn;\n\t\t}\n\t\ttry {\n\t\t\twindow.sessionStorage.removeItem(`${state.prefix}${key}`);\n\t\t\twindow.sessionStorage.removeItem(`${state.prefix}${key}${state.expireSuffix}`);\n\t\t} catch (error) {\n\t\t\tconsoleError(\"Session\", error);\n\t\t}\n\t},\n\t/**\n\t * 根据前缀移除会话缓存\n\t * @param key 缓存的Key\n\t */\n\tremoveByPrefix(key: string): void {\n\t\tif (typeof uni !== \"undefined\") {\n\t\t\tconsoleError(\"Session\", \"UniApp 环境下 [Session] 不可用。\");\n\t\t\treturn;\n\t\t}\n\t\ttry {\n\t\t\tfor (const itemKey in window.sessionStorage) {\n\t\t\t\tif (itemKey.indexOf(`${state.prefix}${key}`) !== -1) {\n\t\t\t\t\twindow.sessionStorage.removeItem(itemKey);\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tconsoleError(\"Session\", error);\n\t\t}\n\t},\n\t/**\n\t * 移除全部会话缓存\n\t */\n\tclear(): void {\n\t\tif (typeof uni !== \"undefined\") {\n\t\t\tconsoleError(\"Session\", \"UniApp 环境下 [Session] 不可用。\");\n\t\t\treturn;\n\t\t}\n\t\ttry {\n\t\t\twindow.sessionStorage.clear();\n\t\t} catch (error) {\n\t\t\tconsoleError(\"Session\", error);\n\t\t}\n\t},\n};\n"],"names":["state","reactive","prefix","expireSuffix","crypto","CACHE_PREFIX","computed","CACHE_EXPIRE_SUFFIX","storage","set","key","val","uni","setStorageSync","window","localStorage","setItem","get","getStorageSync","getItem","remove","removeStorageSync","removeItem","clear","clearStorageSync","keys","getStorageInfoSync","Local","expire","encrypt","isNaN","FastError","expireData","time","Date","now","expireJson","JSON","stringify","valJson","base64Util","toBase64","error","index","consoleError","decrypt","base64ToStr","parse","removeByPrefix","itemKey","indexOf","Session","sessionStorage"],"mappings":"uMAKMA,EAAQC,EAAAA,SAAS,CACtBC,OAAQ,SACRC,aAAc,WACdC,QAAQ,IAMIC,EAAeC,EAAAA,UAAS,IAAMN,EAAME,SAKpCK,EAAsBD,EAAAA,UAAS,IAAMN,EAAMG,eA6BlDK,EAAU,CACf,GAAAC,CAAIC,EAAaC,GACG,oBAARC,IACNA,IAAAC,eAAeH,EAAKC,GAEjBG,OAAAC,aAAaC,QAAQN,EAAKC,EAEnC,EACAM,IAAIP,GACgB,oBAARE,IACHA,IAAIM,eAAeR,GAEnBI,OAAOC,aAAaI,QAAQT,GAGrC,MAAAU,CAAOV,GACa,oBAARE,IACVA,IAAIS,kBAAkBX,GAEfI,OAAAC,aAAaO,WAAWZ,EAEjC,EACA,KAAAa,GACoB,oBAARX,IACVA,IAAIY,mBAEJV,OAAOC,aAAaQ,OAEtB,EACAE,KAA2B,IACP,oBAARb,IACHA,IAAIc,qBAAqBD,KAEzBX,OAAOC,cASJY,EAAQ,CAQpB,GAAAlB,CAAIC,EAAaC,EAAUiB,EAAiBC,GACvC,IAGH,GAFAA,IAAAA,EAAY7B,EAAMI,QAEdwB,EAAQ,CACX,GAAIE,MAAMF,IAAWA,EAAS,EACvB,MAAA,IAAIG,YAAU,eAGrB,MAAMC,EAAa,CAClBC,KAAMC,KAAKC,MACXP,UAEKQ,EAAaC,KAAKC,UAAUN,GAC1BxB,EAAAC,IAAI,GAAGT,EAAME,SAASQ,IAAMV,EAAMG,eAAgBiC,EAAU,CAEjE,IAAAG,EAAUF,KAAKC,UAAU3B,GACzBkB,IACOW,EAAAA,EAAAA,WAAWC,SAASF,IAE/B/B,EAAQC,IAAI,GAAGT,EAAME,SAASQ,IAAO6B,SAC7BG,GACRC,EAAA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EAAME,SAASQ,KAC5CI,OAAAsC,eAAe9B,WAAW,GAAGtB,EAAME,SAASQ,IAAMV,EAAMG,sBACvDuC,GACRC,EAAAC,aAAa,UAAWF,EAAK,MAP7BC,EAAAC,aAAa,UAAW,4BAS1B,EAKA,cAAAI,CAAetC,GACV,GAAe,oBAARE,IAIP,IACQ,IAAA,MAAAqC,KAAWnC,OAAOsC,gBACyB,IAAjDH,EAAQC,QAAQ,GAAGlD,EAAME,SAASQ,MAC9BI,OAAAsC,eAAe9B,WAAW2B,SAG3BP,GACRC,EAAAC,aAAa,UAAWF,EAAK,MAV7BC,EAAAC,aAAa,UAAW,4BAY1B,EAIA,KAAArB,GACK,GAAe,oBAARX,IAIP,IACHE,OAAOsC,eAAe7B,cACdmB,GACRC,EAAAC,aAAa,UAAWF,EAAK,MAN7BC,EAAAC,aAAa,UAAW,4BAOzB,6GAvTwB,KAClB,CAKN,MAAA1C,CAAOQ,GACNV,EAAME,OAASQ,CAChB,EAKA,YAAAP,CAAaO,GACZV,EAAMG,aAAeO,CACtB,EAKA,MAAAN,CAAOA,GACNJ,EAAMI,OAASA,CAAA"}
|
package/lib/string/index.d.ts
CHANGED
|
@@ -10,6 +10,27 @@ export declare const stringUtil: {
|
|
|
10
10
|
* 是否为JSON字符串
|
|
11
11
|
*/
|
|
12
12
|
isJson(value: string): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* 切割骆驼命名式字符串
|
|
15
|
+
*/
|
|
16
|
+
splitCamelCase(value: string): string[];
|
|
17
|
+
/**
|
|
18
|
+
* 将字符串转为 camelCase 格式,支持 - 或 _ 分隔的字符串
|
|
19
|
+
* 例如:'hello-world' 或 'hello_world' => 'helloWorld'
|
|
20
|
+
*/
|
|
21
|
+
toCamelCase(value: string): string;
|
|
22
|
+
/**
|
|
23
|
+
* 字符串首字母大写
|
|
24
|
+
*/
|
|
25
|
+
firstCharToUpper(value: string): string;
|
|
26
|
+
/**
|
|
27
|
+
* 字符串首字母小写
|
|
28
|
+
*/
|
|
29
|
+
firstCharToLower(value: string): string;
|
|
30
|
+
/**
|
|
31
|
+
* 截取指定长度的字符串
|
|
32
|
+
*/
|
|
33
|
+
subStringWithEllipsis(value: string, length: number, suffix?: string): string;
|
|
13
34
|
/**
|
|
14
35
|
* 生成随机字符串
|
|
15
36
|
*/
|
package/lib/string/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("lodash-unified"),t={getUrlParams(e){const t=/[?&][^=?&]+=[^?&]+/g,
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("lodash-unified"),t={getUrlParams(e){const t=/[?&][^=?&]+=[^?&]+/g,r={};let o;for(;null!==(o=t.exec(e));){const[e,t]=o[0].substring(1).split("=");r[e]=decodeURIComponent(t)}return r},isJson:r=>!!e.isString(r)&&(r=r.replace(/\s/g,"").replace(/\n|\r/,""),/^\{(.*?)\}$/.test(r)?/"(.*?)":(.*?)/g.test(r):!!/^\[(.*?)\]$/.test(r)&&r.replace(/^\[/,"").replace(/\]$/,"").replace(/},{/g,"}\n{").split(/\n/).map((e=>t.isJson(e))).reduce(((e,t)=>!!t))),splitCamelCase:e=>e?1===e.length?[e]:e.split(new RegExp("(?=\\p{Lu}\\p{Ll})|(?<=\\p{Ll})(?=\\p{Lu})","u")).filter((e=>e.length>0)):[],toCamelCase:e=>e?e.replace(/[-_](\w)/g,((e,t)=>t?t.toUpperCase():"")):"",firstCharToUpper:e=>e?e.charAt(0).toUpperCase()+e.slice(1):"",firstCharToLower:e=>e?e.charAt(0).toLowerCase()+e.slice(1):"",subStringWithEllipsis:(e,t,r="...")=>e?e.length>t?e.substring(0,t)+r:e:"",generateRandomString(e){const t="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";let r="";for(let o=0;o<e;o++){const e=Math.floor(62*Math.random());r+=t.charAt(e)}return r},generateUUID(){let e="";for(let t=0;t<32;t++){const r=16*Math.random()|0;8!==t&&12!==t&&16!==t&&20!==t||(e+="-"),e+=(12===t?4:16===t?3&r|8:r).toString(16)}return e},async copy(e){if("undefined"!=typeof uni)return new Promise(((t,r)=>{uni.setClipboardData({data:e,success:()=>{t()},fail:()=>{r()}})}));if((null==navigator?void 0:navigator.clipboard)&&window.isSecureContext)await navigator.clipboard.writeText(e);else{const t=document.createElement("textarea");t.value=e,t.style.position="absolute",t.style.opacity="0",t.style.left="-999999px",t.style.top="-999999px",document.body.appendChild(t),t.focus(),t.select(),document.execCommand("copy"),t.remove()}}};exports.stringUtil=t;
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/lib/string/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../../../src/string/index.ts"],"sourcesContent":["import { isString } from \"lodash-unified\";\n\n/**\n * 字符串工具类\n */\nexport const stringUtil = {\n\t/**\n\t * 获取Url参数\n\t */\n\tgetUrlParams(url: string): Record<string, any> {\n\t\tconst regex = /[?&][^=?&]+=[^?&]+/g;\n\t\tconst params: Record<string, any> = {};\n\n\t\tlet match;\n\t\twhile ((match = regex.exec(url)) !== null) {\n\t\t\tconst [key, value] = match[0].substring(1).split(\"=\");\n\t\t\tparams[key] = decodeURIComponent(value);\n\t\t}\n\n\t\treturn params;\n\t},\n\t/**\n\t * 是否为JSON字符串\n\t */\n\tisJson(value: string): boolean {\n\t\tif (!isString(value)) return false;\n\n\t\tvalue = value.replace(/\\s/g, \"\").replace(/\\n|\\r/, \"\");\n\n\t\tif (/^\\{(.*?)\\}$/.test(value)) return /\"(.*?)\":(.*?)/g.test(value);\n\n\t\tif (/^\\[(.*?)\\]$/.test(value)) {\n\t\t\treturn value\n\t\t\t\t.replace(/^\\[/, \"\")\n\t\t\t\t.replace(/\\]$/, \"\")\n\t\t\t\t.replace(/},{/g, \"}\\n{\")\n\t\t\t\t.split(/\\n/)\n\t\t\t\t.map((s) => {\n\t\t\t\t\treturn stringUtil.isJson(s);\n\t\t\t\t})\n\t\t\t\t.reduce((prev, curr) => {\n\t\t\t\t\treturn !!curr;\n\t\t\t\t});\n\t\t}\n\n\t\treturn false;\n\t},\n\t/**\n\t * 生成随机字符串\n\t */\n\tgenerateRandomString(length: number): string {\n\t\tconst characters = \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\";\n\t\tlet randomString = \"\";\n\t\tfor (let i = 0; i < length; i++) {\n\t\t\tconst randomIndex = Math.floor(Math.random() * characters.length);\n\t\t\trandomString += characters.charAt(randomIndex);\n\t\t}\n\t\treturn randomString;\n\t},\n\t/**\n\t * @description 生成唯一 uuid\n\t */\n\tgenerateUUID(): string {\n\t\tlet uuid = \"\";\n\t\tfor (let i = 0; i < 32; i++) {\n\t\t\tconst random = (Math.random() * 16) | 0;\n\t\t\tif (i === 8 || i === 12 || i === 16 || i === 20) uuid += \"-\";\n\t\t\tuuid += (i === 12 ? 4 : i === 16 ? (random & 3) | 8 : random).toString(16);\n\t\t}\n\t\treturn uuid;\n\t},\n\t/**\n\t * 复制\n\t */\n\tasync copy(value: string): Promise<void> {\n\t\tif (typeof uni !== \"undefined\") {\n\t\t\treturn new Promise((resolve, reject) => {\n\t\t\t\tuni.setClipboardData({\n\t\t\t\t\tdata: value,\n\t\t\t\t\tsuccess: () => {\n\t\t\t\t\t\tresolve();\n\t\t\t\t\t},\n\t\t\t\t\tfail: () => {\n\t\t\t\t\t\treject();\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t});\n\t\t} else {\n\t\t\t// navigator.clipboard 需要https等安全上下文\n\t\t\tif (navigator?.clipboard && window.isSecureContext) {\n\t\t\t\tawait navigator.clipboard.writeText(value);\n\t\t\t} else {\n\t\t\t\tconst textareaEl = document.createElement(\"textarea\");\n\t\t\t\ttextareaEl.value = value;\n\t\t\t\t// 使文本域不显示\n\t\t\t\ttextareaEl.style.position = \"absolute\";\n\t\t\t\ttextareaEl.style.opacity = \"0\";\n\t\t\t\ttextareaEl.style.left = \"-999999px\";\n\t\t\t\ttextareaEl.style.top = \"-999999px\";\n\t\t\t\tdocument.body.appendChild(textareaEl);\n\t\t\t\ttextareaEl.focus();\n\t\t\t\ttextareaEl.select();\n\t\t\t\tdocument.execCommand(\"copy\");\n\t\t\t\ttextareaEl.remove();\n\t\t\t}\n\t\t}\n\t},\n};\n"],"names":["stringUtil","getUrlParams","url","regex","params","match","exec","key","value","substring","split","decodeURIComponent","isJson","isString","replace","test","map","s","reduce","prev","curr","
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../../../src/string/index.ts"],"sourcesContent":["import { isString } from \"lodash-unified\";\n\n/**\n * 字符串工具类\n */\nexport const stringUtil = {\n\t/**\n\t * 获取Url参数\n\t */\n\tgetUrlParams(url: string): Record<string, any> {\n\t\tconst regex = /[?&][^=?&]+=[^?&]+/g;\n\t\tconst params: Record<string, any> = {};\n\n\t\tlet match;\n\t\twhile ((match = regex.exec(url)) !== null) {\n\t\t\tconst [key, value] = match[0].substring(1).split(\"=\");\n\t\t\tparams[key] = decodeURIComponent(value);\n\t\t}\n\n\t\treturn params;\n\t},\n\t/**\n\t * 是否为JSON字符串\n\t */\n\tisJson(value: string): boolean {\n\t\tif (!isString(value)) return false;\n\n\t\tvalue = value.replace(/\\s/g, \"\").replace(/\\n|\\r/, \"\");\n\n\t\tif (/^\\{(.*?)\\}$/.test(value)) return /\"(.*?)\":(.*?)/g.test(value);\n\n\t\tif (/^\\[(.*?)\\]$/.test(value)) {\n\t\t\treturn value\n\t\t\t\t.replace(/^\\[/, \"\")\n\t\t\t\t.replace(/\\]$/, \"\")\n\t\t\t\t.replace(/},{/g, \"}\\n{\")\n\t\t\t\t.split(/\\n/)\n\t\t\t\t.map((s) => {\n\t\t\t\t\treturn stringUtil.isJson(s);\n\t\t\t\t})\n\t\t\t\t.reduce((prev, curr) => {\n\t\t\t\t\treturn !!curr;\n\t\t\t\t});\n\t\t}\n\n\t\treturn false;\n\t},\n\t/**\n\t * 切割骆驼命名式字符串\n\t */\n\tsplitCamelCase(value: string): string[] {\n\t\tif (!value) return [];\n\n\t\tif (value.length === 1) return [value];\n\n\t\treturn value.split(/(?=\\p{Lu}\\p{Ll})|(?<=\\p{Ll})(?=\\p{Lu})/u).filter((token) => token.length > 0);\n\t},\n\t/**\n\t * 将字符串转为 camelCase 格式,支持 - 或 _ 分隔的字符串\n\t * 例如:'hello-world' 或 'hello_world' => 'helloWorld'\n\t */\n\ttoCamelCase(value: string): string {\n\t\tif (!value) return \"\";\n\t\treturn value.replace(/[-_](\\w)/g, (_, c) => (c ? c.toUpperCase() : \"\"));\n\t},\n\t/**\n\t * 字符串首字母大写\n\t */\n\tfirstCharToUpper(value: string): string {\n\t\tif (!value) return \"\";\n\t\treturn value.charAt(0).toUpperCase() + value.slice(1);\n\t},\n\t/**\n\t * 字符串首字母小写\n\t */\n\tfirstCharToLower(value: string): string {\n\t\tif (!value) return \"\";\n\t\treturn value.charAt(0).toLowerCase() + value.slice(1);\n\t},\n\t/**\n\t * 截取指定长度的字符串\n\t */\n\tsubStringWithEllipsis(value: string, length: number, suffix = \"...\"): string {\n\t\tif (!value) return \"\";\n\t\treturn value.length > length ? value.substring(0, length) + suffix : value;\n\t},\n\t/**\n\t * 生成随机字符串\n\t */\n\tgenerateRandomString(length: number): string {\n\t\tconst characters = \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\";\n\t\tlet randomString = \"\";\n\t\tfor (let i = 0; i < length; i++) {\n\t\t\tconst randomIndex = Math.floor(Math.random() * characters.length);\n\t\t\trandomString += characters.charAt(randomIndex);\n\t\t}\n\t\treturn randomString;\n\t},\n\t/**\n\t * @description 生成唯一 uuid\n\t */\n\tgenerateUUID(): string {\n\t\tlet uuid = \"\";\n\t\tfor (let i = 0; i < 32; i++) {\n\t\t\tconst random = (Math.random() * 16) | 0;\n\t\t\tif (i === 8 || i === 12 || i === 16 || i === 20) uuid += \"-\";\n\t\t\tuuid += (i === 12 ? 4 : i === 16 ? (random & 3) | 8 : random).toString(16);\n\t\t}\n\t\treturn uuid;\n\t},\n\t/**\n\t * 复制\n\t */\n\tasync copy(value: string): Promise<void> {\n\t\tif (typeof uni !== \"undefined\") {\n\t\t\treturn new Promise((resolve, reject) => {\n\t\t\t\tuni.setClipboardData({\n\t\t\t\t\tdata: value,\n\t\t\t\t\tsuccess: () => {\n\t\t\t\t\t\tresolve();\n\t\t\t\t\t},\n\t\t\t\t\tfail: () => {\n\t\t\t\t\t\treject();\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t});\n\t\t} else {\n\t\t\t// navigator.clipboard 需要https等安全上下文\n\t\t\tif (navigator?.clipboard && window.isSecureContext) {\n\t\t\t\tawait navigator.clipboard.writeText(value);\n\t\t\t} else {\n\t\t\t\tconst textareaEl = document.createElement(\"textarea\");\n\t\t\t\ttextareaEl.value = value;\n\t\t\t\t// 使文本域不显示\n\t\t\t\ttextareaEl.style.position = \"absolute\";\n\t\t\t\ttextareaEl.style.opacity = \"0\";\n\t\t\t\ttextareaEl.style.left = \"-999999px\";\n\t\t\t\ttextareaEl.style.top = \"-999999px\";\n\t\t\t\tdocument.body.appendChild(textareaEl);\n\t\t\t\ttextareaEl.focus();\n\t\t\t\ttextareaEl.select();\n\t\t\t\tdocument.execCommand(\"copy\");\n\t\t\t\ttextareaEl.remove();\n\t\t\t}\n\t\t}\n\t},\n};\n"],"names":["stringUtil","getUrlParams","url","regex","params","match","exec","key","value","substring","split","decodeURIComponent","isJson","isString","replace","test","map","s","reduce","prev","curr","splitCamelCase","length","RegExp","filter","token","toCamelCase","_","c","toUpperCase","firstCharToUpper","charAt","slice","firstCharToLower","toLowerCase","subStringWithEllipsis","suffix","generateRandomString","characters","randomString","i","randomIndex","Math","floor","random","generateUUID","uuid","toString","copy","uni","Promise","resolve","reject","setClipboardData","data","success","fail","navigator","clipboard","window","isSecureContext","writeText","textareaEl","document","createElement","style","position","opacity","left","top","body","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B,IAAK,CAChC,MAAMC,EAAcC,KAAKC,MAAsBL,GAAhBI,KAAKE,UACpBL,GAAAD,EAAWP,OAAOU,EAAW,CAEvC,OAAAF,CACR,EAIA,YAAAM,GACC,IAAIC,EAAO,GACX,IAAA,IAASN,EAAI,EAAGA,EAAI,GAAIA,IAAK,CAC5B,MAAMI,EAA0B,GAAhBF,KAAKE,SAAiB,EAC5B,IAANJ,GAAiB,KAANA,GAAkB,KAANA,GAAkB,KAANA,IAAkBM,GAAA,KAChDA,IAAM,KAANN,EAAW,EAAU,KAANA,EAAqB,EAATI,EAAc,EAAIA,GAAQG,SAAS,GAAE,CAEnE,OAAAD,CACR,EAIA,UAAME,CAAKxC,GACN,GAAe,oBAARyC,IACV,OAAO,IAAIC,SAAQ,CAACC,EAASC,KAC5BH,IAAII,iBAAiB,CACpBC,KAAM9C,EACN+C,QAAS,KACAJ,GAAA,EAETK,KAAM,KACEJ,GAAA,GAER,IAIE,IAAA,MAAAK,eAAA,EAAAA,UAAWC,YAAaC,OAAOC,sBAC5BH,UAAUC,UAAUG,UAAUrD,OAC9B,CACA,MAAAsD,EAAaC,SAASC,cAAc,YAC1CF,EAAWtD,MAAQA,EAEnBsD,EAAWG,MAAMC,SAAW,WAC5BJ,EAAWG,MAAME,QAAU,IAC3BL,EAAWG,MAAMG,KAAO,YACxBN,EAAWG,MAAMI,IAAM,YACdN,SAAAO,KAAKC,YAAYT,GAC1BA,EAAWU,QACXV,EAAWW,SACXV,SAASW,YAAY,QACrBZ,EAAWa,QAAO,CAEpB"}
|
package/lib/vue/useRender.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("vue");exports.useRender=
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("vue"),r=require("../error/index.js");exports.useRender=t=>{const n=e.getCurrentInstance();if(!n)throw new r.FastError("useRender must be called from inside a setup function");n.render=t};
|
|
2
2
|
//# sourceMappingURL=useRender.js.map
|
package/lib/vue/useRender.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useRender.js","sources":["../../../../src/vue/useRender.ts"],"sourcesContent":["import type { VNode } from \"vue\";\nimport { getCurrentInstance } from \"vue\";\n\n/**\n * 使用渲染\n * @description 为了解决使用 TSX 语法返回渲染函数,状态不会出现在 vue.js devtools 中\n */\nexport const useRender = (render: () => VNode): void => {\n\tconst vm = getCurrentInstance() as any;\n\tif (!vm) {\n\t\tthrow new
|
|
1
|
+
{"version":3,"file":"useRender.js","sources":["../../../../src/vue/useRender.ts"],"sourcesContent":["import type { VNode } from \"vue\";\nimport { getCurrentInstance } from \"vue\";\nimport { FastError } from \"../error\";\n\n/**\n * 使用渲染\n * @description 为了解决使用 TSX 语法返回渲染函数,状态不会出现在 vue.js devtools 中\n */\nexport const useRender = (render: () => VNode): void => {\n\tconst vm = getCurrentInstance() as any;\n\tif (!vm) {\n\t\tthrow new FastError(\"useRender must be called from inside a setup function\");\n\t}\n\tvm.render = render;\n};\n"],"names":["render","vm","getCurrentInstance","FastError"],"mappings":"wJAQ0BA,IACnB,MAAAC,EAAKC,EAAAA,qBACX,IAAKD,EACE,MAAA,IAAIE,YAAU,yDAErBF,EAAGD,OAASA,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fast-china/utils",
|
|
3
3
|
"author": "小方",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.5",
|
|
5
5
|
"description": "Fast 工具库.",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"keywords": [
|
|
@@ -75,20 +75,20 @@
|
|
|
75
75
|
"./lib"
|
|
76
76
|
],
|
|
77
77
|
"peerDependencies": {
|
|
78
|
-
"vue": "3.5.
|
|
78
|
+
"vue": "^3.5.0"
|
|
79
79
|
},
|
|
80
80
|
"dependencies": {
|
|
81
81
|
"@types/crypto-js": "^4.2.0",
|
|
82
|
-
"@types/lodash": "
|
|
83
|
-
"@types/lodash-es": "
|
|
84
|
-
"crypto-js": "4.2.0",
|
|
82
|
+
"@types/lodash": "latest",
|
|
83
|
+
"@types/lodash-es": "latest",
|
|
84
|
+
"crypto-js": "^4.2.0",
|
|
85
85
|
"lodash": "^4.17.21",
|
|
86
86
|
"lodash-es": "^4.17.21",
|
|
87
87
|
"lodash-unified": "^1.0.3"
|
|
88
88
|
},
|
|
89
89
|
"devDependencies": {
|
|
90
|
-
"@dcloudio/types": "
|
|
91
|
-
"@types/node": "
|
|
92
|
-
"vue": "3.5.
|
|
90
|
+
"@dcloudio/types": "latest",
|
|
91
|
+
"@types/node": "latest",
|
|
92
|
+
"vue": "^3.5.0"
|
|
93
93
|
}
|
|
94
94
|
}
|