extra-utils 5.4.0 → 5.5.1

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/README.md CHANGED
@@ -191,8 +191,24 @@ removeTrailingBlankLines(
191
191
 
192
192
  ### Enum
193
193
  ```ts
194
- function inEnum<T>(val: unknown, _enum: object): val is T
195
- function notInEnum<T, U>(val: T, _enum: object): val is Exclude<T, U>
194
+ function inEnum<T extends Record<string, string | number>>(
195
+ val: string | number
196
+ , _enum: T
197
+ ): val is T[keyof T]
198
+ function notInEnum(
199
+ val: string | number
200
+ , _enum: Record<string, string | number>
201
+ ): boolean
202
+
203
+ function enumKeys<T extends Record<string, string | number>>(
204
+ _enum: T
205
+ ): Array<keyof T>
206
+ function enumValues<T extends Record<string, string | number>>(
207
+ _enum: T
208
+ ): Array<T[keyof T]>
209
+ function enumEntries<T extends Record<string, string | number>>(
210
+ _enum: T
211
+ ): Array<{ [Key in keyof T]: [Key, T[Key]] }[keyof T]>
196
212
  ```
197
213
 
198
214
  ### Date
@@ -0,0 +1,3 @@
1
+ export declare function enumEntries<T extends Record<string, string | number>>(_enum: T): Array<{
2
+ [Key in keyof T]: [Key, T[Key]];
3
+ }[keyof T]>;
@@ -0,0 +1,5 @@
1
+ import { enumKeys } from './enum-keys.js';
2
+ export function enumEntries(_enum) {
3
+ return enumKeys(_enum).map(key => [key, _enum[key]]);
4
+ }
5
+ //# sourceMappingURL=enum-entries.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"enum-entries.js","sourceRoot":"","sources":["../../src/enum/enum-entries.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAA;AAEzC,MAAM,UAAU,WAAW,CACzB,KAAQ;IAER,OAAO,QAAQ,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAA;AACtD,CAAC"}
@@ -0,0 +1 @@
1
+ export declare function enumKeys<T extends Record<string, string | number>>(_enum: T): Array<keyof T>;
@@ -0,0 +1,5 @@
1
+ export function enumKeys(_enum) {
2
+ return Object.keys(_enum)
3
+ .filter(key => Number.isNaN(Number(key)));
4
+ }
5
+ //# sourceMappingURL=enum-keys.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"enum-keys.js","sourceRoot":"","sources":["../../src/enum/enum-keys.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,QAAQ,CACtB,KAAQ;IAER,OAAO,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC;SACtB,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAA;AAC7C,CAAC"}
@@ -0,0 +1 @@
1
+ export declare function enumValues<T extends Record<string, string | number>>(_enum: T): Array<T[keyof T]>;
@@ -0,0 +1,6 @@
1
+ import { enumKeys } from './enum-keys.js';
2
+ export function enumValues(_enum) {
3
+ const keys = enumKeys(_enum);
4
+ return keys.map(key => _enum[key]);
5
+ }
6
+ //# sourceMappingURL=enum-values.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"enum-values.js","sourceRoot":"","sources":["../../src/enum/enum-values.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAA;AAEzC,MAAM,UAAU,UAAU,CACxB,KAAQ;IAER,MAAM,IAAI,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAA;IAC5B,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAA;AACpC,CAAC"}
@@ -0,0 +1,2 @@
1
+ export declare function inEnum<T extends Record<string, string | number>>(val: string | number, _enum: T): val is T[keyof T];
2
+ export declare function notInEnum(val: string | number, _enum: Record<string, string | number>): boolean;
@@ -0,0 +1,8 @@
1
+ import { enumValues } from './enum-values.js';
2
+ export function inEnum(val, _enum) {
3
+ return enumValues(_enum).includes(val);
4
+ }
5
+ export function notInEnum(val, _enum) {
6
+ return !inEnum(val, _enum);
7
+ }
8
+ //# sourceMappingURL=in-enum.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"in-enum.js","sourceRoot":"","sources":["../../src/enum/in-enum.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAE7C,MAAM,UAAU,MAAM,CACpB,GAAoB,EACpB,KAAQ;IAER,OAAO,UAAU,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,GAAiB,CAAC,CAAA;AACtD,CAAC;AAGD,MAAM,UAAU,SAAS,CACvB,GAAoB,EACpB,KAAsC;IAEtC,OAAO,CAAC,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;AAC5B,CAAC"}
@@ -0,0 +1,4 @@
1
+ export * from './in-enum.js';
2
+ export * from './enum-keys.js';
3
+ export * from './enum-values.js';
4
+ export * from './enum-entries.js';
@@ -0,0 +1,5 @@
1
+ export * from './in-enum.js';
2
+ export * from './enum-keys.js';
3
+ export * from './enum-values.js';
4
+ export * from './enum-entries.js';
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/enum/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA;AAC5B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,kBAAkB,CAAA;AAChC,cAAc,mBAAmB,CAAA"}
package/lib/index.d.ts CHANGED
@@ -6,7 +6,7 @@ export * from './object/index.js';
6
6
  export * from './json/index.js';
7
7
  export * from './string/index.js';
8
8
  export * from './nullish/index.js';
9
- export * from './in-enum.js';
9
+ export * from './enum/index.js';
10
10
  export * from './is-regexp.js';
11
11
  export * from './is-symbol.js';
12
12
  export * from './is-date.js';
package/lib/index.js CHANGED
@@ -6,7 +6,7 @@ export * from './object/index.js';
6
6
  export * from './json/index.js';
7
7
  export * from './string/index.js';
8
8
  export * from './nullish/index.js';
9
- export * from './in-enum.js';
9
+ export * from './enum/index.js';
10
10
  export * from './is-regexp.js';
11
11
  export * from './is-symbol.js';
12
12
  export * from './is-date.js';
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAA;AACrC,cAAc,kBAAkB,CAAA;AAChC,cAAc,oBAAoB,CAAA;AAClC,cAAc,mBAAmB,CAAA;AACjC,cAAc,mBAAmB,CAAA;AACjC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,mBAAmB,CAAA;AACjC,cAAc,oBAAoB,CAAA;AAClC,cAAc,cAAc,CAAA;AAC5B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,cAAc,CAAA;AAC5B,cAAc,kBAAkB,CAAA;AAChC,cAAc,WAAW,CAAA;AACzB,cAAc,iBAAiB,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAA;AACrC,cAAc,kBAAkB,CAAA;AAChC,cAAc,oBAAoB,CAAA;AAClC,cAAc,mBAAmB,CAAA;AACjC,cAAc,mBAAmB,CAAA;AACjC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,mBAAmB,CAAA;AACjC,cAAc,oBAAoB,CAAA;AAClC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,cAAc,CAAA;AAC5B,cAAc,kBAAkB,CAAA;AAChC,cAAc,WAAW,CAAA;AACzB,cAAc,iBAAiB,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "extra-utils",
3
- "version": "5.4.0",
3
+ "version": "5.5.1",
4
4
  "description": "Utilities for JavaScript and Typescript",
5
5
  "files": [
6
6
  "src",
@@ -34,32 +34,32 @@
34
34
  }
35
35
  },
