@cloudcome/utils-core 1.1.1 → 1.2.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/array.cjs +129 -0
- package/dist/array.cjs.map +1 -0
- package/dist/array.d.ts +171 -0
- package/dist/array.mjs +129 -0
- package/dist/array.mjs.map +1 -0
- package/dist/async.cjs +219 -0
- package/dist/async.cjs.map +1 -0
- package/dist/async.d.ts +137 -0
- package/dist/async.mjs +219 -0
- package/dist/async.mjs.map +1 -0
- package/dist/base64.cjs +16 -0
- package/dist/base64.cjs.map +1 -0
- package/dist/base64.d.ts +7 -0
- package/dist/base64.mjs +16 -0
- package/dist/base64.mjs.map +1 -0
- package/dist/cache.cjs +79 -0
- package/dist/cache.cjs.map +1 -0
- package/dist/cache.d.ts +90 -0
- package/dist/cache.mjs +79 -0
- package/dist/cache.mjs.map +1 -0
- package/{src/color/contrast.ts → dist/color/contrast.d.ts} +2 -12
- package/dist/color/distance.d.ts +8 -0
- package/dist/color/helpers.d.ts +2 -0
- package/dist/color/hex-hsl.d.ts +3 -0
- package/{src/color/hex-hsv.ts → dist/color/hex-hsv.d.ts} +3 -11
- package/{src/color/hex-hwb.ts → dist/color/hex-hwb.d.ts} +3 -11
- package/dist/color/hex-rgb.d.ts +18 -0
- package/{src/color/hsl-lighten.ts → dist/color/hsl-lighten.d.ts} +2 -7
- package/{src/color/hsv-brighten.ts → dist/color/hsv-brighten.d.ts} +2 -7
- package/{src/color/luminance.ts → dist/color/luminance.d.ts} +2 -9
- package/{src/color/mix.ts → dist/color/mix.d.ts} +2 -10
- package/dist/color/rgb-hsl.d.ts +23 -0
- package/{src/color/rgb-hsv.ts → dist/color/rgb-hsv.d.ts} +3 -30
- package/dist/color/rgb-hwb.d.ts +29 -0
- package/{src/color/rgb-lab.ts → dist/color/rgb-lab.d.ts} +3 -11
- package/dist/color/rgb-whiter.d.ts +12 -0
- package/dist/color/rgb-xyz.d.ts +22 -0
- package/{src/color/types.ts → dist/color/types.d.ts} +30 -12
- package/{src/color/xyz-lab.ts → dist/color/xyz-lab.d.ts} +3 -32
- package/dist/color.cjs +250 -0
- package/dist/color.cjs.map +1 -0
- package/dist/color.mjs +250 -0
- package/dist/color.mjs.map +1 -0
- package/dist/const.cjs +14 -0
- package/dist/const.cjs.map +1 -0
- package/dist/const.mjs +15 -0
- package/dist/const.mjs.map +1 -0
- package/dist/core.cjs +250 -0
- package/dist/core.cjs.map +1 -0
- package/dist/core.mjs +251 -0
- package/dist/core.mjs.map +1 -0
- package/dist/crypto/md5.d.mts +1 -0
- package/dist/crypto/sha1.d.mts +1 -0
- package/dist/crypto/sha256.d.mts +1 -0
- package/dist/crypto/sha512.d.mts +1 -0
- package/dist/crypto.cjs +812 -0
- package/dist/crypto.cjs.map +1 -0
- package/{src/crypto.ts → dist/crypto.d.ts} +4 -20
- package/dist/crypto.mjs +812 -0
- package/dist/crypto.mjs.map +1 -0
- package/dist/date/const.d.ts +6 -0
- package/dist/date/core.d.ts +52 -0
- package/dist/date/days.d.ts +23 -0
- package/{src/date/is.ts → dist/date/is.d.ts} +8 -102
- package/dist/date/relative.d.ts +44 -0
- package/dist/date/start-end.d.ts +73 -0
- package/dist/date/timezone.d.ts +67 -0
- package/dist/date/weeks.d.ts +72 -0
- package/dist/date.cjs +239 -0
- package/dist/date.cjs.map +1 -0
- package/dist/date.mjs +241 -0
- package/dist/date.mjs.map +1 -0
- package/dist/dict.cjs +2 -0
- package/dist/dict.cjs.map +1 -0
- package/dist/dict.mjs +2 -0
- package/dist/dict.mjs.map +1 -0
- package/dist/each.cjs +18 -0
- package/dist/each.cjs.map +1 -0
- package/dist/each.mjs +19 -0
- package/dist/each.mjs.map +1 -0
- package/dist/easing.cjs +151 -0
- package/dist/easing.cjs.map +1 -0
- package/dist/easing.d.ts +46 -0
- package/dist/easing.mjs +151 -0
- package/dist/easing.mjs.map +1 -0
- package/dist/emitter.cjs +94 -0
- package/dist/emitter.cjs.map +1 -0
- package/dist/emitter.d.ts +68 -0
- package/dist/emitter.mjs +94 -0
- package/dist/emitter.mjs.map +1 -0
- package/dist/enum.cjs +58 -0
- package/dist/enum.cjs.map +1 -0
- package/dist/enum.d.ts +68 -0
- package/dist/enum.mjs +58 -0
- package/dist/enum.mjs.map +1 -0
- package/dist/env.cjs +28 -0
- package/dist/env.cjs.map +1 -0
- package/{src/env.ts → dist/env.d.ts} +6 -30
- package/dist/env.mjs +28 -0
- package/dist/env.mjs.map +1 -0
- package/dist/error.cjs +12 -0
- package/dist/error.cjs.map +1 -0
- package/{src/error.ts → dist/error.d.ts} +3 -12
- package/dist/error.mjs +12 -0
- package/dist/error.mjs.map +1 -0
- package/dist/exception.cjs +22 -0
- package/dist/exception.cjs.map +1 -0
- package/dist/exception.d.ts +31 -0
- package/dist/exception.mjs +22 -0
- package/dist/exception.mjs.map +1 -0
- package/dist/fn.cjs +76 -0
- package/dist/fn.cjs.map +1 -0
- package/dist/fn.d.ts +102 -0
- package/dist/fn.mjs +76 -0
- package/dist/fn.mjs.map +1 -0
- package/dist/index.cjs +5 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.mjs +5 -0
- package/dist/index.mjs.map +1 -0
- package/dist/merge.cjs +87 -0
- package/dist/merge.cjs.map +1 -0
- package/dist/merge.mjs +88 -0
- package/dist/merge.mjs.map +1 -0
- package/dist/number.cjs +14 -0
- package/dist/number.cjs.map +1 -0
- package/dist/number.d.ts +153 -0
- package/dist/number.mjs +14 -0
- package/dist/number.mjs.map +1 -0
- package/{src/object/each.ts → dist/object/each.d.ts} +3 -23
- package/dist/object/get-set.d.ts +111 -0
- package/dist/object/is.d.ts +32 -0
- package/dist/object/merge.d.ts +72 -0
- package/{src/object/process.ts → dist/object/process.d.ts} +4 -38
- package/dist/object.cjs +130 -0
- package/dist/object.cjs.map +1 -0
- package/dist/object.mjs +130 -0
- package/dist/object.mjs.map +1 -0
- package/dist/path.cjs +77 -0
- package/dist/path.cjs.map +1 -0
- package/dist/path.d.ts +82 -0
- package/dist/path.mjs +77 -0
- package/dist/path.mjs.map +1 -0
- package/dist/promise.cjs +62 -0
- package/dist/promise.cjs.map +1 -0
- package/{src/promise.ts → dist/promise.d.ts} +6 -67
- package/dist/promise.mjs +62 -0
- package/dist/promise.mjs.map +1 -0
- package/dist/qs.cjs +47 -0
- package/dist/qs.cjs.map +1 -0
- package/{src/qs.ts → dist/qs.d.ts} +3 -60
- package/dist/qs.mjs +47 -0
- package/dist/qs.mjs.map +1 -0
- package/dist/regexp.cjs +66 -0
- package/dist/regexp.cjs.map +1 -0
- package/dist/regexp.d.ts +65 -0
- package/dist/regexp.mjs +66 -0
- package/dist/regexp.mjs.map +1 -0
- package/dist/string.cjs +16 -0
- package/dist/string.cjs.map +1 -0
- package/dist/string.d.ts +80 -0
- package/dist/string.mjs +16 -0
- package/dist/string.mjs.map +1 -0
- package/dist/string2.cjs +157 -0
- package/dist/string2.cjs.map +1 -0
- package/dist/string2.mjs +158 -0
- package/dist/string2.mjs.map +1 -0
- package/dist/time/from.d.ts +14 -0
- package/dist/time/to.d.ts +38 -0
- package/dist/time.cjs +82 -0
- package/dist/time.cjs.map +1 -0
- package/dist/time.mjs +82 -0
- package/dist/time.mjs.map +1 -0
- package/dist/timer.cjs +119 -0
- package/dist/timer.cjs.map +1 -0
- package/dist/timer.d.ts +96 -0
- package/{src/timer.ts → dist/timer.mjs} +17 -124
- package/dist/timer.mjs.map +1 -0
- package/dist/tree.cjs +125 -0
- package/dist/tree.cjs.map +1 -0
- package/{src/tree.ts → dist/tree.d.ts} +41 -225
- package/dist/tree.mjs +125 -0
- package/dist/tree.mjs.map +1 -0
- package/dist/type.cjs +78 -0
- package/dist/type.cjs.map +1 -0
- package/{src/type.ts → dist/type.d.ts} +20 -96
- package/dist/type.mjs +78 -0
- package/dist/type.mjs.map +1 -0
- package/dist/types.cjs +2 -0
- package/dist/types.cjs.map +1 -0
- package/{src/types.ts → dist/types.d.ts} +12 -33
- package/dist/types.mjs +2 -0
- package/dist/types.mjs.map +1 -0
- package/dist/unique.cjs +46 -0
- package/dist/unique.cjs.map +1 -0
- package/dist/unique.d.ts +22 -0
- package/dist/unique.mjs +46 -0
- package/dist/unique.mjs.map +1 -0
- package/dist/url.cjs +37 -0
- package/dist/url.cjs.map +1 -0
- package/dist/url.d.ts +53 -0
- package/dist/url.mjs +37 -0
- package/dist/url.mjs.map +1 -0
- package/dist/version.cjs +33 -0
- package/dist/version.cjs.map +1 -0
- package/dist/version.d.ts +32 -0
- package/dist/version.mjs +33 -0
- package/dist/version.mjs.map +1 -0
- package/package.json +8 -2
- package/CHANGELOG.md +0 -52
- package/src/array.ts +0 -312
- package/src/async.ts +0 -379
- package/src/base64.ts +0 -20
- package/src/cache.ts +0 -146
- package/src/color/distance.ts +0 -28
- package/src/color/helpers.ts +0 -23
- package/src/color/hex-hsl.ts +0 -11
- package/src/color/hex-rgb.ts +0 -39
- package/src/color/rgb-hsl.ts +0 -53
- package/src/color/rgb-hwb.ts +0 -56
- package/src/color/rgb-whiter.ts +0 -22
- package/src/color/rgb-xyz.ts +0 -62
- package/src/crypto/md5.mjs +0 -357
- package/src/crypto/sha1.mjs +0 -300
- package/src/crypto/sha256.mjs +0 -310
- package/src/crypto/sha512.mjs +0 -459
- package/src/date/const.ts +0 -6
- package/src/date/core.ts +0 -162
- package/src/date/days.ts +0 -51
- package/src/date/relative.ts +0 -92
- package/src/date/start-end.ts +0 -246
- package/src/date/timezone.ts +0 -220
- package/src/date/weeks.ts +0 -100
- package/src/dts/global.d.ts +0 -27
- package/src/easing.ts +0 -166
- package/src/emitter.ts +0 -117
- package/src/enum.ts +0 -171
- package/src/exception.ts +0 -68
- package/src/fn.ts +0 -197
- package/src/index.ts +0 -1
- package/src/number.ts +0 -236
- package/src/object/get-set.ts +0 -273
- package/src/object/is.ts +0 -128
- package/src/object/merge.ts +0 -180
- package/src/path.ts +0 -188
- package/src/regexp.ts +0 -156
- package/src/string.ts +0 -146
- package/src/time/from.ts +0 -57
- package/src/time/to.ts +0 -106
- package/src/unique.ts +0 -77
- package/src/url.ts +0 -93
- package/src/version.ts +0 -71
- package/test/array.test.ts +0 -332
- package/test/async-real.test.ts +0 -39
- package/test/async.test.ts +0 -375
- package/test/base64.test.ts +0 -32
- package/test/cache.test.ts +0 -83
- package/test/color.test.ts +0 -163
- package/test/crypto.test.ts +0 -34
- package/test/date-tz.test.ts +0 -206
- package/test/date.test.ts +0 -353
- package/test/easing.test.ts +0 -33
- package/test/emitter.test.ts +0 -71
- package/test/enum.test.ts +0 -113
- package/test/env.test.ts +0 -69
- package/test/error.test.ts +0 -58
- package/test/exception.test.ts +0 -43
- package/test/fn.test.ts +0 -263
- package/test/helpers.ts +0 -23
- package/test/index.test.ts +0 -6
- package/test/number.test.ts +0 -213
- package/test/object.test.ts +0 -309
- package/test/path.test.ts +0 -156
- package/test/promise.test.ts +0 -199
- package/test/qs.test.ts +0 -79
- package/test/regexp.test.ts +0 -97
- package/test/string.test.ts +0 -150
- package/test/time.test.ts +0 -214
- package/test/timer.test.ts +0 -114
- package/test/tree.test.ts +0 -348
- package/test/type.test.ts +0 -226
- package/test/unique.test.ts +0 -71
- package/test/url.test.ts +0 -136
- package/test/version.test.ts +0 -52
- package/tsconfig.json +0 -31
- package/vite.config.mts +0 -114
- /package/{src/color.ts → dist/color.d.ts} +0 -0
- /package/{src/date.ts → dist/date.d.ts} +0 -0
- /package/{src/dict.ts → dist/dict.d.ts} +0 -0
- /package/{src/object.ts → dist/object.d.ts} +0 -0
- /package/{src/time.ts → dist/time.d.ts} +0 -0
package/src/string.ts
DELETED
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
import { numberConvert, randomNumber } from './number';
|
|
2
|
-
import { isFunction, isNullish, isNumber, isObject, isString, isUndefined } from './type';
|
|
3
|
-
|
|
4
|
-
export const STRING_ARABIC_NUMERALS = '0123456789';
|
|
5
|
-
export const STRING_HEXADECIMALS = '0123456789abcdef';
|
|
6
|
-
export const STRING_LOWERCASE_ALPHA = 'abcdefghijklmnopqrstuvwxyz';
|
|
7
|
-
export const STRING_UPPERCASE_ALPHA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
8
|
-
export const STRING_DICT = `${STRING_ARABIC_NUMERALS + STRING_UPPERCASE_ALPHA + STRING_LOWERCASE_ALPHA}`;
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* 将字符串转换为驼峰格式
|
|
12
|
-
* @param {string} string - 要转换的字符串
|
|
13
|
-
* @param {boolean} [bigger] - 是否大写第一个字母,默认为 false
|
|
14
|
-
* @returns {string} - 转换后的驼峰格式字符串
|
|
15
|
-
*/
|
|
16
|
-
export function stringCamelCase(string: string, bigger?: boolean): string {
|
|
17
|
-
const string2 = string.replace(/[\s_-](.)/g, (_, char) => (char as string).toUpperCase());
|
|
18
|
-
return bigger ? string2.slice(0, 1).toUpperCase() + string2.slice(1) : string2;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* 将字符串转换为连字格式
|
|
23
|
-
* @param {string} string - 要转换的字符串
|
|
24
|
-
* @param {string} [separator] - 分隔符,默认是 "-"(短横线)
|
|
25
|
-
* @returns {string} - 转换后的连字格式字符串
|
|
26
|
-
*/
|
|
27
|
-
export function stringKebabCase(string: string, separator = '-'): string {
|
|
28
|
-
return string.replace(/[A-Z]/g, (origin) => `${separator}${origin.toLowerCase()}`);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* 生成随机字符串
|
|
33
|
-
* @param {number} length - 生成的随机字符串长度
|
|
34
|
-
* @param {string} [dict] - 用于生成随机字符串的字符字典,默认为数字、小写字母和大写字母的组合
|
|
35
|
-
* @returns {string} - 生成的随机字符串
|
|
36
|
-
* @example
|
|
37
|
-
* randomString(10); // 生成一个长度为 10 的随机字符串
|
|
38
|
-
* randomString(8, 'ABCDEF'); // 生成一个长度为 8 的随机字符串,仅包含字符 'ABCDEF'
|
|
39
|
-
*/
|
|
40
|
-
export function randomString(length: number, dict?: string): string {
|
|
41
|
-
const dictFinal = dict || STRING_DICT;
|
|
42
|
-
const dictLength = dictFinal.length;
|
|
43
|
-
|
|
44
|
-
let result = '';
|
|
45
|
-
|
|
46
|
-
for (let i = 0; i < length; i++) {
|
|
47
|
-
result += dictFinal.charAt(randomNumber(0, dictLength - 1));
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
return result;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* 简单的模板引擎,类似于 Python 的 `.format()` 方法
|
|
55
|
-
* 支持通过索引或对象/名称的方式传递变量
|
|
56
|
-
* 当使用对象/名称方式时,可以传递一个回退值作为第三个参数
|
|
57
|
-
*
|
|
58
|
-
* @category 字符串
|
|
59
|
-
* @example
|
|
60
|
-
* ```
|
|
61
|
-
* // 索引方式
|
|
62
|
-
* const result = stringFormat(
|
|
63
|
-
* '你好 {0}!我的名字是 {1}。',
|
|
64
|
-
* '张三',
|
|
65
|
-
* '李四'
|
|
66
|
-
* ); // 你好 张三!我的名字是 李四。
|
|
67
|
-
* ```
|
|
68
|
-
*
|
|
69
|
-
* @example
|
|
70
|
-
* ```
|
|
71
|
-
* // 对象方式
|
|
72
|
-
* const result = stringFormat(
|
|
73
|
-
* '{greet}!我的名字是 {name}。',
|
|
74
|
-
* { greet: '你好', name: '王五' }
|
|
75
|
-
* ); // 你好!我的名字是 王五。
|
|
76
|
-
* ```
|
|
77
|
-
*
|
|
78
|
-
* @example
|
|
79
|
-
* ```
|
|
80
|
-
* // 带回退值的对象方式
|
|
81
|
-
* const result = stringFormat(
|
|
82
|
-
* '{greet}!我的名字是 {name}。',
|
|
83
|
-
* { greet: '你好' }, // name 未传递,因此会使用回退值
|
|
84
|
-
* '未知'
|
|
85
|
-
* ); // 你好!我的名字是 未知。
|
|
86
|
-
* ```
|
|
87
|
-
*/
|
|
88
|
-
export function stringFormat(
|
|
89
|
-
str: string,
|
|
90
|
-
object: Record<string | number, unknown>,
|
|
91
|
-
fallback?: string | ((key: string) => string),
|
|
92
|
-
): string;
|
|
93
|
-
export function stringFormat(str: string, ...args: (string | number | bigint | undefined | null)[]): string;
|
|
94
|
-
export function stringFormat(str: string, ...args: unknown[]): string {
|
|
95
|
-
const [firstArg, fallback] = args;
|
|
96
|
-
|
|
97
|
-
if (isObject(firstArg) || isUndefined(firstArg)) {
|
|
98
|
-
const vars = firstArg || {};
|
|
99
|
-
return str.replace(/\{(\w+)\}/g, (_, key) => vars[key] ?? (isFunction(fallback) ? fallback(key) : fallback) ?? key);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return str.replace(/\{(\d+)\}/g, (_, key) => {
|
|
103
|
-
const index = Number(key);
|
|
104
|
-
if (Number.isNaN(index)) return key;
|
|
105
|
-
return args[index];
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* 生成符合 [RFC 4122](https://www.ietf.org/rfc/rfc4122.txt) 版本 4 的 UUID 字符串
|
|
111
|
-
* @returns {string} - 生成的 UUID 字符串
|
|
112
|
-
* @example
|
|
113
|
-
* const uuid = randomUUID4();
|
|
114
|
-
* console.log(uuid); // 输出类似 '123e4567-e89b-12d3-a456-426614174000' 的 UUID 字符串
|
|
115
|
-
*/
|
|
116
|
-
export function randomUUID4(): string {
|
|
117
|
-
const template = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
|
|
118
|
-
let result = '';
|
|
119
|
-
|
|
120
|
-
for (let i = 0; i < template.length; i++) {
|
|
121
|
-
const t = template[i];
|
|
122
|
-
|
|
123
|
-
if (t === '-' || t === '4') {
|
|
124
|
-
result += t;
|
|
125
|
-
continue;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
if (t === 'y') {
|
|
129
|
-
result += randomString(1, '89ab');
|
|
130
|
-
continue;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
result += randomString(1, STRING_HEXADECIMALS);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
return result;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* 将值转换为字符串,若值为 null 或 undefined 则返回空字符串
|
|
141
|
-
* @param {unknown} value - 需要转换的值
|
|
142
|
-
* @returns {string} 转换后的字符串结果
|
|
143
|
-
*/
|
|
144
|
-
export function stringify(value: unknown) {
|
|
145
|
-
return isNullish(value) ? '' : String(value);
|
|
146
|
-
}
|
package/src/time/from.ts
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
// @rer https://day.js.org/docs/en/durations/creating
|
|
2
|
-
|
|
3
|
-
import { DATE_DAY_MS, DATE_HOUR_MS, DATE_MINUTE_MS, DATE_MONTH_MS, DATE_SECOND_MS, DATE_YEAR_MS } from '@/date';
|
|
4
|
-
import { isString } from '@/type';
|
|
5
|
-
import type { TTimeDuration } from './to';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* 时间转换规则数组
|
|
9
|
-
* @type {Array<[RegExp, (match: RegExpMatchArray) => number]>}
|
|
10
|
-
* @property {RegExp} 0 - 匹配时间单位正则表达式
|
|
11
|
-
* @property {function} 1 - 将匹配结果转换为毫秒数的函数
|
|
12
|
-
*/
|
|
13
|
-
const rules: [key: keyof TTimeDuration, time: number][] = [
|
|
14
|
-
['years', DATE_YEAR_MS],
|
|
15
|
-
['months', DATE_MONTH_MS],
|
|
16
|
-
['days', DATE_DAY_MS],
|
|
17
|
-
['hours', DATE_HOUR_MS],
|
|
18
|
-
['minutes', DATE_MINUTE_MS],
|
|
19
|
-
['seconds', DATE_SECOND_MS],
|
|
20
|
-
];
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* 将时间持续时间字符串或对象转换为毫秒数
|
|
24
|
-
*
|
|
25
|
-
* @param duration - 可以是时间持续时间字符串(如 '1d2h')或 TTimeDuration 对象
|
|
26
|
-
* @returns 计算得到的总毫秒数
|
|
27
|
-
*/
|
|
28
|
-
export function timeFrom(duration: string | TTimeDuration) {
|
|
29
|
-
const td = isString(duration) ? timeParse(duration) : duration;
|
|
30
|
-
return rules.reduce((acc, [key, time]) => acc + (td[key] || 0) * time, 0);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const durationMatchRules: [RegExp, key: keyof TTimeDuration][] = [
|
|
34
|
-
[/(\d+)y/i, 'years'],
|
|
35
|
-
[/(\d+)M/, 'months'],
|
|
36
|
-
[/(\d+)d/i, 'days'],
|
|
37
|
-
[/(\d+)h/i, 'hours'],
|
|
38
|
-
[/(\d+)m/, 'minutes'],
|
|
39
|
-
[/(\d+)s/, 'seconds'],
|
|
40
|
-
];
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* 将时长字符串解析为时间对象
|
|
44
|
-
* @param duration - 时长字符串(例如 "1h30m")
|
|
45
|
-
* @returns 包含解析后时间单位的对象(小时、分钟等)
|
|
46
|
-
*/
|
|
47
|
-
export function timeParse(duration: string) {
|
|
48
|
-
const result = {} as TTimeDuration;
|
|
49
|
-
|
|
50
|
-
for (const [regex, key] of durationMatchRules) {
|
|
51
|
-
const match = duration.match(regex);
|
|
52
|
-
if (match) result[key] = Number(match[1]);
|
|
53
|
-
else result[key] = 0;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
return result;
|
|
57
|
-
}
|
package/src/time/to.ts
DELETED
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
import { DATE_DAY_MS, DATE_HOUR_MS, DATE_MINUTE_MS, DATE_SECOND_MS } from '../date';
|
|
2
|
-
|
|
3
|
-
export type TTimeDuration = {
|
|
4
|
-
years: number;
|
|
5
|
-
months: number;
|
|
6
|
-
/** 天数 */
|
|
7
|
-
days: number;
|
|
8
|
-
/** 小时数 */
|
|
9
|
-
hours: number;
|
|
10
|
-
/** 分钟数 */
|
|
11
|
-
minutes: number;
|
|
12
|
-
/** 秒数 */
|
|
13
|
-
seconds: number;
|
|
14
|
-
/** 毫秒数 */
|
|
15
|
-
milliseconds: number;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
type _TTimeParsePoint = 'D' | 'h' | 'm' | 's' | 'S';
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* 解析时间毫秒数为绝对时间对象
|
|
22
|
-
* @param timeMs 时间毫秒数
|
|
23
|
-
* @param maxPoint 最大时间单位(决定分解的起始单位)
|
|
24
|
-
* @returns 包含时间单位分解结果的对象
|
|
25
|
-
* @example
|
|
26
|
-
* ```typescript
|
|
27
|
-
* // 默认以天为最大单位分解
|
|
28
|
-
* timeInDay(123456789);
|
|
29
|
-
* // { days: 1, hours: 10, minutes: 17, seconds: 36, milliseconds: 789 }
|
|
30
|
-
*
|
|
31
|
-
* // 指定最大单位为分钟,分解到分钟及以下单位
|
|
32
|
-
* timeInMinute(123456789);
|
|
33
|
-
* // { days: 0, hours: 0, minutes: 2057, seconds: 36, milliseconds: 789 }
|
|
34
|
-
* ```
|
|
35
|
-
*/
|
|
36
|
-
function _timeAbsolute(timeMs: number, maxPoint: _TTimeParsePoint): TTimeDuration {
|
|
37
|
-
const minPoint: _TTimeParsePoint = 'S';
|
|
38
|
-
|
|
39
|
-
const defines: [point: _TTimeParsePoint, key: keyof TTimeDuration, base: number][] = [
|
|
40
|
-
['D', 'days', DATE_DAY_MS],
|
|
41
|
-
['h', 'hours', DATE_HOUR_MS],
|
|
42
|
-
['m', 'minutes', DATE_MINUTE_MS],
|
|
43
|
-
['s', 'seconds', DATE_SECOND_MS],
|
|
44
|
-
['S', 'milliseconds', 1],
|
|
45
|
-
] as const;
|
|
46
|
-
|
|
47
|
-
const minIndex = defines.findIndex((item) => item[0] === maxPoint);
|
|
48
|
-
const maxIndex = defines.findIndex((item) => item[0] === minPoint);
|
|
49
|
-
|
|
50
|
-
let timeMsFinal = timeMs;
|
|
51
|
-
const dao: TTimeDuration = {
|
|
52
|
-
years: 0,
|
|
53
|
-
months: 0,
|
|
54
|
-
days: 0,
|
|
55
|
-
hours: 0,
|
|
56
|
-
minutes: 0,
|
|
57
|
-
seconds: 0,
|
|
58
|
-
milliseconds: 0,
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
for (let i = minIndex; i <= maxIndex; i++) {
|
|
62
|
-
const mode = defines[i];
|
|
63
|
-
const base = mode[2];
|
|
64
|
-
const value = Math.floor(timeMsFinal / base);
|
|
65
|
-
timeMsFinal = timeMsFinal - value * base;
|
|
66
|
-
dao[mode[1]] = value;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
return dao;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* 将时间毫秒数解析为以天为最大单位的绝对时间对象
|
|
74
|
-
* @param timeMs 时间毫秒数
|
|
75
|
-
* @returns 包含天/小时/分钟/秒/毫秒分解结果的对象
|
|
76
|
-
*/
|
|
77
|
-
export function timeToDays(timeMs: number) {
|
|
78
|
-
return _timeAbsolute(timeMs, 'D');
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* 将时间毫秒数解析为以小时为最大单位的绝对时间对象
|
|
83
|
-
* @param timeMs 时间毫秒数
|
|
84
|
-
* @returns 包含小时/分钟/秒/毫秒分解结果的对象
|
|
85
|
-
*/
|
|
86
|
-
export function timeToHours(timeMs: number) {
|
|
87
|
-
return _timeAbsolute(timeMs, 'h');
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* 将时间毫秒数解析为以分钟为最大单位的绝对时间对象
|
|
92
|
-
* @param timeMs 时间毫秒数
|
|
93
|
-
* @returns 包含分钟/秒/毫秒分解结果的对象
|
|
94
|
-
*/
|
|
95
|
-
export function timeToMinutes(timeMs: number) {
|
|
96
|
-
return _timeAbsolute(timeMs, 'm');
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* 将时间毫秒数解析为以秒为最大单位的绝对时间对象
|
|
101
|
-
* @param timeMs 时间毫秒数
|
|
102
|
-
* @returns 包含秒/毫秒分解结果的对象
|
|
103
|
-
*/
|
|
104
|
-
export function timeToSeconds(timeMs: number) {
|
|
105
|
-
return _timeAbsolute(timeMs, 's');
|
|
106
|
-
}
|
package/src/unique.ts
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import { numberConvert, randomNumber } from './number';
|
|
2
|
-
import { STRING_DICT } from './string';
|
|
3
|
-
import { isNumber, isString } from './type';
|
|
4
|
-
|
|
5
|
-
let lastTimestamp = 0;
|
|
6
|
-
let lastSafePadding = 0;
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* 生成一个唯一的 BigInt 值。
|
|
10
|
-
*
|
|
11
|
-
* @param randomLength - 可选参数,指定随机部分的长度,默认为 0。
|
|
12
|
-
* 如果大于 0,则会在结果中附加指定长度的随机数。
|
|
13
|
-
* @returns 返回一个唯一的 BigInt 值,包含时间戳、随机部分(如果有)以及递增的填充值。
|
|
14
|
-
*/
|
|
15
|
-
export function uniqueBigInt(randomLength = 0): bigint {
|
|
16
|
-
const now = Date.now();
|
|
17
|
-
|
|
18
|
-
if (now !== lastTimestamp) {
|
|
19
|
-
lastTimestamp = now;
|
|
20
|
-
lastSafePadding = 0;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
let randomPart = '';
|
|
24
|
-
|
|
25
|
-
if (randomLength > 0) {
|
|
26
|
-
const randomMin = 10 ** (randomLength - 1);
|
|
27
|
-
const randomMax = 10 ** randomLength - 1;
|
|
28
|
-
randomPart = String(randomNumber(randomMin, randomMax));
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
return BigInt(randomPart + lastSafePadding++ + now);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const _randomChar = (dict: string): string => {
|
|
35
|
-
const poolIndex = randomNumber(0, dict.length - 1);
|
|
36
|
-
|
|
37
|
-
return dict[poolIndex];
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* 生成一个唯一的字符串。
|
|
42
|
-
*
|
|
43
|
-
* @param minLength - 可选参数,指定生成字符串的最小长度。
|
|
44
|
-
* 如果未提供或为字符串类型,则使用默认长度。
|
|
45
|
-
* @param dict - 可选参数,指定生成字符串时使用的字符集。
|
|
46
|
-
* 如果未提供,则使用默认字符集 `STRING_DICT`。
|
|
47
|
-
* @returns 返回一个唯一的字符串,包含时间戳、随机部分(如果有)以及递增的填充值。
|
|
48
|
-
* 如果指定了 `length`,则返回值的长度将被调整到指定长度。
|
|
49
|
-
*/
|
|
50
|
-
export function uniqueString(minLength: number, dict: string): string;
|
|
51
|
-
export function uniqueString(minLength: number): string;
|
|
52
|
-
export function uniqueString(dict: string): string;
|
|
53
|
-
export function uniqueString(): string;
|
|
54
|
-
export function uniqueString(minLength?: number | string, dict?: string) {
|
|
55
|
-
let finalLength = 0;
|
|
56
|
-
let finalDict = STRING_DICT;
|
|
57
|
-
|
|
58
|
-
if (isString(dict)) {
|
|
59
|
-
finalLength = minLength as number;
|
|
60
|
-
finalDict = dict;
|
|
61
|
-
} else if (isNumber(minLength)) {
|
|
62
|
-
finalLength = minLength;
|
|
63
|
-
} else if (isString(minLength)) {
|
|
64
|
-
finalDict = minLength;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
let uniqueString = numberConvert(uniqueBigInt(), finalDict);
|
|
68
|
-
let insertLength = finalLength - uniqueString.length;
|
|
69
|
-
|
|
70
|
-
if (insertLength <= 0) return uniqueString;
|
|
71
|
-
|
|
72
|
-
while (insertLength--) {
|
|
73
|
-
uniqueString += _randomChar(finalDict);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
return uniqueString;
|
|
77
|
-
}
|
package/src/url.ts
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 表示解析后的 URL 组件。
|
|
3
|
-
*/
|
|
4
|
-
export type TURLMeta = {
|
|
5
|
-
/**
|
|
6
|
-
* 协议部分,包含冒号,例如 "https:"。
|
|
7
|
-
*/
|
|
8
|
-
protocol: string;
|
|
9
|
-
/**
|
|
10
|
-
* 主机部分,包括主机名和端口。
|
|
11
|
-
*/
|
|
12
|
-
host: string;
|
|
13
|
-
/**
|
|
14
|
-
* 主机名部分。
|
|
15
|
-
*/
|
|
16
|
-
hostname: string;
|
|
17
|
-
/**
|
|
18
|
-
* 端口部分。
|
|
19
|
-
*/
|
|
20
|
-
port: string;
|
|
21
|
-
/**
|
|
22
|
-
* 路径部分。
|
|
23
|
-
*/
|
|
24
|
-
pathname: string;
|
|
25
|
-
/**
|
|
26
|
-
* 查询字符串部分。
|
|
27
|
-
*/
|
|
28
|
-
search: string;
|
|
29
|
-
/**
|
|
30
|
-
* 哈希部分。
|
|
31
|
-
*/
|
|
32
|
-
hash: string;
|
|
33
|
-
/**
|
|
34
|
-
* 用户名部分。
|
|
35
|
-
*/
|
|
36
|
-
username: string;
|
|
37
|
-
/**
|
|
38
|
-
* 密码部分。
|
|
39
|
-
*/
|
|
40
|
-
password: string;
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* 解析 URL 字符串为组件对象。
|
|
45
|
-
* @param url - 需要解析的 URL 字符串。
|
|
46
|
-
* @returns 包含解析后 URL 组件的对象。
|
|
47
|
-
*/
|
|
48
|
-
export function urlParse(url: string): TURLMeta {
|
|
49
|
-
let result: URL | null = null;
|
|
50
|
-
|
|
51
|
-
try {
|
|
52
|
-
// 添加 globalThis 是便于对接外部环境 URL 的自行实现
|
|
53
|
-
// 例如在 uni-app、微信小程序等运行环境。
|
|
54
|
-
result = new globalThis.URL(url);
|
|
55
|
-
} catch (e) {
|
|
56
|
-
// ignore
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const protocol = result?.protocol || '';
|
|
60
|
-
const host = result?.host || '';
|
|
61
|
-
|
|
62
|
-
return {
|
|
63
|
-
protocol,
|
|
64
|
-
host,
|
|
65
|
-
hostname: result?.hostname || '',
|
|
66
|
-
port: result?.port || '',
|
|
67
|
-
pathname: result?.pathname || '',
|
|
68
|
-
search: result?.search || '',
|
|
69
|
-
hash: result?.hash || '',
|
|
70
|
-
username: result?.username || '',
|
|
71
|
-
password: result?.password || '',
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* 将 URLInfo 对象转换回 URL 字符串。
|
|
77
|
-
* @param url - 需要转换的 URLInfo 对象。
|
|
78
|
-
* @returns 转换后的 URL 字符串。
|
|
79
|
-
*/
|
|
80
|
-
export function urlStringify(url: TURLMeta) {
|
|
81
|
-
const { protocol, hostname, port, pathname, search, hash, username, password } = url;
|
|
82
|
-
return [
|
|
83
|
-
protocol ? `${protocol}//` : '',
|
|
84
|
-
username && password ? `${username}:${password}@` : '',
|
|
85
|
-
hostname,
|
|
86
|
-
port ? `:${port}` : '',
|
|
87
|
-
pathname,
|
|
88
|
-
search,
|
|
89
|
-
hash,
|
|
90
|
-
]
|
|
91
|
-
.filter(Boolean)
|
|
92
|
-
.join('');
|
|
93
|
-
}
|
package/src/version.ts
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import { isUndefined } from './type';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 表示包含主版本号、次版本号和修订号的对象
|
|
5
|
-
*/
|
|
6
|
-
export type TVersionObject = {
|
|
7
|
-
/**
|
|
8
|
-
* 主版本号,当有不兼容的API修改时递增
|
|
9
|
-
*/
|
|
10
|
-
major: number;
|
|
11
|
-
/**
|
|
12
|
-
* 次版本号,当有向下兼容的功能新增时递增
|
|
13
|
-
*/
|
|
14
|
-
minor: number;
|
|
15
|
-
/**
|
|
16
|
-
* 修订号,当有向下兼容的问题修正时递增
|
|
17
|
-
*/
|
|
18
|
-
patch: number;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* 将数字转换为安全的数值,如果输入为NaN则返回0
|
|
23
|
-
* @param n - 要转换的数字
|
|
24
|
-
* @returns 安全的数字(如果输入为NaN则返回0)
|
|
25
|
-
*/
|
|
26
|
-
function internal_numerical(n?: number) {
|
|
27
|
-
return Number.isNaN(n) || isUndefined(n) ? 0 : n;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* 将语义化版本号字符串解析为VersionObject对象
|
|
32
|
-
* @param version - 要解析的版本号字符串 (例如 "1.2.3")
|
|
33
|
-
* @returns 包含主版本号、次版本号和修订号的对象
|
|
34
|
-
* @throws 如果版本号字符串格式无效将抛出错误
|
|
35
|
-
*/
|
|
36
|
-
export function versionParse(version: string): TVersionObject {
|
|
37
|
-
const [major, minor, patch] = version.split('.').map(Number);
|
|
38
|
-
return {
|
|
39
|
-
major: internal_numerical(major),
|
|
40
|
-
minor: internal_numerical(minor),
|
|
41
|
-
patch: internal_numerical(patch),
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* 比较两个语义化版本号字符串
|
|
47
|
-
* @param version1 - 要比较的第一个版本号字符串
|
|
48
|
-
* @param version2 - 要比较的第二个版本号字符串
|
|
49
|
-
* @returns 如果version1较大返回1,version2较大返回-1,相等返回0
|
|
50
|
-
* @throws 如果任一版本号字符串格式无效将抛出错误
|
|
51
|
-
*/
|
|
52
|
-
export function versionCompare(version1: string, version2: string): number {
|
|
53
|
-
const vo1 = versionParse(version1);
|
|
54
|
-
const vo2 = versionParse(version2);
|
|
55
|
-
const order: (keyof TVersionObject)[] = ['major', 'minor', 'patch'];
|
|
56
|
-
|
|
57
|
-
for (const key of order) {
|
|
58
|
-
const n1 = vo1[key];
|
|
59
|
-
const n2 = vo2[key];
|
|
60
|
-
|
|
61
|
-
if (n1 > n2) {
|
|
62
|
-
return 1;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
if (n1 < n2) {
|
|
66
|
-
return -1;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
return 0;
|
|
71
|
-
}
|