@base-web-kits/base-tools-ts 0.9.12 → 0.9.99
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/base-tools-ts.umd.global.js +2212 -7200
- package/dist/base-tools-ts.umd.global.js.map +1 -1
- package/dist/es-toolkit/index.d.ts +8 -0
- package/dist/es-toolkit/index.d.ts.map +1 -0
- package/dist/index.cjs +2219 -7354
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2166 -7154
- package/dist/index.js.map +1 -1
- package/dist/number/big.d.ts +50 -59
- package/dist/number/big.d.ts.map +1 -1
- package/dist/number/random.d.ts +0 -25
- package/dist/number/random.d.ts.map +1 -1
- package/dist/object/index.d.ts +1 -1
- package/dist/url/param/index.d.ts +0 -38
- package/dist/url/param/index.d.ts.map +1 -1
- package/dist/validator/index.d.ts +0 -11
- package/dist/validator/index.d.ts.map +1 -1
- package/package.json +37 -38
- package/src/ts/es-toolkit/index.ts +7 -0
- package/src/ts/index.ts +1 -1
- package/src/ts/number/big.ts +55 -51
- package/src/ts/number/random.ts +0 -56
- package/src/ts/object/index.ts +1 -1
- package/src/ts/string/random.ts +2 -2
- package/src/ts/url/param/index.ts +0 -91
- package/src/ts/validator/index.ts +0 -25
- package/dist/lodash/index.d.ts +0 -8
- package/dist/lodash/index.d.ts.map +0 -1
- package/src/ts/lodash/index.ts +0 -7
package/src/ts/number/random.ts
CHANGED
|
@@ -1,59 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 随机生成 `a` 到 `b` 的整数(闭区间,包含两端)。
|
|
3
|
-
* - 自动交换边界,按从小到大处理。
|
|
4
|
-
* - 下界向上取整、上界向下取整后再取值。
|
|
5
|
-
* @param a 边界值。
|
|
6
|
-
* @param b 边界值。
|
|
7
|
-
* @returns 闭区间内的随机整数。
|
|
8
|
-
* @example
|
|
9
|
-
* randomInt(0, 10); // => 0..10 之间的随机整数(含 0 与 10)
|
|
10
|
-
* randomInt(10, 0); // => 0..10 之间的随机整数(含 0 与 10)
|
|
11
|
-
* randomInt(5.2, 10.8); // => 6..10 之间取整随机数(含 6 与 10)
|
|
12
|
-
*/
|
|
13
|
-
export function randomInt(a: number, b: number): number {
|
|
14
|
-
if (!Number.isFinite(a) || !Number.isFinite(b)) {
|
|
15
|
-
throw new TypeError('min 和 max 必须是有限数值');
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const low = Math.min(a, b);
|
|
19
|
-
const high = Math.max(a, b);
|
|
20
|
-
|
|
21
|
-
const minInt = Math.ceil(low);
|
|
22
|
-
const maxInt = Math.floor(high);
|
|
23
|
-
|
|
24
|
-
if (maxInt < minInt) {
|
|
25
|
-
throw new RangeError('取整后区间为空');
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
if (maxInt === minInt) return minInt;
|
|
29
|
-
|
|
30
|
-
return Math.floor(Math.random() * (maxInt - minInt + 1)) + minInt;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* 随机生成 `a` 到 `b` 的浮点数(半开区间,包含下界不包含上界)。
|
|
35
|
-
* - 自动交换边界,按从小到大处理。
|
|
36
|
-
* @param a 边界值。
|
|
37
|
-
* @param b 边界值。
|
|
38
|
-
* @returns 半开区间内的随机浮点数。
|
|
39
|
-
* @example
|
|
40
|
-
* randomFloat(0, 10); // => [0, 10) 内的随机浮点数
|
|
41
|
-
* randomFloat(10, 0); // => [0, 10) 内的随机浮点数
|
|
42
|
-
* randomFloat(5.2, 10.8); // => [5.2, 10.8) 内的随机浮点数
|
|
43
|
-
*/
|
|
44
|
-
export function randomFloat(a: number, b: number): number {
|
|
45
|
-
if (!Number.isFinite(a) || !Number.isFinite(b)) {
|
|
46
|
-
throw new TypeError('min 和 max 必须是有限数值');
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const low = Math.min(a, b);
|
|
50
|
-
const high = Math.max(a, b);
|
|
51
|
-
|
|
52
|
-
if (high === low) return low;
|
|
53
|
-
|
|
54
|
-
return Math.random() * (high - low) + low;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
1
|
/**
|
|
58
2
|
* 随机生成一个布尔值。
|
|
59
3
|
* @returns 随机布尔值。
|
package/src/ts/object/index.ts
CHANGED
package/src/ts/string/random.ts
CHANGED
|
@@ -36,7 +36,7 @@ export function createRandId(prefix = 'id_') {
|
|
|
36
36
|
export function createTimeRandId(digits: number = 6) {
|
|
37
37
|
const base = 10 ** (digits - 1);
|
|
38
38
|
const range = 9 * base;
|
|
39
|
-
const
|
|
39
|
+
const int = Math.floor(Math.random() * range) + base;
|
|
40
40
|
|
|
41
|
-
return `${Date.now()}${
|
|
41
|
+
return `${Date.now()}${int}`;
|
|
42
42
|
}
|
|
@@ -1,94 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 获取url的查询参数值
|
|
3
|
-
* - 采用纯JS解析,因为小程序不支持URLSearchParams
|
|
4
|
-
* @param key 参数名
|
|
5
|
-
* @param url 完整 URL 或仅查询串(如 "a=1&b=2")
|
|
6
|
-
* @returns 解码后的参数值 (若不存在|"null"|"undefined",则返回 null)
|
|
7
|
-
* @example
|
|
8
|
-
* const q = getUrlParam('q', 'https://a.com/?q=%E6%B5%8B%E8%AF%95'); // "测试"
|
|
9
|
-
* const a = getUrlParam('a', 'a=1'); // "1"
|
|
10
|
-
* const list = getUrlParam('list', 'list=[1,2]'); // "[1,2]"
|
|
11
|
-
* const list = getUrlParam('list', 'list=null'); // null
|
|
12
|
-
* const list = getUrlParam('list', 'list=undefined'); // null
|
|
13
|
-
*/
|
|
14
|
-
export function getUrlParam(key: string, url: string) {
|
|
15
|
-
const raw = url.includes('?') ? url.slice(url.indexOf('?') + 1) : url.includes('=') ? url : '';
|
|
16
|
-
const qs = raw.split('#')[0];
|
|
17
|
-
if (!qs) return null;
|
|
18
|
-
const pairs = qs.split('&').filter(Boolean);
|
|
19
|
-
const decode = (s: string) => {
|
|
20
|
-
try {
|
|
21
|
-
return decodeURIComponent(s.replace(/\+/g, ' '));
|
|
22
|
-
} catch {
|
|
23
|
-
return s;
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
for (const pair of pairs) {
|
|
27
|
-
const i = pair.indexOf('=');
|
|
28
|
-
const k = i >= 0 ? pair.slice(0, i) : pair;
|
|
29
|
-
if (decode(k) === key) {
|
|
30
|
-
const v = i >= 0 ? decode(pair.slice(i + 1)) : '';
|
|
31
|
-
return v !== 'null' && v !== 'undefined' ? v : null;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
return null;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* 获取url的查询参数值,并转为number类型
|
|
39
|
-
* @param key 参数名
|
|
40
|
-
* @param url 完整 URL 或仅查询串(如 "a=1&b=2")
|
|
41
|
-
* @returns 解码后的参数值 (若不存在|"非数字字符串",则返回 null)
|
|
42
|
-
* @example
|
|
43
|
-
* const a = getUrlNumber('a', 'https://a.com/?a=1'); // 1
|
|
44
|
-
* const a = getUrlNumber('a', 'a=1'); // 1
|
|
45
|
-
* const a = getUrlNumber('a', 'a=1.2'); // 1.2
|
|
46
|
-
* const a = getUrlNumber('a', 'a=abc'); // null
|
|
47
|
-
*/
|
|
48
|
-
export function getUrlNumber(key: string, url: string) {
|
|
49
|
-
const str = getUrlParam(key, url);
|
|
50
|
-
if (!str) return null;
|
|
51
|
-
|
|
52
|
-
const num = Number(str);
|
|
53
|
-
return isNaN(num) ? null : num;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* 获取url的所有查询参数值
|
|
58
|
-
* - 采用纯JS解析,因为小程序不支持URLSearchParams
|
|
59
|
-
* @param url 完整 URL 或仅查询串(如 "a=1&b=2")
|
|
60
|
-
* @returns 解码后的键值对象(无参数返回空对象; "null"|"undefined"的参数会被忽略)
|
|
61
|
-
* @example
|
|
62
|
-
* const params = getUrlParamAll('a=1&b=2'); // { a: "1", b: "2" }
|
|
63
|
-
* const params = getUrlParamAll('https://a.com/?a=1&b=2'); // { a: "1", b: "2" }
|
|
64
|
-
* const params = getUrlParamAll('a=1&b=null'); // { a: "1" }
|
|
65
|
-
* const params = getUrlParamAll('a=1&b=undefined'); // { a: "1" }
|
|
66
|
-
*/
|
|
67
|
-
export function getUrlParamAll(url: string) {
|
|
68
|
-
const raw = url.includes('?') ? url.slice(url.indexOf('?') + 1) : url.includes('=') ? url : '';
|
|
69
|
-
const qs = raw.split('#')[0];
|
|
70
|
-
const result: Record<string, string> = {};
|
|
71
|
-
if (!qs) return result;
|
|
72
|
-
const decode = (s: string) => {
|
|
73
|
-
try {
|
|
74
|
-
return decodeURIComponent(s.replace(/\+/g, ' '));
|
|
75
|
-
} catch {
|
|
76
|
-
return s;
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
for (const seg of qs.split('&')) {
|
|
80
|
-
if (!seg) continue;
|
|
81
|
-
const i = seg.indexOf('=');
|
|
82
|
-
const k = i >= 0 ? seg.slice(0, i) : seg;
|
|
83
|
-
const v = i >= 0 ? seg.slice(i + 1) : '';
|
|
84
|
-
const dv = decode(v);
|
|
85
|
-
if (dv !== 'null' && dv !== 'undefined') {
|
|
86
|
-
result[decode(k)] = dv;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
return result;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
1
|
/**
|
|
93
2
|
* 将对象参数拼接到 URL
|
|
94
3
|
* - 采用纯JS拼接,因为小程序不支持URLSearchParams
|
|
@@ -393,31 +393,6 @@ export function isTaxID(code: string) {
|
|
|
393
393
|
return v[17] === expected;
|
|
394
394
|
}
|
|
395
395
|
|
|
396
|
-
/**
|
|
397
|
-
* 判断字符串是否为合法 JSON 文本。
|
|
398
|
-
* 说明:传入字符串时尝试 `JSON.parse`;传入对象/数组则视为合法。
|
|
399
|
-
* @param input 待判定的值或字符串
|
|
400
|
-
* @returns 是否为合法 JSON
|
|
401
|
-
* @example
|
|
402
|
-
* isJSON('{"a":1}') // true
|
|
403
|
-
* isJSON('[1,2]') // true
|
|
404
|
-
* isJSON('abc') // false
|
|
405
|
-
*/
|
|
406
|
-
export function isJSON(input: unknown) {
|
|
407
|
-
if (typeof input === 'string') {
|
|
408
|
-
const s = input.trim();
|
|
409
|
-
if (s === '') return false;
|
|
410
|
-
try {
|
|
411
|
-
JSON.parse(s);
|
|
412
|
-
return true;
|
|
413
|
-
} catch {
|
|
414
|
-
return false;
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
if (input !== null && typeof input === 'object') return true;
|
|
418
|
-
return false;
|
|
419
|
-
}
|
|
420
|
-
|
|
421
396
|
/**
|
|
422
397
|
* HEX 颜色值(支持 `#RGB`、`#RRGGBB`、`#RRGGBBAA`)。
|
|
423
398
|
* @param s 颜色字符串
|
package/dist/lodash/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/ts/lodash/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,cAAc,WAAW,CAAC"}
|