36
36
  "devDependencies": {
37
- "@blackglory/jest-resolver": "^0.3.1",
38
- "@blackglory/pass": "^1.1.1",
39
- "@commitlint/cli": "^17.6.7",
40
- "@commitlint/config-conventional": "^17.6.7",
41
- "@types/jest": "^29.5.3",
37
+ "@blackglory/jest-resolver": "^0.3.0",
38
+ "@blackglory/pass": "^1.1.0",
39
+ "@commitlint/cli": "^17.6.1",
40
+ "@commitlint/config-conventional": "^17.6.1",
41
+ "@types/jest": "^29.5.0",
42
42
  "@types/jsdom": "^21.1.1",
43
- "@types/lodash-es": "^4.17.8",
44
- "@typescript-eslint/eslint-plugin": "^6.1.0",
45
- "@typescript-eslint/parser": "^6.1.0",
43
+ "@types/lodash-es": "^4.17.7",
44
+ "@typescript-eslint/eslint-plugin": "^5.58.0",
45
+ "@typescript-eslint/parser": "^5.58.0",
46
46
  "cross-env": "^7.0.3",
47
- "eslint": "^8.45.0",
47
+ "eslint": "^8.38.0",
48
48
  "husky": "^4.3.8",
49
- "jest": "^29.6.1",
50
- "jest-resolve": "^29.6.1",
49
+ "jest": "^29.5.0",
50
+ "jest-resolve": "^29.5.0",
51
51
  "npm-run-all": "^4.1.5",
52
- "rimraf": "^5.0.1",
52
+ "rimraf": "^3.0.2",
53
53
  "standard-version": "^9.5.0",
54
- "ts-jest": "^29.1.1",
55
- "ts-patch": "^3.0.2",
54
+ "ts-jest": "^29.1.0",
55
+ "ts-patch": "^2.1.0",
56
56
  "tsd": "^0.28.1",
57
- "tslib": "^2.6.0",
58
- "typescript": "5.1.6",
57
+ "tslib": "^2.5.0",
58
+ "typescript": "4.8",
59
59
  "typescript-transform-paths": "^3.4.6"
60
60
  },
