@anjianshi/utils 2.3.3 → 2.3.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anjianshi/utils",
3
- "version": "2.3.3",
3
+ "version": "2.3.4",
4
4
  "description": "Common JavaScript Utils",
5
5
  "homepage": "https://github.com/anjianshi/js-packages/utils",
6
6
  "bugs": {
@@ -28,10 +28,10 @@
28
28
  "typescript": "^5.5.4",
29
29
  "vconsole": "^3.15.1",
30
30
  "@anjianshi/presets-eslint-typescript": "5.0.5",
31
- "@anjianshi/presets-typescript": "3.2.2",
32
- "@anjianshi/presets-prettier": "3.0.1",
33
31
  "@anjianshi/presets-eslint-node": "4.0.8",
34
- "@anjianshi/presets-eslint-react": "4.0.7"
32
+ "@anjianshi/presets-prettier": "3.0.1",
33
+ "@anjianshi/presets-eslint-react": "4.0.7",
34
+ "@anjianshi/presets-typescript": "3.2.2"
35
35
  },
36
36
  "peerDependencies": {
37
37
  "@emotion/react": "^11.13.3",
@@ -9,6 +9,8 @@ export interface ArrayOptions extends CommonOptions {
9
9
  max?: number;
10
10
  /** 是否对数组元素进行去重 @defaults false */
11
11
  unique?: boolean;
12
+ /** 如果传入的不是数组,是否要将其视为数组内元素,包裹成数组 */
13
+ toArray?: boolean;
12
14
  }
13
15
  type ArrayValues<Options extends ArrayOptions> = Validated<Options extends {
14
16
  item: Validator<infer T, CommonOptions>;
@@ -2,8 +2,11 @@ import { success, failed } from '../lang/index.js';
2
2
  import { getValidatorGenerator, } from './base.js';
3
3
  export function getArrayValidator(options) {
4
4
  return getValidatorGenerator(function validate(field, value, options) {
5
- if (!Array.isArray(value))
5
+ if (!Array.isArray(value)) {
6
+ if (options.toArray)
7
+ value = [value];
6
8
  return failed(`${field} should be an array`);
9
+ }
7
10
  let formatted = [];
8
11
  if (typeof options.min === 'number' && value.length < options.min)
9
12
  return failed(`array ${field}'s length should >= ${options.min}`);