@wapri/gbase 0.0.0-stage → 1.0.1-beta.0

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/os.d.ts ADDED
@@ -0,0 +1,36 @@
1
+ import type { OSType } from './types/os';
2
+ declare const browserList: {
3
+ wechat: RegExp;
4
+ alipay: RegExp;
5
+ qq: RegExp;
6
+ weibo: RegExp;
7
+ tiktok: RegExp;
8
+ baidu: RegExp;
9
+ douban: RegExp;
10
+ xiaomi: RegExp;
11
+ uc: RegExp;
12
+ quark: RegExp;
13
+ ie: RegExp;
14
+ chrome: RegExp;
15
+ safari: RegExp;
16
+ edge: RegExp;
17
+ firefox: RegExp;
18
+ opera: RegExp;
19
+ };
20
+ type BrwoserType = keyof typeof browserList;
21
+ export declare const os: {
22
+ isPC: (ua?: string) => boolean;
23
+ isMobile: (ua?: string) => boolean;
24
+ isIpad: () => boolean;
25
+ isIOS: (ua?: string) => boolean;
26
+ isAndroid: (ua?: string) => boolean;
27
+ isHarmonyOS: (ua?: string) => boolean;
28
+ isHarmonyOSNext: (ua?: string) => boolean;
29
+ isHarmonyOSLegacy: (ua?: string) => boolean;
30
+ isWeixin: (ua?: string) => boolean;
31
+ isInWebview: (ua?: string) => boolean;
32
+ getBrwoserType: (ua?: string) => BrwoserType | 'unknown';
33
+ getOS: () => OSType | undefined;
34
+ getUA: () => string;
35
+ };
36
+ export {};
package/dist/os.js ADDED
@@ -0,0 +1,143 @@
1
+ import { regexpArkWeb, regexpHarmonyOS, regexpHarmonyOSNext } from "./constant/os";
2
+ /* eslint-disable no-useless-escape */
3
+ var browserList = {
4
+ // app内
5
+ wechat: /micromessenger\/([\d\.]+)/i,
6
+ alipay: /alipay/i,
7
+ qq: /qq\/([\d\.]+)/i,
8
+ weibo: /\bweibo\b/i,
9
+ tiktok: /aweme/i,
10
+ // app游览器
11
+ baidu: / (baiduboxapp|biduboxapp)\//i,
12
+ douban: /com\.douban\.frodo\/([\d\.]+)/i,
13
+ xiaomi: /miuibrowser/i,
14
+ uc: /ucbrowser/i,
15
+ quark: /quark/i,
16
+ // 游览器
17
+ ie: /(msie\s([\d.]+)|(trident\/\d\.\d))/i,
18
+ chrome: /(crios|chrome)\/([\d\.]+)/i,
19
+ safari: /safari\/([\d\.]+)/i,
20
+ edge: /edge\/([\d\.]+)/i,
21
+ firefox: /firefox\/([\d\.]+)/i,
22
+ opera: /(?:opera|opr).([\d\.]+)/i
23
+ };
24
+ var getUA = function getUA() {
25
+ var _navigator$userAgent$, _navigator;
26
+ return (_navigator$userAgent$ = (_navigator = navigator) === null || _navigator === void 0 ? void 0 : _navigator.userAgent.toLocaleLowerCase()) !== null && _navigator$userAgent$ !== void 0 ? _navigator$userAgent$ : '';
27
+ };
28
+ var isIOS = function isIOS() {
29
+ var ua = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : getUA();
30
+ return /iphone|ipad|ipod|ios/i.test(ua);
31
+ };
32
+ var isIpad = function isIpad() {
33
+ var platform = navigator.platform === 'MacIntel' || navigator.platform === 'iPad';
34
+ return platform && navigator.maxTouchPoints > 1 || navigator.userAgent.match(/iPad/i) != null;
35
+ };
36
+
37
+ /**
38
+ * 是否为 Android
39
+ *
40
+ * 旧版鸿蒙(HarmonyOS 4 及以前)兼容 AOSP,其 UA 中同时包含 `Android`,因此这里会返回 `true`;
41
+ * 若需要区分两者,请优先用 `isHarmonyOS()` 判断。
42
+ */
43
+ var isAndroid = function isAndroid() {
44
+ var ua = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : getUA();
45
+ return /android/i.test(ua);
46
+ };
47
+
48
+ /**
49
+ * 是否为鸿蒙系统(广义)
50
+ *
51
+ * 覆盖两类:
52
+ * - 纯血鸿蒙 HarmonyOS NEXT:UA 含 `OpenHarmony` / `ArkWeb`,不含 `Android`
53
+ * - 旧版鸿蒙 HarmonyOS 4 及以前:UA 含 `HarmonyOS` / `HMSCore`,同时含 `Android`
54
+ */
55
+ var isHarmonyOS = function isHarmonyOS() {
56
+ var ua = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : getUA();
57
+ return regexpHarmonyOS().test(ua);
58
+ };
59
+
60
+ /**
61
+ * 是否为纯血鸿蒙(HarmonyOS NEXT,5.x)
62
+ *
63
+ * UA 含 `OpenHarmony` 或 `Harmony/x.y`,且不再是 AOSP 兼容架构,不含 `Android`。
64
+ * 需要与旧版鸿蒙区分时使用。
65
+ */
66
+ var isHarmonyOSNext = function isHarmonyOSNext() {
67
+ var ua = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : getUA();
68
+ if (/android/i.test(ua)) return false;
69
+ return regexpHarmonyOSNext().test(ua) || regexpArkWeb().test(ua);
70
+ };
71
+
72
+ /**
73
+ * 是否为旧版鸿蒙(HarmonyOS 4 及以前,兼容 AOSP)
74
+ *
75
+ * UA 同时包含 `Android` 与鸿蒙标识,可正常安装 Android APK。
76
+ */
77
+ var isHarmonyOSLegacy = function isHarmonyOSLegacy() {
78
+ var ua = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : getUA();
79
+ return /android/i.test(ua) && regexpHarmonyOS().test(ua);
80
+ };
81
+ var isPC = function isPC() {
82
+ var ua = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : getUA();
83
+ return !ua.match(/android|iphone|ipad|pod/i) && !isHarmonyOS(ua) && !isIpad();
84
+ };
85
+ var isMobile = function isMobile() {
86
+ var ua = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : getUA();
87
+ return /iphone|ipad|ipod|ios|android|mobile/i.test(ua) || isHarmonyOS(ua) || isIpad();
88
+ };
89
+
90
+ /** 是否在微信内置浏览器中(UA 含 micromessenger),常用于 App Scheme 唤起等能力的降级判断 */
91
+ var isWeixin = function isWeixin() {
92
+ var ua = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : getUA();
93
+ return browserList.wechat.test(ua);
94
+ };
95
+
96
+ // WqmtClient uniwebview https://docs.uniwebview.com/
97
+ // DeutolsWebSDK sdk-webview https://developer.papegames.com/docs/sdk/sdk_webview/
98
+ // UnrealEngine|PlayStation x6判断
99
+ var isInWebview = function isInWebview() {
100
+ var ua = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : getUA();
101
+ return /Client|DeutolsWebSDK|UnrealEngine|PlayStation/i.test(ua);
102
+ };
103
+
104
+ /**
105
+ * 获取操作系统类型
106
+ *
107
+ * 鸿蒙必须最先判断:旧版鸿蒙兼容 AOSP,其 appVersion 与 UA 中同时含 `Linux` 和 `Android`,
108
+ * 若先匹配 `linux` 分支,鸿蒙会被吞成 `linux`,新增的 `harmonyos` 分支永远走不到。
109
+ */
110
+ var getOS = function getOS() {
111
+ var userAgent = getUA();
112
+ var appVersion = 'navigator' in window && 'appVersion' in navigator && navigator.appVersion.toLowerCase() || '';
113
+ if (isHarmonyOS(userAgent)) return 'harmonyos';
114
+ if (/mac/i.test(appVersion)) return 'mac';
115
+ if (/win/i.test(appVersion)) return 'windows';
116
+ if (/linux/i.test(appVersion)) return 'linux';
117
+ if (/iphone/i.test(userAgent) || /ipad/i.test(userAgent) || /ipod/i.test(userAgent)) return 'ios';
118
+ if (/android/i.test(userAgent)) return 'android';
119
+ };
120
+ var getBrwoserType = function getBrwoserType() {
121
+ var ua = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : getUA();
122
+ for (var i in browserList) {
123
+ if (ua.match(browserList[i])) {
124
+ return i;
125
+ }
126
+ }
127
+ return 'unknown';
128
+ };
129
+ export var os = {
130
+ isPC: isPC,
131
+ isMobile: isMobile,
132
+ isIpad: isIpad,
133
+ isIOS: isIOS,
134
+ isAndroid: isAndroid,
135
+ isHarmonyOS: isHarmonyOS,
136
+ isHarmonyOSNext: isHarmonyOSNext,
137
+ isHarmonyOSLegacy: isHarmonyOSLegacy,
138
+ isWeixin: isWeixin,
139
+ isInWebview: isInWebview,
140
+ getBrwoserType: getBrwoserType,
141
+ getOS: getOS,
142
+ getUA: getUA
143
+ };
@@ -0,0 +1,10 @@
1
+ /** 业务平台标识,同时用于账号中心 URL 的 platform 参数。 */
2
+ export declare const platformType: {
3
+ readonly KAIMAI_ADMIN: "kaimai-admin";
4
+ readonly KAIMAI_MERCHANT: "kaimai-merchant";
5
+ readonly MAITIAN: "maitian";
6
+ };
7
+ export type PlatformType = (typeof platformType)[keyof typeof platformType];
8
+ export declare const platformTypes: PlatformType[];
9
+ /** 判断值是否为受支持的平台标识。 */
10
+ export declare const isPlatformType: (value: unknown) => value is PlatformType;
@@ -0,0 +1,12 @@
1
+ /** 业务平台标识,同时用于账号中心 URL 的 platform 参数。 */
2
+ export var platformType = {
3
+ KAIMAI_ADMIN: 'kaimai-admin',
4
+ KAIMAI_MERCHANT: 'kaimai-merchant',
5
+ MAITIAN: 'maitian'
6
+ };
7
+ export var platformTypes = Object.values(platformType);
8
+
9
+ /** 判断值是否为受支持的平台标识。 */
10
+ export var isPlatformType = function isPlatformType(value) {
11
+ return typeof value === 'string' && platformTypes.includes(value);
12
+ };
@@ -0,0 +1,12 @@
1
+ /**
2
+ * 校验邮箱
3
+ * 正则构造或匹配失败(如浏览器不支持对应语法)时返回 false,不向上抛错
4
+ * @param value 待校验的值
5
+ */
6
+ export declare const isEmail: (value?: string | null) => boolean;
7
+ /**
8
+ * 校验手机号(中国大陆 11 位)
9
+ * 正则构造或匹配失败(如浏览器不支持对应语法)时返回 false,不向上抛错
10
+ * @param value 待校验的值
11
+ */
12
+ export declare const isPhone: (value?: string | null) => boolean;
package/dist/regexp.js ADDED
@@ -0,0 +1,27 @@
1
+ import { regexpEmail, regexpPhone } from "./constant/regexp";
2
+
3
+ /**
4
+ * 校验邮箱
5
+ * 正则构造或匹配失败(如浏览器不支持对应语法)时返回 false,不向上抛错
6
+ * @param value 待校验的值
7
+ */
8
+ export var isEmail = function isEmail(value) {
9
+ try {
10
+ return regexpEmail().test(value !== null && value !== void 0 ? value : '');
11
+ } catch (_unused) {
12
+ return false;
13
+ }
14
+ };
15
+
16
+ /**
17
+ * 校验手机号(中国大陆 11 位)
18
+ * 正则构造或匹配失败(如浏览器不支持对应语法)时返回 false,不向上抛错
19
+ * @param value 待校验的值
20
+ */
21
+ export var isPhone = function isPhone(value) {
22
+ try {
23
+ return regexpPhone().test(value !== null && value !== void 0 ? value : '');
24
+ } catch (_unused2) {
25
+ return false;
26
+ }
27
+ };
package/dist/size.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare const getSize: () => string | undefined;
package/dist/size.js ADDED
@@ -0,0 +1,4 @@
1
+ import { default as Cookies } from 'js-cookie';
2
+ export var getSize = function getSize() {
3
+ return Cookies.get('size');
4
+ };
@@ -0,0 +1 @@
1
+ export declare const sleep: (ms: number) => Promise<unknown>;
package/dist/sleep.js ADDED
@@ -0,0 +1,22 @@
1
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
+ function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
3
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
4
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
5
+ export var sleep = /*#__PURE__*/function () {
6
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(ms) {
7
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
8
+ while (1) switch (_context.prev = _context.next) {
9
+ case 0:
10
+ return _context.abrupt("return", new Promise(function (resolve) {
11
+ setTimeout(resolve, ms);
12
+ }));
13
+ case 1:
14
+ case "end":
15
+ return _context.stop();
16
+ }
17
+ }, _callee);
18
+ }));
19
+ return function sleep(_x) {
20
+ return _ref.apply(this, arguments);
21
+ };
22
+ }();
@@ -0,0 +1,18 @@
1
+ /**
2
+ * 在 App 壳未提供浏览器存储时,回退到内存存储,避免读写时抛出异常。
3
+ */
4
+ export type StorageType = 'localStorage' | 'sessionStorage';
5
+ export declare class MyStorage implements Pick<Storage, 'setItem' | 'getItem' | 'clear' | 'removeItem'> {
6
+ readonly type: StorageType;
7
+ private readonly memory;
8
+ readonly noSupportLS: boolean;
9
+ constructor(type: StorageType);
10
+ getItem(key: string): string | null;
11
+ setItem(key: string, value: string): void;
12
+ clear(): void;
13
+ removeItem(key: string): void;
14
+ private get storage();
15
+ }
16
+ export declare const storage: MyStorage;
17
+ export declare const session: MyStorage;
18
+ export default storage;
@@ -0,0 +1,124 @@
1
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
3
+ function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
4
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
5
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
7
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
8
+ /**
9
+ * 在 App 壳未提供浏览器存储时,回退到内存存储,避免读写时抛出异常。
10
+ */
11
+
12
+ var noSupportLS = function noSupportLS(type) {
13
+ try {
14
+ var _storage = globalThis[type];
15
+ _storage.setItem('_kaimai-test-key', 'miao');
16
+ _storage.getItem('_kaimai-test-key');
17
+ _storage.removeItem('_kaimai-test-key');
18
+ return false;
19
+ } catch (_unused) {
20
+ return true;
21
+ }
22
+ };
23
+ var MemoryStorage = /*#__PURE__*/function () {
24
+ function MemoryStorage() {
25
+ _classCallCheck(this, MemoryStorage);
26
+ _defineProperty(this, "cache", new Map());
27
+ }
28
+ _createClass(MemoryStorage, [{
29
+ key: "setItem",
30
+ value: function setItem(key, value) {
31
+ this.cache.set(key, value);
32
+ }
33
+ }, {
34
+ key: "getItem",
35
+ value: function getItem(key) {
36
+ var _this$cache$get;
37
+ return (_this$cache$get = this.cache.get(key)) !== null && _this$cache$get !== void 0 ? _this$cache$get : null;
38
+ }
39
+ }, {
40
+ key: "clear",
41
+ value: function clear() {
42
+ this.cache.clear();
43
+ }
44
+ }, {
45
+ key: "removeItem",
46
+ value: function removeItem(key) {
47
+ this.cache.delete(key);
48
+ }
49
+ }]);
50
+ return MemoryStorage;
51
+ }();
52
+ export var MyStorage = /*#__PURE__*/function () {
53
+ function MyStorage(type) {
54
+ _classCallCheck(this, MyStorage);
55
+ _defineProperty(this, "memory", new MemoryStorage());
56
+ _defineProperty(this, "noSupportLS", void 0);
57
+ this.type = type;
58
+ this.noSupportLS = noSupportLS(type);
59
+ }
60
+ _createClass(MyStorage, [{
61
+ key: "getItem",
62
+ value: function getItem(key) {
63
+ if (this.noSupportLS) {
64
+ return this.memory.getItem(key);
65
+ }
66
+ try {
67
+ return this.storage.getItem(key);
68
+ } catch (_unused2) {
69
+ return this.memory.getItem(key);
70
+ }
71
+ }
72
+ }, {
73
+ key: "setItem",
74
+ value: function setItem(key, value) {
75
+ if (this.noSupportLS) {
76
+ this.memory.setItem(key, value);
77
+ return;
78
+ }
79
+ try {
80
+ this.storage.setItem(key, value);
81
+ this.memory.removeItem(key);
82
+ } catch (_unused3) {
83
+ this.memory.setItem(key, value);
84
+ }
85
+ }
86
+ }, {
87
+ key: "clear",
88
+ value: function clear() {
89
+ if (this.noSupportLS) {
90
+ this.memory.clear();
91
+ return;
92
+ }
93
+ try {
94
+ this.storage.clear();
95
+ this.memory.clear();
96
+ } catch (_unused4) {
97
+ this.memory.clear();
98
+ }
99
+ }
100
+ }, {
101
+ key: "removeItem",
102
+ value: function removeItem(key) {
103
+ if (this.noSupportLS) {
104
+ this.memory.removeItem(key);
105
+ return;
106
+ }
107
+ try {
108
+ this.storage.removeItem(key);
109
+ this.memory.removeItem(key);
110
+ } catch (_unused5) {
111
+ this.memory.removeItem(key);
112
+ }
113
+ }
114
+ }, {
115
+ key: "storage",
116
+ get: function get() {
117
+ return globalThis[this.type];
118
+ }
119
+ }]);
120
+ return MyStorage;
121
+ }();
122
+ export var storage = new MyStorage('localStorage');
123
+ export var session = new MyStorage('sessionStorage');
124
+ export default storage;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * 操作系统类型
3
+ * 与 `os.getOS()` 的返回值保持一致
4
+ */
5
+ export type OSType = 'mac' | 'windows' | 'linux' | 'ios' | 'android' | 'harmonyos';
@@ -0,0 +1 @@
1
+ export {};
package/dist/url.d.ts ADDED
@@ -0,0 +1,18 @@
1
+ export declare const obj2url: (obj: any) => string;
2
+ export declare function getUrlParam(key: string, params?: string): string;
3
+ export declare function getUrlParam(): Record<string, string>;
4
+ export declare const removeUrlParams: (keys: string[]) => void;
5
+ export type UrlParamValue = any;
6
+ export type UrlParams = Record<string, UrlParamValue>;
7
+ /**
8
+ * 通过 history.replaceState 修改地址栏参数,不触发页面刷新。
9
+ *
10
+ * - 同名参数以传入值为准,未传入的原有参数保持不变
11
+ * - 值为 `null` / `undefined` / `''` 时删除该参数
12
+ * - hash 路由(`/#/home?page=1`)时参数写在 hash 内
13
+ *
14
+ * @param params 需要合并进地址的参数
15
+ * @param url 目标地址,默认为当前页面地址
16
+ * @returns 替换后的完整地址
17
+ */
18
+ export declare function replaceUrl(params: UrlParams, url?: string): string;
package/dist/url.js ADDED
@@ -0,0 +1,111 @@
1
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
2
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
3
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
4
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
5
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
6
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
7
+ import { isEmpty } from 'lodash-es';
8
+ export var obj2url = function obj2url(obj) {
9
+ var urls = [];
10
+ for (var k in obj) {
11
+ if (Object.prototype.hasOwnProperty.call(obj, k)) {
12
+ var value = obj[k];
13
+ if (value === '' || value === undefined || value === null) {
14
+ continue;
15
+ }
16
+ if (_typeof(value) === 'object') {
17
+ value = JSON.stringify(value);
18
+ }
19
+ value = encodeURIComponent(value);
20
+ urls.push("".concat(k, "=").concat(value));
21
+ }
22
+ }
23
+ var str = urls.join('&');
24
+ return str;
25
+ };
26
+ var parse = function parse(url) {
27
+ var reg = /([^?=&]+)=([^?=&]+)/g;
28
+ var arr = url.match(reg) || [];
29
+ var obj = {};
30
+ arr.forEach(function (item) {
31
+ var res = item.split('=');
32
+ obj[res[0]] = ['undefined', 'null'].includes(res[1]) ? '' : decodeURIComponent(res[1]);
33
+ });
34
+ return obj;
35
+ };
36
+ export function getUrlParam(key, params) {
37
+ if (!params) {
38
+ var url = window.location.search || window.location.hash;
39
+ if (url.indexOf('?') !== -1) {
40
+ params = url;
41
+ }
42
+ if (!params) {
43
+ return key ? undefined : {};
44
+ }
45
+ }
46
+ var index = params.indexOf('?');
47
+ if (index > -1) {
48
+ params = params.slice(1 + index);
49
+ }
50
+ var obj = parse(params);
51
+ return key ? obj[key] : obj;
52
+ }
53
+ export var removeUrlParams = function removeUrlParams(keys) {
54
+ var urlParams = getUrlParam();
55
+ keys.forEach(function (i) {
56
+ return delete urlParams[i];
57
+ });
58
+ var searchStr = obj2url(urlParams);
59
+ var search = isEmpty(searchStr) ? '' : "?".concat(obj2url(urlParams));
60
+ history.replaceState(null, '', "".concat(location.href.replace(location.search, search)));
61
+ };
62
+ /** 拆出地址的 base(不含 query)与 query 串,兼容 hash 路由(如 `/#/home?page=1`)。 */
63
+ var splitUrl = function splitUrl(href) {
64
+ var hashIndex = href.indexOf('#');
65
+ if (hashIndex > -1) {
66
+ var hash = href.slice(hashIndex);
67
+ var hashQueryIndex = hash.indexOf('?');
68
+ if (hashQueryIndex > -1) {
69
+ return {
70
+ base: href.slice(0, hashIndex) + hash.slice(0, hashQueryIndex),
71
+ query: hash.slice(hashQueryIndex)
72
+ };
73
+ }
74
+ }
75
+ var queryIndex = href.indexOf('?');
76
+ if (queryIndex > -1) {
77
+ return {
78
+ base: href.slice(0, queryIndex),
79
+ query: href.slice(queryIndex)
80
+ };
81
+ }
82
+ return {
83
+ base: href,
84
+ query: ''
85
+ };
86
+ };
87
+
88
+ /**
89
+ * 通过 history.replaceState 修改地址栏参数,不触发页面刷新。
90
+ *
91
+ * - 同名参数以传入值为准,未传入的原有参数保持不变
92
+ * - 值为 `null` / `undefined` / `''` 时删除该参数
93
+ * - hash 路由(`/#/home?page=1`)时参数写在 hash 内
94
+ *
95
+ * @param params 需要合并进地址的参数
96
+ * @param url 目标地址,默认为当前页面地址
97
+ * @returns 替换后的完整地址
98
+ */
99
+ export function replaceUrl(params, url) {
100
+ var _window$history;
101
+ var href = url || (typeof window === 'undefined' ? '' : window.location.href);
102
+ var _splitUrl = splitUrl(href),
103
+ base = _splitUrl.base,
104
+ query = _splitUrl.query;
105
+ var searchStr = obj2url(_objectSpread(_objectSpread({}, parse(query)), params));
106
+ var newUrl = searchStr ? "".concat(base, "?").concat(searchStr) : base;
107
+ if (typeof window !== 'undefined' && typeof ((_window$history = window.history) === null || _window$history === void 0 ? void 0 : _window$history.replaceState) === 'function') {
108
+ window.history.replaceState(window.history.state, '', newUrl);
109
+ }
110
+ return newUrl;
111
+ }
package/dist/uuid.d.ts ADDED
@@ -0,0 +1 @@
1
+ export { nanoid as uuid } from 'nanoid';
package/dist/uuid.js ADDED
@@ -0,0 +1 @@
1
+ export { nanoid as uuid } from 'nanoid';
package/package.json CHANGED
@@ -1,6 +1,36 @@
1
1
  {
2
2
  "name": "@wapri/gbase",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "1.0.1-beta.0",
4
+ "description": "前端基础公共库",
5
+ "keywords": [],
6
+ "license": "UNLICENSED",
7
+ "author": "wa",
8
+ "sideEffects": false,
9
+ "module": "dist/index.js",
10
+ "types": "dist/index.d.ts",
11
+ "files": [
12
+ "dist"
13
+ ],
14
+ "dependencies": {
15
+ "copy-to-clipboard": "^4.0.2",
16
+ "currency.js": "^2.0.4",
17
+ "dayjs": "^1.11.9",
18
+ "js-cookie": "^3.0.8",
19
+ "lodash-es": "^4.17.21",
20
+ "mitt": "^3.0.1",
21
+ "nanoid": "^5.1.5"
22
+ },
23
+ "devDependencies": {
24
+ "@types/js-cookie": "^3.0.6",
25
+ "@types/lodash-es": "^4.17.12"
26
+ },
27
+ "publishConfig": {
28
+ "access": "public",
29
+ "registry": "https://registry.npmjs.org/"
30
+ },
31
+ "peerDependenciesMeta": {},
32
+ "scripts": {
33
+ "build": "father build",
34
+ "dev": "father dev"
35
+ }
6
36
  }