61
61
  "dependencies": {
62
- "justypes": "^4.2.1",
62
+ "justypes": "^4.2.0",
63
63
  "lodash-es": "^4.17.21"
64
64
  }
65
65
  }
@@ -0,0 +1,7 @@
1
+ import { enumKeys } from './enum-keys.js'
2
+
3
+ export function enumEntries<T extends Record<string, string | number>>(
4
+ _enum: T
5
+ ): Array<{ [Key in keyof T]: [Key, T[Key]] }[keyof T]> {
6
+ return enumKeys(_enum).map(key => [key, _enum[key]])
7
+ }
@@ -0,0 +1,6 @@
1
+ export function enumKeys<T extends Record<string, string | number>>(
2
+ _enum: T
3
+ ): Array<keyof T> {
4
+ return Object.keys(_enum)
5
+ .filter(key => Number.isNaN(Number(key))) // TypeScript枚举不能以数值为键
6
+ }
@@ -0,0 +1,8 @@
1
+ import { enumKeys } from './enum-keys.js'
2
+
3
+ export function enumValues<T extends Record<string, string | number>>(
4
+ _enum: T
5
+ ): Array<T[keyof T]> {
6
+ const keys = enumKeys(_enum)
7
+ return keys.map(key => _enum[key])
8
+ }
@@ -0,0 +1,16 @@
1
+ import { enumValues } from './enum-values.js'
2
+
3
+ export function inEnum<T extends Record<string, string | number>>(
4
+ val: string | number
5
+ , _enum: T
6
+ ): val is T[keyof T] {
7
+ return enumValues(_enum).includes(val as T[keyof T])
8
+ }
9
+
10
+ // 由于TypeScript无法从string | number里精确排除掉枚举类型, 因此返回值为boolean.
11
+ export function notInEnum(
12
+ val: string | number
13
+ , _enum: Record<string, string | number>
14
+ ): boolean {
15
+ return !inEnum(val, _enum)
16
+ }
@@ -0,0 +1,4 @@
1
+ export * from './in-enum.js'
2
+ export * from './enum-keys.js'
3
+ export * from './enum-values.js'
4
+ export * from './enum-entries.js'
package/src/index.ts CHANGED
@@ -6,7 +6,7 @@ export * from './object/index.js'
6
6
  export * from './json/index.js'
7
7
  export * from './string/index.js'
8
8
  export * from './nullish/index.js'
9
- export * from './in-enum.js'
9
+ export * from './enum/index.js'
10
10
  export * from './is-regexp.js'
11
11
  export * from './is-symbol.js'
12
12
  export * from './is-date.js'
package/lib/in-enum.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export declare function inEnum<T>(val: unknown, _enum: object): val is T;
2
- export declare function notInEnum<T, U>(val: T, _enum: object): val is Exclude<T, U>;
package/lib/in-enum.js DELETED
@@ -1,7 +0,0 @@
1
- export function inEnum(val, _enum) {
2
- return Object.values(_enum).includes(val);
3
- }
4
- export function notInEnum(val, _enum) {
5
- return !inEnum(val, _enum);
6
- }
7
- //# sourceMappingURL=in-enum.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"in-enum.js","sourceRoot":"","sources":["../src/in-enum.ts"],"names":[],"mappings":"AAGA,MAAM,UAAU,MAAM,CAAI,GAAY,EAAE,KAAa;IACnD,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAA;AAC3C,CAAC;AAED,MAAM,UAAU,SAAS,CAAO,GAAM,EAAE,KAAa;IACnD,OAAO,CAAC,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;AAC5B,CAAC"}
package/src/in-enum.ts DELETED
@@ -1,10 +0,0 @@
1
- // 由于以下原因, 该函数不应该被改写为其他形式:
2
- // - enum在TypeScript里被作为单独的类型看待, 不满足extends object
3
- // - 作为泛型时, enum参数会被提取为typeof enum类型而不是enum
4
- export function inEnum<T>(val: unknown, _enum: object): val is T {
5
- return Object.values(_enum).includes(val)
6
- }
7
-
8
- export function notInEnum<T, U>(val: T, _enum: object): val is Exclude<T, U> {
9
- return !inEnum(val, _enum)
10
- }