@anjianshi/utils 2.3.1 → 2.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/env-node/random.d.ts +2 -1
- package/env-node/random.js +1 -0
- package/lang/random.d.ts +2 -1
- package/lang/random.js +1 -0
- package/package.json +3 -3
- package/validators/number.d.ts +4 -2
- package/validators/number.js +5 -2
package/env-node/random.d.ts
CHANGED
|
@@ -8,5 +8,6 @@ export declare function getRandomInt(min: number, max: number): number;
|
|
|
8
8
|
export declare function getRandomString(length?: number, seed?: string): string;
|
|
9
9
|
/**
|
|
10
10
|
* 从给定的选择中随机选中一项
|
|
11
|
+
* 如果数组为空,会返回 undefined
|
|
11
12
|
*/
|
|
12
|
-
export declare function choiceRandom<T>(choices: T[]):
|
|
13
|
+
export declare function choiceRandom<T>(choices: T[]): T | undefined;
|
package/env-node/random.js
CHANGED
package/lang/random.d.ts
CHANGED
|
@@ -8,5 +8,6 @@ export declare function getRandomInt(min: number, max: number): number;
|
|
|
8
8
|
export declare function getRandomString(length?: number, seed?: string): string;
|
|
9
9
|
/**
|
|
10
10
|
* 从给定的选择中随机选中一项
|
|
11
|
+
* 如果数组为空,会返回 undefined
|
|
11
12
|
*/
|
|
12
|
-
export declare function choiceRandom<T>(choices: T[]):
|
|
13
|
+
export declare function choiceRandom<T>(choices: T[]): T | undefined;
|
package/lang/random.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@anjianshi/utils",
|
|
3
|
-
"version": "2.3.
|
|
3
|
+
"version": "2.3.3",
|
|
4
4
|
"description": "Common JavaScript Utils",
|
|
5
5
|
"homepage": "https://github.com/anjianshi/js-packages/utils",
|
|
6
6
|
"bugs": {
|
|
@@ -27,10 +27,10 @@
|
|
|
27
27
|
"typeorm": "^0.3.20",
|
|
28
28
|
"typescript": "^5.5.4",
|
|
29
29
|
"vconsole": "^3.15.1",
|
|
30
|
-
"@anjianshi/presets-eslint-node": "4.0.8",
|
|
31
30
|
"@anjianshi/presets-eslint-typescript": "5.0.5",
|
|
32
|
-
"@anjianshi/presets-prettier": "3.0.1",
|
|
33
31
|
"@anjianshi/presets-typescript": "3.2.2",
|
|
32
|
+
"@anjianshi/presets-prettier": "3.0.1",
|
|
33
|
+
"@anjianshi/presets-eslint-node": "4.0.8",
|
|
34
34
|
"@anjianshi/presets-eslint-react": "4.0.7"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
package/validators/number.d.ts
CHANGED
|
@@ -9,9 +9,11 @@ export interface NumberOptions extends CommonOptions<number> {
|
|
|
9
9
|
/**
|
|
10
10
|
* 指定可选值
|
|
11
11
|
* 若指定,前几个选项将不再生效 */
|
|
12
|
-
choices?: number[]
|
|
12
|
+
choices?: number[] | Record<number, string>;
|
|
13
13
|
}
|
|
14
14
|
export type NumberValueWithChoices<Options extends NumberOptions> = Options extends {
|
|
15
15
|
choices: (infer T)[];
|
|
16
|
-
} ? T :
|
|
16
|
+
} ? T : Options extends {
|
|
17
|
+
choices: Record<number, string>;
|
|
18
|
+
} ? Options['choices'][keyof Options['choices']] : number;
|
|
17
19
|
export declare function getNumberValidator<const Options extends NumberOptions>(options?: Options): import("./base.js").Validator<NumberValueWithChoices<Options>, Options>;
|
package/validators/number.js
CHANGED
|
@@ -7,8 +7,11 @@ export function getNumberValidator(options = {}) {
|
|
|
7
7
|
if (typeof value !== 'number' || !isFinite(value))
|
|
8
8
|
return failed(`${field} must be a valid number`);
|
|
9
9
|
if ('choices' in options && options.choices) {
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
const choices = Array.isArray(options.choices)
|
|
11
|
+
? options.choices
|
|
12
|
+
: Object.values(options.choices).map(v => parseInt(v, 10));
|
|
13
|
+
if (!choices.includes(value))
|
|
14
|
+
return failed(`${field} can only be one of ${choices.join(', ')}.`);
|
|
12
15
|
}
|
|
13
16
|
else {
|
|
14
17
|
if (!truthy(options.float) && value % 1 !== 0)
|