@ceale/util 1.14.1 → 1.15.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/README.md CHANGED
@@ -43,4 +43,7 @@
43
43
  - tryCatch()
44
44
  - `type.ts`
45
45
  - assert()
46
- - defineEnum()
46
+ - expand()
47
+ - defineEnum()
48
+ - Enum()
49
+ - EnumKeys<>
package/dist/cjs/index.js CHANGED
@@ -37,12 +37,14 @@ __export(exports_src, {
37
37
  sleepAsync: () => sleepAsync,
38
38
  sleep: () => sleep,
39
39
  quadraticCurve: () => quadraticCurve,
40
+ expand: () => expand,
40
41
  defineEnum: () => defineEnum,
41
42
  debounce: () => debounce,
42
43
  cubicCurve: () => cubicCurve,
43
44
  css: () => css,
44
45
  assert: () => assert,
45
46
  QuadraticBezier: () => QuadraticBezier,
47
+ Enum: () => Enum,
46
48
  CubicBezier: () => CubicBezier
47
49
  });
48
50
  module.exports = __toCommonJS(exports_src);
@@ -422,10 +424,13 @@ var tryCatch = (parameter) => {
422
424
  throw new TypeError("参数类型错误,应为 Promise 或函数");
423
425
  };
424
426
  // src/type.ts
425
- var assert = (variable) => {};
426
- var defineEnum = (...keys) => {
427
- return keys.reduce((acc, key) => {
428
- acc[key] = key;
429
- return acc;
430
- }, {});
431
- };
427
+ var assert = (variable) => variable;
428
+ var expand = (variable) => variable;
429
+ var defineEnum = (...keys) => keys.reduce((acc, key) => {
430
+ acc[key] = key;
431
+ return acc;
432
+ }, {});
433
+ var Enum = (keys) => ({
434
+ keys: () => Object.keys(keys),
435
+ includes: (key) => Object.keys(keys).includes(key)
436
+ });
package/dist/esm/index.js CHANGED
@@ -373,13 +373,16 @@ var tryCatch = (parameter) => {
373
373
  throw new TypeError("参数类型错误,应为 Promise 或函数");
374
374
  };
375
375
  // src/type.ts
376
- var assert = (variable) => {};
377
- var defineEnum = (...keys) => {
378
- return keys.reduce((acc, key) => {
379
- acc[key] = key;
380
- return acc;
381
- }, {});
382
- };
376
+ var assert = (variable) => variable;
377
+ var expand = (variable) => variable;
378
+ var defineEnum = (...keys) => keys.reduce((acc, key) => {
379
+ acc[key] = key;
380
+ return acc;
381
+ }, {});
382
+ var Enum = (keys) => ({
383
+ keys: () => Object.keys(keys),
384
+ includes: (key) => Object.keys(keys).includes(key)
385
+ });
383
386
  export {
384
387
  waitSync,
385
388
  wait,
@@ -389,11 +392,13 @@ export {
389
392
  sleepAsync,
390
393
  sleep,
391
394
  quadraticCurve,
395
+ expand,
392
396
  defineEnum,
393
397
  debounce,
394
398
  cubicCurve,
395
399
  css,
396
400
  assert,
397
401
  QuadraticBezier,
402
+ Enum,
398
403
  CubicBezier
399
404
  };
@@ -2,8 +2,16 @@
2
2
  * 就地断言一个变量为指定类型
3
3
  * @template Type 断言的类型,默认为`any`
4
4
  * @param variable 待断言的变量
5
+ * @returns 传入的变量,且类型已经被断言为指定类型
5
6
  */
6
7
  export declare const assert: <Type = any>(variable: any) => asserts variable is Type;
8
+ /**
9
+ * 拓展一个变量的类型
10
+ * @param variable 待拓展的变量
11
+ * @param Type 拓展的类型
12
+ * @returns 传入的变量,且类型被拓展为指定类型
13
+ */
14
+ export declare const expand: <Type>(variable: any) => asserts variable is (typeof variable & Type);
7
15
  /**
8
16
  * 定义一个枚举类型。
9
17
  * 返回一个枚举对象,其中每个键名与值相同。
@@ -18,3 +26,32 @@ export declare const assert: <Type = any>(variable: any) => asserts variable is
18
26
  * }
19
27
  */
20
28
  export declare const defineEnum: <T extends string>(...keys: T[]) => { [K in T]: K; };
29
+ /**
30
+ * 枚举工具函数,为枚举对象提供工具方法
31
+ * @param keys 由 defineEnum 创建的枚举对象
32
+ * @returns 返回一个包含`keys`、`includes`方法的对象
33
+ *
34
+ * @example
35
+ * const 对吗 = defineEnum("对", "不对")
36
+ *
37
+ * // 获取所有枚举键
38
+ * Enum(对吗).keys() // ["对", "不对"]
39
+ *
40
+ * // 检查值是否为有效枚举值
41
+ * Enum(对吗).includes("对") // true
42
+ * Enum(对吗).includes("错") // false
43
+ */
44
+ export declare const Enum: (keys: Record<string, string>) => {
45
+ keys: () => (keyof typeof keys)[];
46
+ includes: (key: any) => boolean;
47
+ };
48
+ /**
49
+ * 提取枚举的所有键组成联合类型
50
+ * @template E 由 defineEnum 创建的枚举对象
51
+ * @returns 枚举键的联合类型
52
+ *
53
+ * @example
54
+ * const 对吗 = defineEnum("对", "不对")
55
+ * Enum<typeof 对吗> // "对" | "不对"
56
+ */
57
+ export type EnumKeys<E> = E[keyof E];
package/package.json CHANGED
@@ -1,7 +1,9 @@
1
1
  {
2
2
  "name": "@ceale/util",
3
3
  "author": "Ceale",
4
- "version": "1.14.1",
4
+ "description": "小工具集",
5
+ "license": "Unlicense or CC0 or WTFPL",
6
+ "version": "1.15.0",
5
7
  "module": "index.ts",
6
8
  "type": "module",
7
9
  "main": "dist/esm/index.js",