@base-web-kits/base-tools-ts 0.9.12 → 1.0.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.
@@ -14,18 +14,18 @@ export type NumLike = string | number | BigNumber;
14
14
  * big('0.1'); // => BigNumber
15
15
  * big(0.2); // => BigNumber
16
16
  */
17
- export function big(x: NumLike): BigNumber {
17
+ function big(x: NumLike): BigNumber {
18
18
  return x instanceof BigNumber ? x : new BigNumber(x);
19
19
  }
20
20
 
21
21
  /**
22
22
  * 高精度加法(支持多个参数连加)。
23
23
  * @example
24
- * bigPlus(0.1, 0.2); // => 0.3
25
- * bigPlus('0.1', '0.2'); // => 0.3
26
- * bigPlus(1, 2, 3, 4); // => 10 // 多参数连加: 1+2+3+4
24
+ * mathPlus(0.1, 0.2); // => 0.3
25
+ * mathPlus('0.1', '0.2'); // => 0.3
26
+ * mathPlus(1, 2, 3, 4); // => 10 // 多参数连加: 1+2+3+4
27
27
  */
28
- export function bigPlus(...rest: NumLike[]) {
28
+ export function mathPlus(...rest: NumLike[]) {
29
29
  let acc = big(rest[0]);
30
30
  for (const x of rest.slice(1)) acc = acc.plus(big(x));
31
31
  return acc.toNumber();
@@ -34,11 +34,11 @@ export function bigPlus(...rest: NumLike[]) {
34
34
  /**
35
35
  * 高精度减法(支持多个参数连减)。
36
36
  * @example
37
- * bigMinus(1, 0.9); // => 0.1
38
- * bigMinus('1.1', '0.2'); // => 0.9
39
- * bigMinus(10, 1, 2, 3); // => 4 // 多参数连减: 10-1-2-3
37
+ * mathMinus(1, 0.9); // => 0.1
38
+ * mathMinus('1.1', '0.2'); // => 0.9
39
+ * mathMinus(10, 1, 2, 3); // => 4 // 多参数连减: 10-1-2-3
40
40
  */
41
- export function bigMinus(...rest: NumLike[]) {
41
+ export function mathMinus(...rest: NumLike[]) {
42
42
  let acc = big(rest[0]);
43
43
  for (const x of rest.slice(1)) acc = acc.minus(big(x));
44
44
  return acc.toNumber();
@@ -47,11 +47,11 @@ export function bigMinus(...rest: NumLike[]) {
47
47
  /**
48
48
  * 高精度乘法(支持多个参数连乘)。
49
49
  * @example
50
- * bigTimes(0.1, 0.2); // => 0.02
51
- * bigTimes('1.5', '3'); // => 4.5
52
- * bigTimes(2, 3, 4); // => 24 // 多参数连乘: 2*3*4
50
+ * mathTimes(0.1, 0.2); // => 0.02
51
+ * mathTimes('1.5', '3'); // => 4.5
52
+ * mathTimes(2, 3, 4); // => 24 // 多参数连乘: 2*3*4
53
53
  */
54
- export function bigTimes(...rest: NumLike[]) {
54
+ export function mathTimes(...rest: NumLike[]) {
55
55
  let acc = big(rest[0]);
56
56
  for (const x of rest.slice(1)) acc = acc.times(big(x));
57
57
  return acc.toNumber();
@@ -60,11 +60,11 @@ export function bigTimes(...rest: NumLike[]) {
60
60
  /**
61
61
  * 高精度除法(支持多个参数连除)。
62
62
  * @example
63
- * bigDiv(1, 3); // => 0.333333...
64
- * bigDiv('10', '4'); // => 2.5
65
- * bigDiv(100, 5, 2); // => 10 // 多参数连除: 100/5/2
63
+ * mathDiv(1, 3); // => 0.333333...
64
+ * mathDiv('10', '4'); // => 2.5
65
+ * mathDiv(100, 5, 2); // => 10 // 多参数连除: 100/5/2
66
66
  */
67
- export function bigDiv(...rest: NumLike[]) {
67
+ export function mathDiv(...rest: NumLike[]) {
68
68
  let acc = big(rest[0]);
69
69
  for (const x of rest.slice(1)) acc = acc.div(big(x));
70
70
  return acc.toNumber();
@@ -76,10 +76,10 @@ export function bigDiv(...rest: NumLike[]) {
76
76
  * @param y 指数。
77
77
  * @returns 计算结果。
78
78
  * @example
79
- * bigPow(2, 3); // => 8
80
- * bigPow('2.5', 2); // => 6.25
79
+ * mathPow(2, 3); // => 8
80
+ * mathPow('2.5', 2); // => 6.25
81
81
  */
82
- export function bigPow(x: NumLike, y: NumLike) {
82
+ export function mathPow(x: NumLike, y: NumLike) {
83
83
  return big(x).pow(big(y)).toNumber();
84
84
  }
85
85
 
@@ -90,12 +90,16 @@ export function bigPow(x: NumLike, y: NumLike) {
90
90
  * @param rm 舍入模式,默认 `ROUND_HALF_UP`(四舍五入)。
91
91
  * @returns 舍入后的数值。
92
92
  * @example
93
- * bigRound(1.6); // => 2
94
- * bigRound('1.234', 2); // => 1.23
95
- * bigRound('1.235', 2); // => 1.24
96
- * bigRound('1.299', 2, BigNumber.ROUND_DOWN); // => 1.29
93
+ * mathRound(1.6); // => 2
94
+ * mathRound('1.234', 2); // => 1.23
95
+ * mathRound('1.235', 2); // => 1.24
96
+ * mathRound('1.299', 2, BigNumber.ROUND_DOWN); // => 1.29
97
97
  */
98
- export function bigRound(x: NumLike, dp = 0, rm: BigNumber.RoundingMode = BigNumber.ROUND_HALF_UP) {
98
+ export function mathRound(
99
+ x: NumLike,
100
+ dp = 0,
101
+ rm: BigNumber.RoundingMode = BigNumber.ROUND_HALF_UP,
102
+ ) {
99
103
  return big(x).decimalPlaces(dp, rm).toNumber();
100
104
  }
101
105
 
@@ -106,13 +110,13 @@ export function bigRound(x: NumLike, dp = 0, rm: BigNumber.RoundingMode = BigNum
106
110
  * @param rm 舍入模式,默认 `ROUND_HALF_UP`(四舍五入)。
107
111
  * @returns 格式化后的字符串。
108
112
  * @example
109
- * bigFixed('1'); // => '1.00'
110
- * +bigFixed('1'); // => 1
111
- * bigFixed(1.2345); // => '1.23'
112
- * bigFixed(1.2345, 3); // => '1.235'
113
- * bigFixed('1.2345', 0, BigNumber.ROUND_UP); // => '2'
113
+ * mathFixed('1'); // => '1.00'
114
+ * +mathFixed('1'); // => 1
115
+ * mathFixed(1.2345); // => '1.23'
116
+ * mathFixed(1.2345, 3); // => '1.235'
117
+ * mathFixed('1.2345', 0, BigNumber.ROUND_UP); // => '2'
114
118
  */
115
- export function bigFixed(
119
+ export function mathFixed(
116
120
  x: NumLike,
117
121
  dp = 2,
118
122
  rm: BigNumber.RoundingMode = BigNumber.ROUND_HALF_UP,
@@ -123,62 +127,62 @@ export function bigFixed(
123
127
  /**
124
128
  * 比较两个数值大小。
125
129
  * @example
126
- * bigCompare('2', '10'); // => -1
127
- * bigCompare(3, 3); // => 0
128
- * bigCompare('10', 2); // => 1
130
+ * mathCompare('2', '10'); // => -1
131
+ * mathCompare(3, 3); // => 0
132
+ * mathCompare('10', 2); // => 1
129
133
  */
130
- export function bigCompare(a: NumLike, b: NumLike): -1 | 0 | 1 | null {
134
+ export function mathCompare(a: NumLike, b: NumLike): -1 | 0 | 1 | null {
131
135
  return big(a).comparedTo(big(b));
132
136
  }
133
137
 
134
138
  /**
135
139
  * 判断两个数值是否相等。
136
140
  * @example
137
- * bigEqual('1.0', 1); // => true
138
- * bigEqual(2, 1); // => false
141
+ * mathEqual('1.0', 1); // => true
142
+ * mathEqual(2, 1); // => false
139
143
  */
140
- export function bigEqual(a: NumLike, b: NumLike): boolean {
144
+ export function mathEqual(a: NumLike, b: NumLike): boolean {
141
145
  return big(a).isEqualTo(big(b));
142
146
  }
143
147
 
144
148
  /**
145
149
  * 判断 a 是否大于 b。
146
150
  * @example
147
- * bigGreaterThan(2, 1); // => true
148
- * bigGreaterThan(1, 2); // => false
151
+ * mathGreaterThan(2, 1); // => true
152
+ * mathGreaterThan(1, 2); // => false
149
153
  */
150
- export function bigGreaterThan(a: NumLike, b: NumLike): boolean {
154
+ export function mathGreaterThan(a: NumLike, b: NumLike): boolean {
151
155
  return big(a).isGreaterThan(big(b));
152
156
  }
153
157
 
154
158
  /**
155
159
  * 判断 a 是否大于等于 b。
156
160
  * @example
157
- * bigGreaterThanOrEqual(2, 2); // => true
158
- * bigGreaterThanOrEqual(1, 2); // => false
161
+ * mathGreaterThanOrEqual(2, 2); // => true
162
+ * mathGreaterThanOrEqual(1, 2); // => false
159
163
  */
160
- export function bigGreaterThanOrEqualTo(a: NumLike, b: NumLike): boolean {
164
+ export function mathGreaterThanOrEqual(a: NumLike, b: NumLike): boolean {
161
165
  return big(a).isGreaterThanOrEqualTo(big(b));
162
166
  }
163
167
 
164
168
  /**
165
169
  * 判断 a 是否小于 b。
166
170
  * @example
167
- * bigLessThan(1, 2); // => true
168
- * bigLessThan(2, 1); // => false
171
+ * mathLessThan(1, 2); // => true
172
+ * mathLessThan(2, 1); // => false
169
173
  */
170
- export function bigLessThan(a: NumLike, b: NumLike): boolean {
174
+ export function mathLessThan(a: NumLike, b: NumLike): boolean {
171
175
  return big(a).isLessThan(big(b));
172
176
  }
173
177
 
174
178
  /**
175
179
  * 判断 a 是否小于等于 b。
176
180
  * @example
177
- * bigLessThanOrEqual(2, 2); // => true
178
- * bigLessThanOrEqual(1, 2); // => true
179
- * bigLessThanOrEqual(2, 1); // => false
181
+ * mathLessThanOrEqual(2, 2); // => true
182
+ * mathLessThanOrEqual(1, 2); // => true
183
+ * mathLessThanOrEqual(2, 1); // => false
180
184
  */
181
- export function bigLessThanOrEqual(a: NumLike, b: NumLike): boolean {
185
+ export function mathLessThanOrEqual(a: NumLike, b: NumLike): boolean {
182
186
  return big(a).isLessThanOrEqualTo(big(b));
183
187
  }
184
188
 
@@ -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 随机布尔值。
@@ -1,6 +1,8 @@
1
+ import { get, set } from 'es-toolkit/compat';
2
+
1
3
  /**
2
4
  * 获取对象键名数组(类型安全)。
3
- * 注:内置 `Object.keys` 与 `lodash-es` 的 `keys` 在 TS 中通常返回 `string[]`,无法精确到 `keyof T`。
5
+ * 注:内置 `Object.keys` 与 `es-toolkit` 的 `keys` 在 TS 中通常返回 `string[]`,无法精确到 `keyof T`。
4
6
  * @param obj 目标对象
5
7
  * @returns 类型精确的 `Array<keyof T>`
6
8
  * @example
@@ -10,3 +12,28 @@
10
12
  export function getObjectKeys<T extends object>(obj: T): Array<keyof T> {
11
13
  return Object.keys(obj) as (keyof T)[];
12
14
  }
15
+
16
+ /**
17
+ * 获取对象值。
18
+ * @param obj 目标对象
19
+ * @param path 路径。(点路径:'a.b'、'a[0].b';数组路径:['a',0,'b'])
20
+ * @returns 值
21
+ * @example
22
+ * const o = { b: { c: 'x' }, users: [{ name: 'john' }, { name: 'jane' }] };
23
+ * const c = getObjectValue(o, 'b.c'); // 点路径: 'x'
24
+ * const name0 = getObjectValue(o, 'users[0].name'); // 数组字符: 'john'
25
+ * const name1 = getObjectValue(o, ['users', 1, 'name']); // 数组路径: 'jane'
26
+ */
27
+ export const getObjectValue = get;
28
+
29
+ /**
30
+ * 设置对象值。
31
+ * @param obj 目标对象
32
+ * @param path 路径。(点路径:'a.b'、'a[0].b';数组路径:['a',0,'b'])
33
+ * @param value 值
34
+ * @example
35
+ * const o = { b: { c: 'x' }, users: [{ name: 'john' }, { name: 'jane' }] };
36
+ * setObjectValue(o, 'b.c', 'y'); // 点路径
37
+ * setObjectValue(o, ['users', 1, 'name'], 'jane-doe'); // 数组路径
38
+ */
39
+ export const setObjectValue = set;
@@ -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 randomInt = Math.floor(Math.random() * range) + base;
39
+ const int = Math.floor(Math.random() * range) + base;
40
40
 
41
- return `${Date.now()}${randomInt}`;
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 颜色字符串
@@ -1,8 +0,0 @@
1
- /**
2
- * re-export 全量 lodash-es
3
- * 文档: https://www.lodashjs.com/
4
- * 目的: 从工具库统一loadsh版本,避免项目多个版本冲突
5
- * 注意: 需在tsup.config.ts加入noExternal: ['lodash-es'],确保打包时不被忽略
6
- */
7
- export * from 'lodash-es';
8
- //# sourceMappingURL=index.d.ts.map
@@ -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"}
@@ -1,7 +0,0 @@
1
- /**
2
- * re-export 全量 lodash-es
3
- * 文档: https://www.lodashjs.com/
4
- * 目的: 从工具库统一loadsh版本,避免项目多个版本冲突
5
- * 注意: 需在tsup.config.ts加入noExternal: ['lodash-es'],确保打包时不被忽略
6
- */
7
- export * from 'lodash-es';