@deot/helper-utils 1.1.3 → 1.1.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/README.md +23 -22
- package/dist/index.cjs +25 -27
- package/dist/index.d.ts +3 -5
- package/dist/index.iife.js +25 -27
- package/dist/index.js +25 -27
- package/dist/index.umd.cjs +25 -27
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -3,33 +3,34 @@
|
|
|
3
3
|
### `Utils`
|
|
4
4
|
```js
|
|
5
5
|
import * as Utils from '@deot/helper-utils';
|
|
6
|
+
```
|
|
6
7
|
|
|
8
|
+
```js
|
|
7
9
|
// or
|
|
8
10
|
import { Utils } from '@deot/helper';
|
|
9
|
-
|
|
10
11
|
```
|
|
11
12
|
|
|
12
13
|
| 方法 | 说明 |
|
|
13
14
|
| ------------------------------------------------------------------ | ------------------------------ |
|
|
14
|
-
| [asterisk]
|
|
15
|
-
| [autoCatch]
|
|
16
|
-
| [canvasToImage]
|
|
17
|
-
| [cloneDeepEasier]
|
|
18
|
-
| [compressImage]
|
|
19
|
-
| [dataURLToFile]
|
|
20
|
-
| [debounce]
|
|
21
|
-
| [def]
|
|
22
|
-
| [flattenDecodeURIComponent]
|
|
23
|
-
| [flattenJSONParse]
|
|
24
|
-
| [getPropByPath]
|
|
25
|
-
| [getUid]
|
|
26
|
-
| [hasOwn]
|
|
27
|
-
| [numberToUnit]
|
|
28
|
-
| [preZero]
|
|
29
|
-
| [raf]
|
|
30
|
-
| [random]
|
|
31
|
-
| [sleep]
|
|
32
|
-
| [throttle]
|
|
33
|
-
| [genterateString]
|
|
34
|
-
| [toPromise]
|
|
15
|
+
| [asterisk](./src/asterisk.ts) | 文字加`*` |
|
|
16
|
+
| [autoCatch](./src/auto-catch.ts) | 捕获异常 |
|
|
17
|
+
| [canvasToImage](./src/canvas-to-image.ts) | 转图片信息 |
|
|
18
|
+
| [cloneDeepEasier](./src/clone-deep-easier.ts) | 深拷贝 |
|
|
19
|
+
| [compressImage](./src/compress-image.ts) | 压缩图片 |
|
|
20
|
+
| [dataURLToFile](./src/dataURL-to-file.ts) | base64转文件 |
|
|
21
|
+
| [debounce](./src/debounce.ts) | 防抖 |
|
|
22
|
+
| [def](./src/def.ts) | 定义 |
|
|
23
|
+
| [flattenDecodeURIComponent](./src/flatten-decode-uri-component.ts) | 深度解码 |
|
|
24
|
+
| [flattenJSONParse](./src/flatten-json-parse.ts) | 深度反序列化 |
|
|
25
|
+
| [getPropByPath](./src/get-prop-by-path.ts) | 根据路径找值 |
|
|
26
|
+
| [getUid](./src/get-uid.ts) | 生成唯一id |
|
|
27
|
+
| [hasOwn](./src/has-own.ts) | 是否在原型上 |
|
|
28
|
+
| [numberToUnit](./src/number-to-unit.ts) | 大数据转成中文单位 |
|
|
29
|
+
| [preZero](./src/pre-zero.ts) | 加零 |
|
|
30
|
+
| [raf](./src/raf.ts) | raf |
|
|
31
|
+
| [random](./src/random.ts) | `range` 和 `probs` |
|
|
32
|
+
| [sleep](./src/sleep.ts) | 等待 |
|
|
33
|
+
| [throttle](./src/throttle.ts) | 节流 |
|
|
34
|
+
| [genterateString](./src/genterate-string.ts) | 随机生成指定长度的字符串 |
|
|
35
|
+
| [toPromise](./src/to-promise.ts) | 让返回对象可操作且能直接使用`await instance` |
|
|
35
36
|
|
package/dist/index.cjs
CHANGED
|
@@ -39,11 +39,11 @@ const dataURLToFile = (dataURL, filename, filetype) => {
|
|
|
39
39
|
if (!hasPrefix) {
|
|
40
40
|
dataURL = `data:${filetype || getMime(filename)};base64,${dataURL}`;
|
|
41
41
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
42
|
+
const [suffix, dataURL$] = dataURL.split(",");
|
|
43
|
+
const mime = suffix.match(/:(.*?);/)?.[1];
|
|
44
|
+
const bstr = window.atob(dataURL$);
|
|
45
45
|
let n = bstr.length;
|
|
46
|
-
|
|
46
|
+
const u8arr = new Uint8Array(n);
|
|
47
47
|
while (n--) {
|
|
48
48
|
u8arr[n] = bstr.charCodeAt(n);
|
|
49
49
|
}
|
|
@@ -53,7 +53,7 @@ const dataURLToFile = (dataURL, filename, filetype) => {
|
|
|
53
53
|
const canvasToImage = (canvas, filename) => {
|
|
54
54
|
const dataURL = canvas.toDataURL("image/png");
|
|
55
55
|
return new Promise((resolve) => {
|
|
56
|
-
|
|
56
|
+
const result = {
|
|
57
57
|
dataURL
|
|
58
58
|
};
|
|
59
59
|
if (typeof filename === "string") {
|
|
@@ -113,11 +113,11 @@ const debounce = (original, wait, options) => {
|
|
|
113
113
|
const { leading, trailing = true, throttle } = options || {};
|
|
114
114
|
let timer;
|
|
115
115
|
let invoke;
|
|
116
|
-
|
|
116
|
+
const cancel = () => {
|
|
117
117
|
timer && clearTimeout(timer);
|
|
118
118
|
timer = null;
|
|
119
119
|
};
|
|
120
|
-
|
|
120
|
+
const start = () => {
|
|
121
121
|
timer = setTimeout(() => {
|
|
122
122
|
timer = null;
|
|
123
123
|
trailing && invoke && invoke();
|
|
@@ -128,8 +128,7 @@ const debounce = (original, wait, options) => {
|
|
|
128
128
|
original.apply(this, args);
|
|
129
129
|
invoke = null;
|
|
130
130
|
};
|
|
131
|
-
if (!wait && throttle)
|
|
132
|
-
return invoke();
|
|
131
|
+
if (!wait && throttle) return invoke();
|
|
133
132
|
if (!timer) {
|
|
134
133
|
leading && invoke();
|
|
135
134
|
start();
|
|
@@ -163,17 +162,17 @@ const getPropByPath = (target, path) => {
|
|
|
163
162
|
let o = target;
|
|
164
163
|
path = path.replace(/\[(\w+)\]/g, ".$1");
|
|
165
164
|
path = path.replace(/^\./, "");
|
|
166
|
-
|
|
165
|
+
const keyArr = path.split(".");
|
|
167
166
|
let i = 0;
|
|
168
167
|
for (let len = keyArr.length; i < len - 1; ++i) {
|
|
169
|
-
|
|
168
|
+
const key = keyArr[i];
|
|
170
169
|
try {
|
|
171
170
|
if (key in o) {
|
|
172
171
|
o = o[key];
|
|
173
172
|
} else {
|
|
174
173
|
throw new Error();
|
|
175
174
|
}
|
|
176
|
-
} catch {
|
|
175
|
+
} catch (_) {
|
|
177
176
|
throw new Error("无效路径!");
|
|
178
177
|
}
|
|
179
178
|
}
|
|
@@ -201,7 +200,7 @@ const getDigit = (integer) => {
|
|
|
201
200
|
return digit;
|
|
202
201
|
};
|
|
203
202
|
const addWan = (integer, num, mutiple, decimalDigit) => {
|
|
204
|
-
|
|
203
|
+
const digit = getDigit(integer);
|
|
205
204
|
if (digit > 3) {
|
|
206
205
|
let remainder = digit % 8;
|
|
207
206
|
if (remainder >= 5) {
|
|
@@ -217,11 +216,11 @@ const numberToUnit = (number, decimalDigit) => {
|
|
|
217
216
|
number = Number.MAX_SAFE_INTEGER;
|
|
218
217
|
}
|
|
219
218
|
decimalDigit = decimalDigit == null ? 2 : decimalDigit;
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
219
|
+
const integer = Math.floor(number);
|
|
220
|
+
const digit = getDigit(integer);
|
|
221
|
+
const unit = [];
|
|
223
222
|
if (digit > 3) {
|
|
224
|
-
|
|
223
|
+
const multiple = Math.floor(digit / 8);
|
|
225
224
|
if (multiple >= 1) {
|
|
226
225
|
let tmp = Math.round(integer / Math.pow(10, 8 * multiple));
|
|
227
226
|
unit.push(addWan(tmp, number, 8 * multiple, decimalDigit));
|
|
@@ -252,11 +251,11 @@ const range = (min, max) => {
|
|
|
252
251
|
return Math.floor(Math.random() * (max - min + 1) + min);
|
|
253
252
|
};
|
|
254
253
|
const probs = (weights) => {
|
|
255
|
-
|
|
254
|
+
const sum = weights.reduce((pre, cur) => pre + (!cur || cur <= 0 ? 0 : cur), 0);
|
|
256
255
|
if (!sum) {
|
|
257
256
|
throw new Error("不可能得到索引值");
|
|
258
257
|
}
|
|
259
|
-
|
|
258
|
+
const probs$ = weights.map((i) => i / sum);
|
|
260
259
|
let index = 0;
|
|
261
260
|
let r = Math.random();
|
|
262
261
|
while (r > 0) {
|
|
@@ -275,7 +274,7 @@ const sleep = (wait, immediate) => {
|
|
|
275
274
|
duration = wait || 0;
|
|
276
275
|
}
|
|
277
276
|
return new Promise((r) => {
|
|
278
|
-
|
|
277
|
+
const timer = setTimeout(r, duration);
|
|
279
278
|
immediate && immediate(timer, duration, r);
|
|
280
279
|
});
|
|
281
280
|
};
|
|
@@ -293,7 +292,7 @@ const throttle = (original, wait, options) => {
|
|
|
293
292
|
);
|
|
294
293
|
};
|
|
295
294
|
|
|
296
|
-
const flatten = (value, parser) => {
|
|
295
|
+
const flatten = (value, parser, exit) => {
|
|
297
296
|
let need = true;
|
|
298
297
|
let safeCount = 1;
|
|
299
298
|
let parseValue = value;
|
|
@@ -302,12 +301,12 @@ const flatten = (value, parser) => {
|
|
|
302
301
|
throw new Error(value);
|
|
303
302
|
}
|
|
304
303
|
try {
|
|
305
|
-
|
|
306
|
-
if (parseValue === next) {
|
|
304
|
+
const next = (parser || decodeURIComponent)(parseValue);
|
|
305
|
+
if (parseValue === next || typeof exit === "function" && exit(next)) {
|
|
307
306
|
need = false;
|
|
308
307
|
}
|
|
309
308
|
parseValue = next;
|
|
310
|
-
} catch {
|
|
309
|
+
} catch (_) {
|
|
311
310
|
need = false;
|
|
312
311
|
}
|
|
313
312
|
safeCount++;
|
|
@@ -316,9 +315,8 @@ const flatten = (value, parser) => {
|
|
|
316
315
|
};
|
|
317
316
|
|
|
318
317
|
const flattenJSONParse = (value) => {
|
|
319
|
-
if (value === null)
|
|
320
|
-
|
|
321
|
-
let regex = /^\d+$/;
|
|
318
|
+
if (value === null) return null;
|
|
319
|
+
const regex = /^\d+$/;
|
|
322
320
|
if (regex.test(value) && value.length >= 16 && +value > Number.MAX_SAFE_INTEGER) {
|
|
323
321
|
return value;
|
|
324
322
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
|
|
3
1
|
export declare const asterisk: (value: string, from?: number, length?: number) => string;
|
|
4
2
|
|
|
5
3
|
export declare const autoCatch: (impl: any, options?: Record<string, any>) => Promise<any>;
|
|
@@ -30,7 +28,7 @@ export declare const debounce: (original: Function, wait?: number, options?: Opt
|
|
|
30
28
|
|
|
31
29
|
export declare const def: <T = object>(target: T, key: PropertyKey, value?: any, options?: PropertyDescriptor) => T;
|
|
32
30
|
|
|
33
|
-
export declare const flatten: (value: any, parser?: ((x: any) => any)
|
|
31
|
+
export declare const flatten: (value: any, parser?: ((x: any) => any), exit?: ((x: any) => boolean)) => any;
|
|
34
32
|
|
|
35
33
|
export declare const flattenJSONParse: (value: string | null) => any;
|
|
36
34
|
|
|
@@ -85,7 +83,7 @@ declare interface Result {
|
|
|
85
83
|
export declare const sleep: (wait?: number | {
|
|
86
84
|
min: number;
|
|
87
85
|
max: number;
|
|
88
|
-
}, immediate?: (
|
|
86
|
+
}, immediate?: (timer: ReturnType<typeof global.setTimeout>, duration: number, done: Function) => any) => Promise<unknown>;
|
|
89
87
|
|
|
90
88
|
export declare const throttle: (original: Function, wait?: number, options?: Options_2) => {
|
|
91
89
|
(this: any, ...args: any[]): any;
|
|
@@ -93,6 +91,6 @@ export declare const throttle: (original: Function, wait?: number, options?: Opt
|
|
|
93
91
|
flush(): void;
|
|
94
92
|
};
|
|
95
93
|
|
|
96
|
-
export declare const toPromise: <T extends {}, K = any>(target: T, promise: Promise<K>) => T & IPromise<K
|
|
94
|
+
export declare const toPromise: <T extends {}, K = any>(target: T, promise: Promise<K>) => T & IPromise<K>;
|
|
97
95
|
|
|
98
96
|
export { }
|
package/dist/index.iife.js
CHANGED
|
@@ -38,11 +38,11 @@ var HelperUtils = (function (exports) {
|
|
|
38
38
|
if (!hasPrefix) {
|
|
39
39
|
dataURL = `data:${filetype || getMime(filename)};base64,${dataURL}`;
|
|
40
40
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
41
|
+
const [suffix, dataURL$] = dataURL.split(",");
|
|
42
|
+
const mime = suffix.match(/:(.*?);/)?.[1];
|
|
43
|
+
const bstr = window.atob(dataURL$);
|
|
44
44
|
let n = bstr.length;
|
|
45
|
-
|
|
45
|
+
const u8arr = new Uint8Array(n);
|
|
46
46
|
while (n--) {
|
|
47
47
|
u8arr[n] = bstr.charCodeAt(n);
|
|
48
48
|
}
|
|
@@ -52,7 +52,7 @@ var HelperUtils = (function (exports) {
|
|
|
52
52
|
const canvasToImage = (canvas, filename) => {
|
|
53
53
|
const dataURL = canvas.toDataURL("image/png");
|
|
54
54
|
return new Promise((resolve) => {
|
|
55
|
-
|
|
55
|
+
const result = {
|
|
56
56
|
dataURL
|
|
57
57
|
};
|
|
58
58
|
if (typeof filename === "string") {
|
|
@@ -112,11 +112,11 @@ var HelperUtils = (function (exports) {
|
|
|
112
112
|
const { leading, trailing = true, throttle } = options || {};
|
|
113
113
|
let timer;
|
|
114
114
|
let invoke;
|
|
115
|
-
|
|
115
|
+
const cancel = () => {
|
|
116
116
|
timer && clearTimeout(timer);
|
|
117
117
|
timer = null;
|
|
118
118
|
};
|
|
119
|
-
|
|
119
|
+
const start = () => {
|
|
120
120
|
timer = setTimeout(() => {
|
|
121
121
|
timer = null;
|
|
122
122
|
trailing && invoke && invoke();
|
|
@@ -127,8 +127,7 @@ var HelperUtils = (function (exports) {
|
|
|
127
127
|
original.apply(this, args);
|
|
128
128
|
invoke = null;
|
|
129
129
|
};
|
|
130
|
-
if (!wait && throttle)
|
|
131
|
-
return invoke();
|
|
130
|
+
if (!wait && throttle) return invoke();
|
|
132
131
|
if (!timer) {
|
|
133
132
|
leading && invoke();
|
|
134
133
|
start();
|
|
@@ -162,17 +161,17 @@ var HelperUtils = (function (exports) {
|
|
|
162
161
|
let o = target;
|
|
163
162
|
path = path.replace(/\[(\w+)\]/g, ".$1");
|
|
164
163
|
path = path.replace(/^\./, "");
|
|
165
|
-
|
|
164
|
+
const keyArr = path.split(".");
|
|
166
165
|
let i = 0;
|
|
167
166
|
for (let len = keyArr.length; i < len - 1; ++i) {
|
|
168
|
-
|
|
167
|
+
const key = keyArr[i];
|
|
169
168
|
try {
|
|
170
169
|
if (key in o) {
|
|
171
170
|
o = o[key];
|
|
172
171
|
} else {
|
|
173
172
|
throw new Error();
|
|
174
173
|
}
|
|
175
|
-
} catch {
|
|
174
|
+
} catch (_) {
|
|
176
175
|
throw new Error("无效路径!");
|
|
177
176
|
}
|
|
178
177
|
}
|
|
@@ -200,7 +199,7 @@ var HelperUtils = (function (exports) {
|
|
|
200
199
|
return digit;
|
|
201
200
|
};
|
|
202
201
|
const addWan = (integer, num, mutiple, decimalDigit) => {
|
|
203
|
-
|
|
202
|
+
const digit = getDigit(integer);
|
|
204
203
|
if (digit > 3) {
|
|
205
204
|
let remainder = digit % 8;
|
|
206
205
|
if (remainder >= 5) {
|
|
@@ -216,11 +215,11 @@ var HelperUtils = (function (exports) {
|
|
|
216
215
|
number = Number.MAX_SAFE_INTEGER;
|
|
217
216
|
}
|
|
218
217
|
decimalDigit = decimalDigit == null ? 2 : decimalDigit;
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
218
|
+
const integer = Math.floor(number);
|
|
219
|
+
const digit = getDigit(integer);
|
|
220
|
+
const unit = [];
|
|
222
221
|
if (digit > 3) {
|
|
223
|
-
|
|
222
|
+
const multiple = Math.floor(digit / 8);
|
|
224
223
|
if (multiple >= 1) {
|
|
225
224
|
let tmp = Math.round(integer / Math.pow(10, 8 * multiple));
|
|
226
225
|
unit.push(addWan(tmp, number, 8 * multiple, decimalDigit));
|
|
@@ -251,11 +250,11 @@ var HelperUtils = (function (exports) {
|
|
|
251
250
|
return Math.floor(Math.random() * (max - min + 1) + min);
|
|
252
251
|
};
|
|
253
252
|
const probs = (weights) => {
|
|
254
|
-
|
|
253
|
+
const sum = weights.reduce((pre, cur) => pre + (!cur || cur <= 0 ? 0 : cur), 0);
|
|
255
254
|
if (!sum) {
|
|
256
255
|
throw new Error("不可能得到索引值");
|
|
257
256
|
}
|
|
258
|
-
|
|
257
|
+
const probs$ = weights.map((i) => i / sum);
|
|
259
258
|
let index = 0;
|
|
260
259
|
let r = Math.random();
|
|
261
260
|
while (r > 0) {
|
|
@@ -274,7 +273,7 @@ var HelperUtils = (function (exports) {
|
|
|
274
273
|
duration = wait || 0;
|
|
275
274
|
}
|
|
276
275
|
return new Promise((r) => {
|
|
277
|
-
|
|
276
|
+
const timer = setTimeout(r, duration);
|
|
278
277
|
immediate && immediate(timer, duration, r);
|
|
279
278
|
});
|
|
280
279
|
};
|
|
@@ -292,7 +291,7 @@ var HelperUtils = (function (exports) {
|
|
|
292
291
|
);
|
|
293
292
|
};
|
|
294
293
|
|
|
295
|
-
const flatten = (value, parser) => {
|
|
294
|
+
const flatten = (value, parser, exit) => {
|
|
296
295
|
let need = true;
|
|
297
296
|
let safeCount = 1;
|
|
298
297
|
let parseValue = value;
|
|
@@ -301,12 +300,12 @@ var HelperUtils = (function (exports) {
|
|
|
301
300
|
throw new Error(value);
|
|
302
301
|
}
|
|
303
302
|
try {
|
|
304
|
-
|
|
305
|
-
if (parseValue === next) {
|
|
303
|
+
const next = (parser || decodeURIComponent)(parseValue);
|
|
304
|
+
if (parseValue === next || typeof exit === "function" && exit(next)) {
|
|
306
305
|
need = false;
|
|
307
306
|
}
|
|
308
307
|
parseValue = next;
|
|
309
|
-
} catch {
|
|
308
|
+
} catch (_) {
|
|
310
309
|
need = false;
|
|
311
310
|
}
|
|
312
311
|
safeCount++;
|
|
@@ -315,9 +314,8 @@ var HelperUtils = (function (exports) {
|
|
|
315
314
|
};
|
|
316
315
|
|
|
317
316
|
const flattenJSONParse = (value) => {
|
|
318
|
-
if (value === null)
|
|
319
|
-
|
|
320
|
-
let regex = /^\d+$/;
|
|
317
|
+
if (value === null) return null;
|
|
318
|
+
const regex = /^\d+$/;
|
|
321
319
|
if (regex.test(value) && value.length >= 16 && +value > Number.MAX_SAFE_INTEGER) {
|
|
322
320
|
return value;
|
|
323
321
|
}
|
package/dist/index.js
CHANGED
|
@@ -35,11 +35,11 @@ const dataURLToFile = (dataURL, filename, filetype) => {
|
|
|
35
35
|
if (!hasPrefix) {
|
|
36
36
|
dataURL = `data:${filetype || getMime(filename)};base64,${dataURL}`;
|
|
37
37
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
38
|
+
const [suffix, dataURL$] = dataURL.split(",");
|
|
39
|
+
const mime = suffix.match(/:(.*?);/)?.[1];
|
|
40
|
+
const bstr = window.atob(dataURL$);
|
|
41
41
|
let n = bstr.length;
|
|
42
|
-
|
|
42
|
+
const u8arr = new Uint8Array(n);
|
|
43
43
|
while (n--) {
|
|
44
44
|
u8arr[n] = bstr.charCodeAt(n);
|
|
45
45
|
}
|
|
@@ -49,7 +49,7 @@ const dataURLToFile = (dataURL, filename, filetype) => {
|
|
|
49
49
|
const canvasToImage = (canvas, filename) => {
|
|
50
50
|
const dataURL = canvas.toDataURL("image/png");
|
|
51
51
|
return new Promise((resolve) => {
|
|
52
|
-
|
|
52
|
+
const result = {
|
|
53
53
|
dataURL
|
|
54
54
|
};
|
|
55
55
|
if (typeof filename === "string") {
|
|
@@ -109,11 +109,11 @@ const debounce = (original, wait, options) => {
|
|
|
109
109
|
const { leading, trailing = true, throttle } = options || {};
|
|
110
110
|
let timer;
|
|
111
111
|
let invoke;
|
|
112
|
-
|
|
112
|
+
const cancel = () => {
|
|
113
113
|
timer && clearTimeout(timer);
|
|
114
114
|
timer = null;
|
|
115
115
|
};
|
|
116
|
-
|
|
116
|
+
const start = () => {
|
|
117
117
|
timer = setTimeout(() => {
|
|
118
118
|
timer = null;
|
|
119
119
|
trailing && invoke && invoke();
|
|
@@ -124,8 +124,7 @@ const debounce = (original, wait, options) => {
|
|
|
124
124
|
original.apply(this, args);
|
|
125
125
|
invoke = null;
|
|
126
126
|
};
|
|
127
|
-
if (!wait && throttle)
|
|
128
|
-
return invoke();
|
|
127
|
+
if (!wait && throttle) return invoke();
|
|
129
128
|
if (!timer) {
|
|
130
129
|
leading && invoke();
|
|
131
130
|
start();
|
|
@@ -159,17 +158,17 @@ const getPropByPath = (target, path) => {
|
|
|
159
158
|
let o = target;
|
|
160
159
|
path = path.replace(/\[(\w+)\]/g, ".$1");
|
|
161
160
|
path = path.replace(/^\./, "");
|
|
162
|
-
|
|
161
|
+
const keyArr = path.split(".");
|
|
163
162
|
let i = 0;
|
|
164
163
|
for (let len = keyArr.length; i < len - 1; ++i) {
|
|
165
|
-
|
|
164
|
+
const key = keyArr[i];
|
|
166
165
|
try {
|
|
167
166
|
if (key in o) {
|
|
168
167
|
o = o[key];
|
|
169
168
|
} else {
|
|
170
169
|
throw new Error();
|
|
171
170
|
}
|
|
172
|
-
} catch {
|
|
171
|
+
} catch (_) {
|
|
173
172
|
throw new Error("无效路径!");
|
|
174
173
|
}
|
|
175
174
|
}
|
|
@@ -197,7 +196,7 @@ const getDigit = (integer) => {
|
|
|
197
196
|
return digit;
|
|
198
197
|
};
|
|
199
198
|
const addWan = (integer, num, mutiple, decimalDigit) => {
|
|
200
|
-
|
|
199
|
+
const digit = getDigit(integer);
|
|
201
200
|
if (digit > 3) {
|
|
202
201
|
let remainder = digit % 8;
|
|
203
202
|
if (remainder >= 5) {
|
|
@@ -213,11 +212,11 @@ const numberToUnit = (number, decimalDigit) => {
|
|
|
213
212
|
number = Number.MAX_SAFE_INTEGER;
|
|
214
213
|
}
|
|
215
214
|
decimalDigit = decimalDigit == null ? 2 : decimalDigit;
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
215
|
+
const integer = Math.floor(number);
|
|
216
|
+
const digit = getDigit(integer);
|
|
217
|
+
const unit = [];
|
|
219
218
|
if (digit > 3) {
|
|
220
|
-
|
|
219
|
+
const multiple = Math.floor(digit / 8);
|
|
221
220
|
if (multiple >= 1) {
|
|
222
221
|
let tmp = Math.round(integer / Math.pow(10, 8 * multiple));
|
|
223
222
|
unit.push(addWan(tmp, number, 8 * multiple, decimalDigit));
|
|
@@ -248,11 +247,11 @@ const range = (min, max) => {
|
|
|
248
247
|
return Math.floor(Math.random() * (max - min + 1) + min);
|
|
249
248
|
};
|
|
250
249
|
const probs = (weights) => {
|
|
251
|
-
|
|
250
|
+
const sum = weights.reduce((pre, cur) => pre + (!cur || cur <= 0 ? 0 : cur), 0);
|
|
252
251
|
if (!sum) {
|
|
253
252
|
throw new Error("不可能得到索引值");
|
|
254
253
|
}
|
|
255
|
-
|
|
254
|
+
const probs$ = weights.map((i) => i / sum);
|
|
256
255
|
let index = 0;
|
|
257
256
|
let r = Math.random();
|
|
258
257
|
while (r > 0) {
|
|
@@ -271,7 +270,7 @@ const sleep = (wait, immediate) => {
|
|
|
271
270
|
duration = wait || 0;
|
|
272
271
|
}
|
|
273
272
|
return new Promise((r) => {
|
|
274
|
-
|
|
273
|
+
const timer = setTimeout(r, duration);
|
|
275
274
|
immediate && immediate(timer, duration, r);
|
|
276
275
|
});
|
|
277
276
|
};
|
|
@@ -289,7 +288,7 @@ const throttle = (original, wait, options) => {
|
|
|
289
288
|
);
|
|
290
289
|
};
|
|
291
290
|
|
|
292
|
-
const flatten = (value, parser) => {
|
|
291
|
+
const flatten = (value, parser, exit) => {
|
|
293
292
|
let need = true;
|
|
294
293
|
let safeCount = 1;
|
|
295
294
|
let parseValue = value;
|
|
@@ -298,12 +297,12 @@ const flatten = (value, parser) => {
|
|
|
298
297
|
throw new Error(value);
|
|
299
298
|
}
|
|
300
299
|
try {
|
|
301
|
-
|
|
302
|
-
if (parseValue === next) {
|
|
300
|
+
const next = (parser || decodeURIComponent)(parseValue);
|
|
301
|
+
if (parseValue === next || typeof exit === "function" && exit(next)) {
|
|
303
302
|
need = false;
|
|
304
303
|
}
|
|
305
304
|
parseValue = next;
|
|
306
|
-
} catch {
|
|
305
|
+
} catch (_) {
|
|
307
306
|
need = false;
|
|
308
307
|
}
|
|
309
308
|
safeCount++;
|
|
@@ -312,9 +311,8 @@ const flatten = (value, parser) => {
|
|
|
312
311
|
};
|
|
313
312
|
|
|
314
313
|
const flattenJSONParse = (value) => {
|
|
315
|
-
if (value === null)
|
|
316
|
-
|
|
317
|
-
let regex = /^\d+$/;
|
|
314
|
+
if (value === null) return null;
|
|
315
|
+
const regex = /^\d+$/;
|
|
318
316
|
if (regex.test(value) && value.length >= 16 && +value > Number.MAX_SAFE_INTEGER) {
|
|
319
317
|
return value;
|
|
320
318
|
}
|
package/dist/index.umd.cjs
CHANGED
|
@@ -41,11 +41,11 @@
|
|
|
41
41
|
if (!hasPrefix) {
|
|
42
42
|
dataURL = `data:${filetype || getMime(filename)};base64,${dataURL}`;
|
|
43
43
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
44
|
+
const [suffix, dataURL$] = dataURL.split(",");
|
|
45
|
+
const mime = suffix.match(/:(.*?);/)?.[1];
|
|
46
|
+
const bstr = window.atob(dataURL$);
|
|
47
47
|
let n = bstr.length;
|
|
48
|
-
|
|
48
|
+
const u8arr = new Uint8Array(n);
|
|
49
49
|
while (n--) {
|
|
50
50
|
u8arr[n] = bstr.charCodeAt(n);
|
|
51
51
|
}
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
const canvasToImage = (canvas, filename) => {
|
|
56
56
|
const dataURL = canvas.toDataURL("image/png");
|
|
57
57
|
return new Promise((resolve) => {
|
|
58
|
-
|
|
58
|
+
const result = {
|
|
59
59
|
dataURL
|
|
60
60
|
};
|
|
61
61
|
if (typeof filename === "string") {
|
|
@@ -115,11 +115,11 @@
|
|
|
115
115
|
const { leading, trailing = true, throttle } = options || {};
|
|
116
116
|
let timer;
|
|
117
117
|
let invoke;
|
|
118
|
-
|
|
118
|
+
const cancel = () => {
|
|
119
119
|
timer && clearTimeout(timer);
|
|
120
120
|
timer = null;
|
|
121
121
|
};
|
|
122
|
-
|
|
122
|
+
const start = () => {
|
|
123
123
|
timer = setTimeout(() => {
|
|
124
124
|
timer = null;
|
|
125
125
|
trailing && invoke && invoke();
|
|
@@ -130,8 +130,7 @@
|
|
|
130
130
|
original.apply(this, args);
|
|
131
131
|
invoke = null;
|
|
132
132
|
};
|
|
133
|
-
if (!wait && throttle)
|
|
134
|
-
return invoke();
|
|
133
|
+
if (!wait && throttle) return invoke();
|
|
135
134
|
if (!timer) {
|
|
136
135
|
leading && invoke();
|
|
137
136
|
start();
|
|
@@ -165,17 +164,17 @@
|
|
|
165
164
|
let o = target;
|
|
166
165
|
path = path.replace(/\[(\w+)\]/g, ".$1");
|
|
167
166
|
path = path.replace(/^\./, "");
|
|
168
|
-
|
|
167
|
+
const keyArr = path.split(".");
|
|
169
168
|
let i = 0;
|
|
170
169
|
for (let len = keyArr.length; i < len - 1; ++i) {
|
|
171
|
-
|
|
170
|
+
const key = keyArr[i];
|
|
172
171
|
try {
|
|
173
172
|
if (key in o) {
|
|
174
173
|
o = o[key];
|
|
175
174
|
} else {
|
|
176
175
|
throw new Error();
|
|
177
176
|
}
|
|
178
|
-
} catch {
|
|
177
|
+
} catch (_) {
|
|
179
178
|
throw new Error("无效路径!");
|
|
180
179
|
}
|
|
181
180
|
}
|
|
@@ -203,7 +202,7 @@
|
|
|
203
202
|
return digit;
|
|
204
203
|
};
|
|
205
204
|
const addWan = (integer, num, mutiple, decimalDigit) => {
|
|
206
|
-
|
|
205
|
+
const digit = getDigit(integer);
|
|
207
206
|
if (digit > 3) {
|
|
208
207
|
let remainder = digit % 8;
|
|
209
208
|
if (remainder >= 5) {
|
|
@@ -219,11 +218,11 @@
|
|
|
219
218
|
number = Number.MAX_SAFE_INTEGER;
|
|
220
219
|
}
|
|
221
220
|
decimalDigit = decimalDigit == null ? 2 : decimalDigit;
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
221
|
+
const integer = Math.floor(number);
|
|
222
|
+
const digit = getDigit(integer);
|
|
223
|
+
const unit = [];
|
|
225
224
|
if (digit > 3) {
|
|
226
|
-
|
|
225
|
+
const multiple = Math.floor(digit / 8);
|
|
227
226
|
if (multiple >= 1) {
|
|
228
227
|
let tmp = Math.round(integer / Math.pow(10, 8 * multiple));
|
|
229
228
|
unit.push(addWan(tmp, number, 8 * multiple, decimalDigit));
|
|
@@ -254,11 +253,11 @@
|
|
|
254
253
|
return Math.floor(Math.random() * (max - min + 1) + min);
|
|
255
254
|
};
|
|
256
255
|
const probs = (weights) => {
|
|
257
|
-
|
|
256
|
+
const sum = weights.reduce((pre, cur) => pre + (!cur || cur <= 0 ? 0 : cur), 0);
|
|
258
257
|
if (!sum) {
|
|
259
258
|
throw new Error("不可能得到索引值");
|
|
260
259
|
}
|
|
261
|
-
|
|
260
|
+
const probs$ = weights.map((i) => i / sum);
|
|
262
261
|
let index = 0;
|
|
263
262
|
let r = Math.random();
|
|
264
263
|
while (r > 0) {
|
|
@@ -277,7 +276,7 @@
|
|
|
277
276
|
duration = wait || 0;
|
|
278
277
|
}
|
|
279
278
|
return new Promise((r) => {
|
|
280
|
-
|
|
279
|
+
const timer = setTimeout(r, duration);
|
|
281
280
|
immediate && immediate(timer, duration, r);
|
|
282
281
|
});
|
|
283
282
|
};
|
|
@@ -295,7 +294,7 @@
|
|
|
295
294
|
);
|
|
296
295
|
};
|
|
297
296
|
|
|
298
|
-
const flatten = (value, parser) => {
|
|
297
|
+
const flatten = (value, parser, exit) => {
|
|
299
298
|
let need = true;
|
|
300
299
|
let safeCount = 1;
|
|
301
300
|
let parseValue = value;
|
|
@@ -304,12 +303,12 @@
|
|
|
304
303
|
throw new Error(value);
|
|
305
304
|
}
|
|
306
305
|
try {
|
|
307
|
-
|
|
308
|
-
if (parseValue === next) {
|
|
306
|
+
const next = (parser || decodeURIComponent)(parseValue);
|
|
307
|
+
if (parseValue === next || typeof exit === "function" && exit(next)) {
|
|
309
308
|
need = false;
|
|
310
309
|
}
|
|
311
310
|
parseValue = next;
|
|
312
|
-
} catch {
|
|
311
|
+
} catch (_) {
|
|
313
312
|
need = false;
|
|
314
313
|
}
|
|
315
314
|
safeCount++;
|
|
@@ -318,9 +317,8 @@
|
|
|
318
317
|
};
|
|
319
318
|
|
|
320
319
|
const flattenJSONParse = (value) => {
|
|
321
|
-
if (value === null)
|
|
322
|
-
|
|
323
|
-
let regex = /^\d+$/;
|
|
320
|
+
if (value === null) return null;
|
|
321
|
+
const regex = /^\d+$/;
|
|
324
322
|
if (regex.test(value) && value.length >= 16 && +value > Number.MAX_SAFE_INTEGER) {
|
|
325
323
|
return value;
|
|
326
324
|
}
|