foreslash 0.3.1 → 0.3.3
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/CHANGELOG.md +30 -0
- package/lib/index.cmn.cjs +504 -4
- package/lib/index.d.ts +442 -20
- package/lib/index.mjs +487 -5
- package/lib/index.umd.js +504 -4
- package/package.json +7 -6
package/lib/index.d.ts
CHANGED
|
@@ -1,5 +1,111 @@
|
|
|
1
1
|
import { A, F } from 'ts-toolbelt';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* 将多个数组或类数组对象以笛卡尔积的形式拼接为一个新的二维数组
|
|
5
|
+
* @returns 笛卡尔积
|
|
6
|
+
* @example
|
|
7
|
+
* ```js
|
|
8
|
+
* cartesianProduct([1, 2, 3, 4, 5]) // [[1], [2], [3], [4], [5]]
|
|
9
|
+
* cartesianProduct([1, 2], '12') // [[1, '1'], [1, '2'], [2, '1'], [2, '2']]
|
|
10
|
+
* cartesianProduct([1, 2], '12', [3, 4])
|
|
11
|
+
* // [[1, '1', 3], [1, '1', 4], [1, '2', 3], [1, '2', 4]
|
|
12
|
+
* // [2, '1', 3], [2, '1', 4], [2, '2', 3], [2, '2', 4]]
|
|
13
|
+
* cartesianProduct([1, 2, 3, 4, 5], '12345', []) // [] 空集与其他集合的笛卡尔积是空集
|
|
14
|
+
* ```
|
|
15
|
+
* @version 0.3.2
|
|
16
|
+
*/
|
|
17
|
+
declare function cartesianProduct<T>(arr1: ArrayLike<T>): [T][];
|
|
18
|
+
declare function cartesianProduct<T1, T2>(arr1: ArrayLike<T1>, arr2: ArrayLike<T2>): [T1, T2][];
|
|
19
|
+
declare function cartesianProduct<T1, T2, T3>(arr1: ArrayLike<T1>, arr2: ArrayLike<T2>, arr3: ArrayLike<T3>): [T1, T2, T3][];
|
|
20
|
+
declare function cartesianProduct<T1, T2, T3, T4>(arr1: ArrayLike<T1>, arr2: ArrayLike<T2>, arr3: ArrayLike<T3>, arr4: ArrayLike<T4>): [T1, T2, T3, T4][];
|
|
21
|
+
declare function cartesianProduct<T1, T2, T3, T4, T5>(arr1: ArrayLike<T1>, arr2: ArrayLike<T2>, arr3: ArrayLike<T3>, arr4: ArrayLike<T4>, arr5: ArrayLike<T5>): [T1, T2, T3, T4, T5][];
|
|
22
|
+
declare function cartesianProduct<T1, T2, T3, T4, T5, T6>(arr1: ArrayLike<T1>, arr2: ArrayLike<T2>, arr3: ArrayLike<T3>, arr4: ArrayLike<T4>, arr5: ArrayLike<T5>, arr6: ArrayLike<T6>): [T1, T2, T3, T4, T5, T6][];
|
|
23
|
+
declare function cartesianProduct<T1, T2, T3, T4, T5, T6, T7>(arr1: ArrayLike<T1>, arr2: ArrayLike<T2>, arr3: ArrayLike<T3>, arr4: ArrayLike<T4>, arr5: ArrayLike<T5>, arr6: ArrayLike<T6>, arr7: ArrayLike<T7>): [T1, T2, T3, T4, T5, T6, T7][];
|
|
24
|
+
declare function cartesianProduct<T1, T2, T3, T4, T5, T6, T7, T8>(arr1: ArrayLike<T1>, arr2: ArrayLike<T2>, arr3: ArrayLike<T3>, arr4: ArrayLike<T4>, arr5: ArrayLike<T5>, arr6: ArrayLike<T6>, arr7: ArrayLike<T7>, arr8: ArrayLike<T8>): [T1, T2, T3, T4, T5, T6, T7, T8][];
|
|
25
|
+
declare function cartesianProduct(...arrList: ArrayLike<any>[]): any[][];
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 将值转换为一个数组
|
|
29
|
+
* @param value 需要处理的值
|
|
30
|
+
* @returns 一个数组, 如果输入值是数组, 则输出的数组是其**浅拷贝**; 如果输入不是数组,则将输入包进一个新数组里
|
|
31
|
+
* @example
|
|
32
|
+
* ```js
|
|
33
|
+
* // 如果输入不是数组,则将输入包进一个新数组里
|
|
34
|
+
* castArray(1) // [1]
|
|
35
|
+
* castArray('1') // ['1']
|
|
36
|
+
* castArray(null) // [null]
|
|
37
|
+
*
|
|
38
|
+
* // 如果输入值是数组, 则输出的数组是其浅拷贝
|
|
39
|
+
* castArray([1, 2, 3, 4, 5]) // [1, 2, 3, 4, 5]
|
|
40
|
+
* ```
|
|
41
|
+
* @version 0.3.3
|
|
42
|
+
*/
|
|
43
|
+
declare function castArray<T>(value: T): CastArray<T>;
|
|
44
|
+
/**
|
|
45
|
+
* 将任意输入处理为数组:
|
|
46
|
+
* 1. `never` 类型判断, 确保 `castArray<never>` 返回 `never[]` 而不是 `unknown[]`
|
|
47
|
+
* 2. `unknown` 类型判断, `unknown` 是顶级类型, 需要特殊处理
|
|
48
|
+
* 3. `readonly any[]` 类型判断, 输出的类型需要去除 `readonly`
|
|
49
|
+
* 4. 非数组类型判断, 处理联合类型
|
|
50
|
+
*
|
|
51
|
+
* 数组类型判据:
|
|
52
|
+
* - 如果 `Exclude<T, readonly any[]> extends never` 意味着 `T` 的类型与 `any[]` 兼容, 此时应该走逻辑 3
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* type T = CastArray<string> // string[]
|
|
57
|
+
* type T = CastArray<string[]> // string[]
|
|
58
|
+
* type T = CastArray<readonly string[]> // string[]
|
|
59
|
+
* // 特殊类型
|
|
60
|
+
* type T = CastArray<any> // any[]
|
|
61
|
+
* type T = CastArray<unknown> // unknown[]
|
|
62
|
+
* type T = CastArray<never> // never[]
|
|
63
|
+
* // 联合类型
|
|
64
|
+
* type T = CastArray<string | number> // (string | number)[]
|
|
65
|
+
* type T = CastArray<string[] | number> // string[] | number[]
|
|
66
|
+
* type T = CastArray<string | number | never> // (string | number)[]
|
|
67
|
+
* type T = CastArray<string | number[] | never[]> // never[] | number[] | string[]
|
|
68
|
+
* type T = CastArray<string | number | never[]> // never[] | (string | number)[]
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
type CastArray<T> = [
|
|
72
|
+
T
|
|
73
|
+
] extends [never] ? never[] : [
|
|
74
|
+
unknown
|
|
75
|
+
] extends [T] ? unknown[] : (T extends readonly (infer U)[] ? U[] : never) | (Exclude<T, readonly any[]> extends never ? never : Exclude<T, readonly any[]>[]);
|
|
76
|
+
|
|
77
|
+
type Not<T> = T extends false | 0 | '' | null | undefined ? true : false;
|
|
78
|
+
|
|
79
|
+
type Stringify<T extends string | number | boolean | bigint | null | undefined> = `${T}`;
|
|
80
|
+
|
|
81
|
+
/** 检查输入的数字是否为 `0` 或 `0n` */
|
|
82
|
+
type IsZero<T> = T extends 0 | 0n ? true : false;
|
|
83
|
+
/** 检查输入的数字是否为负数 */
|
|
84
|
+
type IsNegative<T> = T extends number | bigint ? (Stringify<T> extends `-${infer R}` ? true : false) : false;
|
|
85
|
+
/** 检查输入的数字是否为正数 */
|
|
86
|
+
type IsPositive<T> = T extends number | bigint ? (IsZero<T> extends true ? false : Not<IsNegative<T>>) : false;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* 将一个数组或类数组对象分割成多个指定大小的小块\
|
|
90
|
+
* 类似 Lodash 的 `chunk` 或是 Radash 的 `cluster`\
|
|
91
|
+
* 可以填入 `size` 以自定义每个小块的大小
|
|
92
|
+
* @param array 需要分块的数组
|
|
93
|
+
* @param size 每块大小, 不能小于 1, 默认为 `2`
|
|
94
|
+
* @returns 分块后的数组
|
|
95
|
+
* @example
|
|
96
|
+
* ```js
|
|
97
|
+
* chunk([1, 2, 3, 4, 5], 2) // [[1, 2], [3, 4], [5]]
|
|
98
|
+
* chunk('12345', 3) // [['1', '2', '3'], ['4', '5']]
|
|
99
|
+
* chunk({0: 1, 1: 2, 2: 3, length: 3}, 2) // [[1, 2], [3]]
|
|
100
|
+
* // 如果传入了不正常的 size 则返回空数组(类型为 `never[]`)
|
|
101
|
+
* chunk([1, 2, 3, 4, 5], 0) // []
|
|
102
|
+
* chunk([1, 2, 3, 4, 5], NaN) // []
|
|
103
|
+
* ```
|
|
104
|
+
* @version 0.3.2
|
|
105
|
+
*/
|
|
106
|
+
declare function chunk<T, Size extends number = 2>(array: ArrayLike<T>, size?: Size): Chunked<T, Size>;
|
|
107
|
+
type Chunked<T, Size extends number> = Size extends 1 ? [T][] : Size extends 2 ? [T, T][] : Size extends 3 ? [T, T, T][] : Size extends 4 ? [T, T, T, T][] : Size extends 5 ? [T, T, T, T, T][] : Size extends 6 ? [T, T, T, T, T, T][] : Size extends 7 ? [T, T, T, T, T, T, T][] : Size extends 8 ? [T, T, T, T, T, T, T, T][] : Size extends 9 ? [T, T, T, T, T, T, T, T, T][] : IsNegative<Size> extends true ? never[] : Size extends 0 ? never[] : T[][];
|
|
108
|
+
|
|
3
109
|
type RangeOptions<T> = {
|
|
4
110
|
step?: number;
|
|
5
111
|
value?: T;
|
|
@@ -197,24 +303,6 @@ declare function retry<T>(asyncFunction: RetryFunction<T>, option?: RetryOption)
|
|
|
197
303
|
declare function sleep(time?: number): Promise<unknown>;
|
|
198
304
|
|
|
199
305
|
type TryitResult<Res, Err> = [undefined, Res] | [Err, undefined];
|
|
200
|
-
/**
|
|
201
|
-
* 将一个函数处理为 `error-first` 的函数
|
|
202
|
-
* @param fn 需要处理的函数, 可以是异步函数
|
|
203
|
-
* @returns 处理过的函数, 调用后返回一个 `error-first` 的元组 `[error, result]`;\
|
|
204
|
-
* 如果原函数是异步函数, 则返回值会是 `Promise<[error, result]>`;\
|
|
205
|
-
* 如果运行正常则 `result` 是原函数的返回值, `error` 为 `undefined`;\
|
|
206
|
-
* 如果出现异常则 `result` 为 `undefined`, `error` 是原函数抛出的错误
|
|
207
|
-
* @example
|
|
208
|
-
* ```js
|
|
209
|
-
* // Async Function
|
|
210
|
-
* const normalAsyncFn = () => { return Promise.resolve(1) }
|
|
211
|
-
* const errorAsyncFn = () => { return Promise.reject('1') }
|
|
212
|
-
* tryit(normalAsyncFn)() // Promise<[undefined, 1]>
|
|
213
|
-
* tryit(errorAsyncFn)() // Promise<[Error('1'), undefined]>
|
|
214
|
-
* ```
|
|
215
|
-
* @version 0.2.0
|
|
216
|
-
*/
|
|
217
|
-
declare function tryit<Args extends any[], Res, Err extends Error>(fn: (...args: Args) => Promise<Res>): (...args: Args) => Promise<TryitResult<Res, Err>>;
|
|
218
306
|
/**
|
|
219
307
|
* 将一个函数处理为 `error-first` 的函数
|
|
220
308
|
* @param fn 需要处理的函数, 可以是异步函数
|
|
@@ -229,9 +317,15 @@ declare function tryit<Args extends any[], Res, Err extends Error>(fn: (...args:
|
|
|
229
317
|
* const errorFn = () => { throw new Error('1') }
|
|
230
318
|
* tryit(normalFn)() // [undefined, 1]
|
|
231
319
|
* tryit(errorFn)() // [Error('1'), undefined]
|
|
320
|
+
* // Async Function
|
|
321
|
+
* const normalAsyncFn = () => { return Promise.resolve(1) }
|
|
322
|
+
* const errorAsyncFn = () => { return Promise.reject(new Error('1')) }
|
|
323
|
+
* tryit(normalAsyncFn)() // Promise<[undefined, 1]>
|
|
324
|
+
* tryit(errorAsyncFn)() // Promise<[Error('1'), undefined]>
|
|
232
325
|
* ```
|
|
233
326
|
* @version 0.2.0
|
|
234
327
|
*/
|
|
328
|
+
declare function tryit<Args extends any[], Res, Err extends Error>(fn: (...args: Args) => Promise<Res>): (...args: Args) => Promise<TryitResult<Res, Err>>;
|
|
235
329
|
declare function tryit<Args extends any[], Res, Err extends Error>(fn: (...args: Args) => Res): (...args: Args) => TryitResult<Res, Err>;
|
|
236
330
|
|
|
237
331
|
interface PromiseLikeConstructor {
|
|
@@ -539,6 +633,21 @@ declare function isNumber(value: unknown): value is number;
|
|
|
539
633
|
*/
|
|
540
634
|
declare function isObject(value: unknown): value is object;
|
|
541
635
|
|
|
636
|
+
/**
|
|
637
|
+
* 类型守卫,判断给定的值是否为普通对象(不是 `Date`、数组等特殊对象)
|
|
638
|
+
* - 注意,与 lodash 的同名方法不同,不会将函数视为对象
|
|
639
|
+
* @param value 要判断的值
|
|
640
|
+
* @example
|
|
641
|
+
* ```js
|
|
642
|
+
* isObject({}) // true
|
|
643
|
+
* isObject(Object.create(null)) // true
|
|
644
|
+
* isObject([]) // false
|
|
645
|
+
* isObject(Object(123)) // false
|
|
646
|
+
* isObject(null) // false
|
|
647
|
+
* ```
|
|
648
|
+
*/
|
|
649
|
+
declare function isPlainObject(value: unknown): value is object;
|
|
650
|
+
|
|
542
651
|
/**
|
|
543
652
|
* 类型守卫,判断给定的值是否为原始类型(如字符串数字等 JS 内置的类型)
|
|
544
653
|
* - 包括 `undefined` 和 `null`
|
|
@@ -876,6 +985,289 @@ declare function clamp(num: number, min: number, max: number, options?: {
|
|
|
876
985
|
defaultMax?: number;
|
|
877
986
|
}): number;
|
|
878
987
|
|
|
988
|
+
/**
|
|
989
|
+
* 将一个数字转换为正常的十进制计数法
|
|
990
|
+
* @param num 需要转换的数字
|
|
991
|
+
* @returns 返回一个十进制计数法表示的数字
|
|
992
|
+
* @example
|
|
993
|
+
* ```js
|
|
994
|
+
* decimalNotation(1e12) // 1000000000000
|
|
995
|
+
* decimalNotation(-2.33e8) // -233000000
|
|
996
|
+
* decimalNotation(1.234e-6) // 0.000001234
|
|
997
|
+
* decimalNotation(-4.321e-8) // -0.00000004321
|
|
998
|
+
* ```
|
|
999
|
+
* @version 0.3.2
|
|
1000
|
+
*/
|
|
1001
|
+
declare function decimalNotation(num: string | number): string;
|
|
1002
|
+
|
|
1003
|
+
/**
|
|
1004
|
+
* 将一个数字转换为指定的格式(如千分位逗号分隔)\
|
|
1005
|
+
* 如果想要将数字转换为科学计数法, 请使用 `scientificNotation`
|
|
1006
|
+
* @param num 需要格式化的数字
|
|
1007
|
+
* @param options 格式化配置\
|
|
1008
|
+
* `separator` 分割符, 默认为 `','`\
|
|
1009
|
+
* `separate` 按位分割, 默认为 `3`\
|
|
1010
|
+
* `decimal` 小数点, 默认为 `'.'`\
|
|
1011
|
+
* `precision` 小数精度, 默认为 `2`\
|
|
1012
|
+
* `round` 数值修约规则
|
|
1013
|
+
* - `'round'` 四舍五入
|
|
1014
|
+
* - `'banker'` 四舍六入五成双
|
|
1015
|
+
* - `'floor'` 向下取整
|
|
1016
|
+
* - `'ceil'` 向上取整
|
|
1017
|
+
* @returns 返回一个指定格式的十进制数字
|
|
1018
|
+
* @version 0.3.2
|
|
1019
|
+
*/
|
|
1020
|
+
declare function format(num: number | string, options?: {
|
|
1021
|
+
separator?: string;
|
|
1022
|
+
separate?: number;
|
|
1023
|
+
decimal?: string;
|
|
1024
|
+
precision?: number;
|
|
1025
|
+
round?: 'round' | 'banker' | 'floor' | 'ceil';
|
|
1026
|
+
}): string;
|
|
1027
|
+
|
|
1028
|
+
/**
|
|
1029
|
+
* 检查一个数字是否为奇数
|
|
1030
|
+
* @param num 需要检查的数字
|
|
1031
|
+
* @returns 如果是奇数则返回 true 否则返回 false
|
|
1032
|
+
* @version 0.3.2
|
|
1033
|
+
*/
|
|
1034
|
+
declare function isOdd(num: number): boolean;
|
|
1035
|
+
/**
|
|
1036
|
+
* 检查一个数字是否为偶数
|
|
1037
|
+
* @param num 需要检查的数字
|
|
1038
|
+
* @returns 如果是偶数则返回 true 否则返回 false
|
|
1039
|
+
* @version 0.3.2
|
|
1040
|
+
*/
|
|
1041
|
+
declare function isEven(num: number): boolean;
|
|
1042
|
+
|
|
1043
|
+
/**
|
|
1044
|
+
* 用线性插值法求取一个中间值
|
|
1045
|
+
* @param val1 第一个值, 当 权重=0 时返回的将是这个值, 可以是数组或二维数组
|
|
1046
|
+
* @param val2 第二个值, 当 权重=0 时返回的将是这个值, 可以是数组或二维数组
|
|
1047
|
+
* @param t 权重
|
|
1048
|
+
* @example
|
|
1049
|
+
* ```js
|
|
1050
|
+
* // 传入数值
|
|
1051
|
+
* lerp(1, 2, 0) // 1
|
|
1052
|
+
* lerp(1, 2, 0.5) // 1.5
|
|
1053
|
+
* lerp(1, 2, 1) // 2
|
|
1054
|
+
* lerp(1, 2, 3) // 4
|
|
1055
|
+
*
|
|
1056
|
+
* // 传入数组(数组长度必须一致否则会报错)
|
|
1057
|
+
* lerp([1, 2], [2, 4], 0.5) // [1.5, 3]
|
|
1058
|
+
*
|
|
1059
|
+
* // 传入二维数组(数组长度必须一致否则会报错)
|
|
1060
|
+
* lerp(
|
|
1061
|
+
* [
|
|
1062
|
+
* [1, 2],
|
|
1063
|
+
* [3, 4],
|
|
1064
|
+
* ],
|
|
1065
|
+
* [
|
|
1066
|
+
* [11, 12],
|
|
1067
|
+
* [13, 14],
|
|
1068
|
+
* ],
|
|
1069
|
+
* 0.5
|
|
1070
|
+
* )
|
|
1071
|
+
* // [
|
|
1072
|
+
* // [6, 7],
|
|
1073
|
+
* // [8, 9],
|
|
1074
|
+
* // ]
|
|
1075
|
+
* ```
|
|
1076
|
+
* @version 0.3.3
|
|
1077
|
+
*/
|
|
1078
|
+
declare function lerp(val1: number, val2: number, t: number): number;
|
|
1079
|
+
declare function lerp(val1: [number], val2: [number], t: number): [number];
|
|
1080
|
+
declare function lerp(val1: [number, number], val2: [number, number], t: number): [number, number];
|
|
1081
|
+
declare function lerp(val1: [number, number, number], val2: [number, number, number], t: number): [number, number, number];
|
|
1082
|
+
declare function lerp(val1: [number, number, number, number], val2: [number, number, number, number], t: number): [number, number, number, number];
|
|
1083
|
+
declare function lerp(val1: [number, number, number, number, number], val2: [number, number, number, number, number], t: number): [number, number, number, number, number];
|
|
1084
|
+
declare function lerp(val1: [number, number, number, number, number, number], val2: [number, number, number, number, number, number], t: number): [number, number, number, number, number, number];
|
|
1085
|
+
declare function lerp(val1: [number, number, number, number, number, number, number], val2: [number, number, number, number, number, number, number], t: number): [number, number, number, number, number, number, number];
|
|
1086
|
+
declare function lerp(val1: [number, number, number, number, number, number, number, number], val2: [number, number, number, number, number, number, number, number], t: number): [number, number, number, number, number, number, number, number];
|
|
1087
|
+
declare function lerp(val1: number[], val2: number[], t: number): number[];
|
|
1088
|
+
declare function lerp(val1: number[][], val2: number[][], t: number): number[][];
|
|
1089
|
+
|
|
1090
|
+
/**
|
|
1091
|
+
* 将一个数字转换为罗马数字, 罗马数字没有 `0`、小数和负数
|
|
1092
|
+
* @param num 需要转换的数字
|
|
1093
|
+
* @param options 格式化配置\
|
|
1094
|
+
* `type` 类型, 默认为 `unicode`, 以数字 `4090` 为例
|
|
1095
|
+
* - `'unicode'` 使用 Unicode 表示 `'I̅V̅XC'`
|
|
1096
|
+
* - `'js'` 适合用于 JavaScript 代码字符串 `'I\\u0305V\\u0305XC'`
|
|
1097
|
+
* - `'html'` 适合用于 HTML 展示的字符串 `'I̅V̅XC'`
|
|
1098
|
+
* - `'json'` 一个 JSON 字符串, 具体数值是下标^1000 `["XC", "IV"]`
|
|
1099
|
+
*
|
|
1100
|
+
* `thousand` 千分位类型, 默认为 `normal`
|
|
1101
|
+
* - `'normal'` 习惯用法, 超过 3999 的部分才使用上划线区分
|
|
1102
|
+
* - `'strict'` 严格区分千分位
|
|
1103
|
+
*
|
|
1104
|
+
* `zero` 用什么字符串表示 `0` (罗马数字里没有 `0`), 不填默认为 `'0'`\
|
|
1105
|
+
* `one` 用什么字符串表示 `1` (你可以设置一个彩蛋, 比如输出 `Yo`), 不填默认为 `'I'`, 这项**只会影响数字 1**
|
|
1106
|
+
* @returns 返回一个罗马数字表示的数字
|
|
1107
|
+
* @example
|
|
1108
|
+
* ```js
|
|
1109
|
+
* romanNumerals(2025) // MMXXV
|
|
1110
|
+
* romanNumerals(2025, { thousand: 'strict' }) // I̅I̅XXV
|
|
1111
|
+
* ```
|
|
1112
|
+
* @version 0.3.3
|
|
1113
|
+
*/
|
|
1114
|
+
declare function romanNumerals(num: string | number, options?: {
|
|
1115
|
+
type?: 'unicode' | 'js' | 'html' | 'json';
|
|
1116
|
+
thousand?: 'normal' | 'strict';
|
|
1117
|
+
zero?: string;
|
|
1118
|
+
one?: string;
|
|
1119
|
+
}): string;
|
|
1120
|
+
|
|
1121
|
+
/**
|
|
1122
|
+
* 数值修约, 可以使用 4 种修约方法, 默认使用四舍五入
|
|
1123
|
+
* - 四舍五入: 修约位置的后一位如果是 5 则进位, 否则不进位
|
|
1124
|
+
* - 四舍六入五成双: 修约位置的后一位如果是 5 且前一位是偶数则进位, 否则不进位
|
|
1125
|
+
* - 向下取整
|
|
1126
|
+
* - 向上取整
|
|
1127
|
+
* @param num 需要修约的数字
|
|
1128
|
+
* @param precision 精度, 小数点后几位
|
|
1129
|
+
* @param type 修约方法, 默认使用四舍五入
|
|
1130
|
+
* @example
|
|
1131
|
+
* ```js
|
|
1132
|
+
* // 四舍五入
|
|
1133
|
+
* round(1.95, 1) // '2.0'
|
|
1134
|
+
* round('1.95', 1) // '2.0'
|
|
1135
|
+
* // 四舍六入五成双
|
|
1136
|
+
* round('1.85', 1, 'banker') // '1.8'
|
|
1137
|
+
* round('1.95', 1, 'banker') // '2.0'
|
|
1138
|
+
* // 向下取整
|
|
1139
|
+
* round('1.95', 1, 'floor') // '1.9'
|
|
1140
|
+
* round(-1.95, 1, 'floor') // '-2.0'
|
|
1141
|
+
* // 向上取整
|
|
1142
|
+
* round('1.95', 1, 'ceil') // '2.0'
|
|
1143
|
+
* round(-1.95, 1, 'ceil') // '-1.9'
|
|
1144
|
+
* ```
|
|
1145
|
+
* @version 0.3.2
|
|
1146
|
+
*/
|
|
1147
|
+
declare function round(num: string | number, precision: number, type?: 'round' | 'banker' | 'floor' | 'ceil'): string;
|
|
1148
|
+
/**
|
|
1149
|
+
* 四舍五入, 由于 JS 的 toFixed 有浮点误差所以不能使用
|
|
1150
|
+
* @example
|
|
1151
|
+
* ```js
|
|
1152
|
+
* // 四舍五入
|
|
1153
|
+
* roundBase('1', '34', 1, false) // ['1', '3']
|
|
1154
|
+
* roundBase('1', '35', 1, false) // ['1', '4']
|
|
1155
|
+
* roundBase('1', '36', 1, false) // ['1', '4']
|
|
1156
|
+
* // 与符号无关
|
|
1157
|
+
* roundBase('1', '34', 1, true) // ['1', '3']
|
|
1158
|
+
* roundBase('1', '35', 1, true) // ['1', '4']
|
|
1159
|
+
* roundBase('1', '36', 1, true) // ['1', '4']
|
|
1160
|
+
* ```
|
|
1161
|
+
* @version 0.3.2
|
|
1162
|
+
*/
|
|
1163
|
+
declare function roundBase(integer: string, fractional: string, precision: number): [integer: string, fractional: string];
|
|
1164
|
+
/**
|
|
1165
|
+
* 四舍六入五成双
|
|
1166
|
+
* @example
|
|
1167
|
+
* ```js
|
|
1168
|
+
* // 四舍六入, 如果后一位是 5(000...) 则看前一位, 凑偶数
|
|
1169
|
+
* roundBank('1', '34', 1, false) // ['1', '3']
|
|
1170
|
+
* roundBank('1', '35', 1, false) // ['1', '4'] 五成双
|
|
1171
|
+
* roundBank('1', '36', 1, false) // ['1', '4']
|
|
1172
|
+
* roundBank('1', '44', 1, false) // ['1', '4']
|
|
1173
|
+
* roundBank('1', '45', 1, false) // ['1', '4'] 五成双
|
|
1174
|
+
* roundBank('1', '450001', 1, false) // ['1', '5'] 后一位比 5(000...) 多所以仍然进位
|
|
1175
|
+
* roundBank('1', '46', 1, false) // ['1', '5']
|
|
1176
|
+
* // 向整数进位也是如此
|
|
1177
|
+
* roundBank('1', '4', 0, false) // ['1', '']
|
|
1178
|
+
* roundBank('1', '5', 0, false) // ['2', ''] 五成双
|
|
1179
|
+
* roundBank('1', '6', 0, false) // ['2', '']
|
|
1180
|
+
* roundBank('2', '4', 0, false) // ['2', '']
|
|
1181
|
+
* roundBank('2', '5', 0, false) // ['2', ''] 五成双
|
|
1182
|
+
* roundBank('2', '6', 0, false) // ['3', '']
|
|
1183
|
+
* // 与符号无关
|
|
1184
|
+
* roundBank('1', '34', 1, true) // ['1', '3']
|
|
1185
|
+
* roundBank('1', '35', 1, true) // ['1', '4'] 五成双
|
|
1186
|
+
* roundBank('1', '36', 1, true) // ['1', '4']
|
|
1187
|
+
* roundBank('1', '44', 1, true) // ['1', '4']
|
|
1188
|
+
* roundBank('1', '45', 1, true) // ['1', '4'] 五成双
|
|
1189
|
+
* roundBank('1', '450001', 1, true) // ['1', '5']
|
|
1190
|
+
* roundBank('1', '46', 1, true) // ['1', '5']
|
|
1191
|
+
* ```
|
|
1192
|
+
* @version 0.3.2
|
|
1193
|
+
*/
|
|
1194
|
+
declare function roundBank(integer: string, fractional: string, precision: number): [integer: string, fractional: string];
|
|
1195
|
+
/** 向下取整
|
|
1196
|
+
* @example
|
|
1197
|
+
* ```js
|
|
1198
|
+
* // 忽略后面的数字向下取整
|
|
1199
|
+
* roundFloor('1', '34', 1, false) // ['1', '3']
|
|
1200
|
+
* roundFloor('1', '36', 1, false) // ['1', '3']
|
|
1201
|
+
* roundFloor('1', '39', 1, false) // ['1', '3']
|
|
1202
|
+
* // 与符号有关 向更小的数字进位
|
|
1203
|
+
* roundFloor('1', '34', 1, true) // ['1', '4']
|
|
1204
|
+
* roundFloor('1', '35', 1, true) // ['1', '4']
|
|
1205
|
+
* roundFloor('1', '36', 1, true) // ['1', '4']
|
|
1206
|
+
* ```
|
|
1207
|
+
* @version 0.3.2
|
|
1208
|
+
*/
|
|
1209
|
+
declare function roundFloor(integer: string, fractional: string, precision: number, isNegative: boolean): [integer: string, fractional: string];
|
|
1210
|
+
/** 向上取整
|
|
1211
|
+
* @example
|
|
1212
|
+
* ```js
|
|
1213
|
+
* // 忽略后面的数字向上取整
|
|
1214
|
+
* roundCeil('1', '34', 1, false) // ['1', '4']
|
|
1215
|
+
* roundCeil('1', '35', 1, false) // ['1', '4']
|
|
1216
|
+
* roundCeil('1', '36', 1, false) // ['1', '4']
|
|
1217
|
+
* // 与符号有关 向更大的数字进位
|
|
1218
|
+
* roundCeil('1', '34', 1, true) // ['1', '3']
|
|
1219
|
+
* roundCeil('1', '35', 1, true) // ['1', '3']
|
|
1220
|
+
* roundCeil('1', '36', 1, true) // ['1', '3']
|
|
1221
|
+
* ```
|
|
1222
|
+
* @version 0.3.2
|
|
1223
|
+
*/
|
|
1224
|
+
declare function roundCeil(integer: string, fractional: string, precision: number, isNegative: boolean): [integer: string, fractional: string];
|
|
1225
|
+
|
|
1226
|
+
/**
|
|
1227
|
+
* 将一个数字转换为科学计数法
|
|
1228
|
+
* @param num 需要转换的数字
|
|
1229
|
+
* @param options 格式化配置\
|
|
1230
|
+
* `type` 类型, 默认为 `unicode`
|
|
1231
|
+
* - `'unicode'` 指数部分使用 Unicode 表示 `1.23×10⁴⁵⁶`
|
|
1232
|
+
* - `'exp'` 用指数表示法, 用 `e` 替换 `×10` 部分 `1.23e+456`
|
|
1233
|
+
* - `'js'` 适合用于 JavaScript 的算式 `1.23*10**456`
|
|
1234
|
+
* - `'code'` 适合用于其他计算机语言的算式 `1.23*10^456`
|
|
1235
|
+
* - `'html'` 适合用于 HTML 展示的字符串 `1.23×10<sup>456</sup>`
|
|
1236
|
+
* - `'json'` 一个 JSON 字符串, 可以自由处理 `{"number":1.23,"exp":456}`
|
|
1237
|
+
*
|
|
1238
|
+
* `precision` 小数精度, 默认不作数值修约\
|
|
1239
|
+
* `round` 数值修约规则
|
|
1240
|
+
* - `'round'` 四舍五入
|
|
1241
|
+
* - `'banker'` 四舍六入五成双
|
|
1242
|
+
* - `'floor'` 向下取整
|
|
1243
|
+
* - `'ceil'` 向上取整
|
|
1244
|
+
* @returns 返回一个科学计数法表示的数字
|
|
1245
|
+
* @example
|
|
1246
|
+
* ```js
|
|
1247
|
+
* scientificNotation(1e12) // 1×10¹²
|
|
1248
|
+
* scientificNotation(-2.33e-8) // -2.33×10⁻⁸
|
|
1249
|
+
* // 可以指定输出类型
|
|
1250
|
+
* scientificNotation(1.234e-6, { type: 'exp' }) // 1.234e-6
|
|
1251
|
+
* scientificNotation(1.234e6, { type: 'exp' }) // 1.234e+6
|
|
1252
|
+
* scientificNotation(6.534e-6, { type: 'code' }) // 6.534*10^-6
|
|
1253
|
+
* scientificNotation(6.534e6, { type: 'code' }) // 6.534*10^6
|
|
1254
|
+
* scientificNotation(-4.321e-8, { type: 'html' }) // -4.321×10<sup>-8</sup>
|
|
1255
|
+
* scientificNotation(-4.321e8, { type: 'html' }) // -4.321×10<sup>8</sup>
|
|
1256
|
+
* scientificNotation(-9.87e-9, { type: 'json' }) // {"number":"-9.87","exp":-9}
|
|
1257
|
+
* scientificNotation(-9.87e9, { type: 'json' }) // {"number":"-9.87","exp":9}
|
|
1258
|
+
* // 可以指定小数点后的位数及数值修约规则
|
|
1259
|
+
* scientificNotation(1.235e6, { type: 'exp', precision: 2 }) // 1.24e+6
|
|
1260
|
+
* scientificNotation(6.545e-6, { type: 'code', precision: 2, round: 'banker' }) // 6.54*10^-6
|
|
1261
|
+
* scientificNotation(-9.87e9, { type: 'json', precision: 1, round: 'floor' }) // {"number":"-9.9","exp":9}
|
|
1262
|
+
* ```
|
|
1263
|
+
* @version 0.3.3
|
|
1264
|
+
*/
|
|
1265
|
+
declare function scientificNotation(num: string | number, options?: {
|
|
1266
|
+
type?: 'unicode' | 'exp' | 'js' | 'code' | 'html' | 'json';
|
|
1267
|
+
precision?: number;
|
|
1268
|
+
round?: 'round' | 'banker' | 'floor' | 'ceil';
|
|
1269
|
+
}): string;
|
|
1270
|
+
|
|
879
1271
|
/**
|
|
880
1272
|
* 根据文件名称判断是否匹配支持的文件类型,需要注意 `.C` 与 `.c` 的区别
|
|
881
1273
|
* - `text/x-c++src` 对应 `.C`
|
|
@@ -970,6 +1362,35 @@ declare function getAcceptableMIMEByExt(ext: string): string[];
|
|
|
970
1362
|
*/
|
|
971
1363
|
declare function randomChoice<T>(arr: ArrayLike<T>, weights?: ArrayLike<number>): T;
|
|
972
1364
|
|
|
1365
|
+
/**
|
|
1366
|
+
* 一个广泛使用的[伪随机数生成算法(PRD)](https://github.com/Moushudyx/pseudo-random-distribution)\
|
|
1367
|
+
* 返回一个方法, 输出为布尔型, 可以生成一个比正常随机序列分布更加均匀的伪随机数列\
|
|
1368
|
+
* 可以极大地避免罕见牌型出现, 缺点是初始概率很低(即第一次命中的概率远远小于输入的 `p`)\
|
|
1369
|
+
* 建议在投入使用之前, 先调用 `ceil(1 / p)` 次作为初始化\
|
|
1370
|
+
* 此外需要注意的是, 这个算法的最终的精度并不是完全等于输入的 `p` 值
|
|
1371
|
+
* @param p 整体概率值, 即概率的数学期望
|
|
1372
|
+
* @param options 相关配置
|
|
1373
|
+
* - `threshold` 精度, 数值越小, 精度越高, 序列越接近目标 `p` 值, 默认为 `5e-6`
|
|
1374
|
+
* @returns 一个方法, 按给定的概率值返回 `true` 和 `false`
|
|
1375
|
+
* @example
|
|
1376
|
+
* ```js
|
|
1377
|
+
* const p0_5 = pipe(randomDistribution(0.5), Number)
|
|
1378
|
+
* p0_5() // 0, 1, 0, 0, 1, 1 ...
|
|
1379
|
+
* ```
|
|
1380
|
+
* @version 0.3.3
|
|
1381
|
+
*/
|
|
1382
|
+
declare function randomDistribution(p: number, options?: {
|
|
1383
|
+
threshold: number;
|
|
1384
|
+
}): () => boolean;
|
|
1385
|
+
/**
|
|
1386
|
+
* 内部方法, 用于实现 `randomDistribution` 中的 PRD 算法部分, 根据目标概率, 给出一个初始概率
|
|
1387
|
+
* @param targetP 目标概率
|
|
1388
|
+
* @param threshold 精度, 数值越小, 精度越高
|
|
1389
|
+
* @returns PRD 算法需要的初始概率
|
|
1390
|
+
* @version 0.3.3
|
|
1391
|
+
*/
|
|
1392
|
+
declare function getInitP(targetP: number, threshold?: number): number;
|
|
1393
|
+
|
|
973
1394
|
/**
|
|
974
1395
|
* 生成指定范围内的随机整数
|
|
975
1396
|
* @param min 随机数的下界,包含于此范围内
|
|
@@ -1799,6 +2220,7 @@ declare function isEmpty(value: unknown): boolean;
|
|
|
1799
2220
|
* return fib(n - 1) + fib(n - 2)
|
|
1800
2221
|
* })
|
|
1801
2222
|
* ```
|
|
2223
|
+
* @version 0.2.1
|
|
1802
2224
|
*/
|
|
1803
2225
|
declare function memo<TArgs extends any[], TRes>(fn: (...args: TArgs) => TRes, options?: {
|
|
1804
2226
|
getKey?: (...args: TArgs) => string | number;
|
|
@@ -1986,5 +2408,5 @@ declare function throttle<T extends any[]>(fn: (...args: T) => any, delay: numbe
|
|
|
1986
2408
|
reset: () => void;
|
|
1987
2409
|
};
|
|
1988
2410
|
|
|
1989
|
-
export { $$Empty, _, acceptableFileName, acceptableFileType, camelCase, capitalize, caseCamel, caseConvert, caseKebab, casePascal, caseSnake, clamp, compose, _curryMore as curry, debounce, dedent, deepClone, deepMerge, defer, fastClone, getAcceptableExtByMIME, getAcceptableMIMEByExt, getGlobalThis, getTag, indent, isArray, isArrayBuffer, isArrayLike, isBigInt, isBigInt64Array, isBigUint64Array, isBlob, isBoolean, isBuffer, isDataView, isDate, isEmpty, isFile, isFloat32Array, isFloat64Array, isFormData, isFunction, isInt16Array, isInt32Array, isInt8Array, isInteger, isIterable, isMap, isMergeEmptyPlaceholder, isNil, isNull, isNumber, isObject, isPlaceholder, isPrimitive, isPromise, isPromiseLike, isRegExp, isSet, isString, isSymbol, isTypedArray, isUint16Array, isUint32Array, isUint8Array, isUint8ClampedArray, isUndefined, isWeakMap, isWeakSet, isWrapperBigInt, isWrapperBoolean, isWrapperNumber, isWrapperObject, isWrapperString, isWrapperSymbol, kebabCase, memo, noop, not, parallel, pascalCase, pass, passWith, pipe, randomBase32String, randomChoice, randomHexString, randomInt, randomIntFloor, randomString, range, remove, retry, shuffle, sleep, snakeCase, splitWords, throttle, titleCase, tryit, ulid, uncapitalize, uuidNil, uuidV4, withResolvers };
|
|
1990
|
-
export type { BaseMargeType, CloneOptions, CustomCloner, MergeOption, MergeStrategy, MergeStrategyFunction, MergeType, MergeTypeStrategy, RangeOptions, SourceMergeType, TargetMergeType, TypedArray };
|
|
2411
|
+
export { $$Empty, _, acceptableFileName, acceptableFileType, camelCase, capitalize, cartesianProduct, caseCamel, caseConvert, caseKebab, casePascal, caseSnake, castArray, chunk, clamp, compose, _curryMore as curry, debounce, decimalNotation, dedent, deepClone, deepMerge, defer, fastClone, format, getAcceptableExtByMIME, getAcceptableMIMEByExt, getGlobalThis, getInitP, getTag, indent, isArray, isArrayBuffer, isArrayLike, isBigInt, isBigInt64Array, isBigUint64Array, isBlob, isBoolean, isBuffer, isDataView, isDate, isEmpty, isEven, isFile, isFloat32Array, isFloat64Array, isFormData, isFunction, isInt16Array, isInt32Array, isInt8Array, isInteger, isIterable, isMap, isMergeEmptyPlaceholder, isNil, isNull, isNumber, isObject, isOdd, isPlaceholder, isPlainObject, isPrimitive, isPromise, isPromiseLike, isRegExp, isSet, isString, isSymbol, isTypedArray, isUint16Array, isUint32Array, isUint8Array, isUint8ClampedArray, isUndefined, isWeakMap, isWeakSet, isWrapperBigInt, isWrapperBoolean, isWrapperNumber, isWrapperObject, isWrapperString, isWrapperSymbol, kebabCase, lerp, memo, noop, not, parallel, pascalCase, pass, passWith, pipe, randomBase32String, randomChoice, randomDistribution, randomHexString, randomInt, randomIntFloor, randomString, range, remove, retry, romanNumerals, round, roundBank, roundBase, roundCeil, roundFloor, scientificNotation, shuffle, sleep, snakeCase, splitWords, throttle, titleCase, tryit, ulid, uncapitalize, uuidNil, uuidV4, withResolvers };
|
|
2412
|
+
export type { BaseMargeType, CastArray, Chunked, CloneOptions, CustomCloner, IsNegative, IsPositive, IsZero, MergeOption, MergeStrategy, MergeStrategyFunction, MergeType, MergeTypeStrategy, Not, RangeOptions, SourceMergeType, Stringify, TargetMergeType, TypedArray };
|