@ceale/util 1.19.0 → 1.20.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/dist/cjs/index.js CHANGED
@@ -21,13 +21,13 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
23
  CubicBezier: () => CubicBezier,
24
- Enum: () => Enum,
24
+ Enum: () => Enum2,
25
+ EnumV1: () => enum_v1_exports,
25
26
  QuadraticBezier: () => QuadraticBezier,
26
27
  assert: () => assert,
27
28
  css: () => css,
28
29
  cubicCurve: () => cubicCurve,
29
30
  debounce: () => debounce,
30
- defineEnum: () => defineEnum,
31
31
  expand: () => expand,
32
32
  quadraticCurve: () => quadraticCurve,
33
33
  sleep: () => sleep,
@@ -434,14 +434,6 @@ var tryCatch = (func, catchClass) => {
434
434
  // src/type.ts
435
435
  var assert = (variable) => variable;
436
436
  var expand = (variable) => variable;
437
- var defineEnum = (...keys) => keys.reduce((acc, key) => {
438
- acc[key] = key;
439
- return acc;
440
- }, {});
441
- var Enum = (keys) => ({
442
- keys: () => Object.keys(keys),
443
- includes: (key) => Object.keys(keys).includes(key)
444
- });
445
437
 
446
438
  // src/object.ts
447
439
  Object.defineProperty(Object.prototype, "hasKeys", {
@@ -462,3 +454,27 @@ Object.defineProperty(Object.prototype, "inKeys", {
462
454
  writable: true,
463
455
  configurable: true
464
456
  });
457
+
458
+ // src/enum_v1.ts
459
+ var enum_v1_exports = {};
460
+ __export(enum_v1_exports, {
461
+ Enum: () => Enum,
462
+ defineEnum: () => defineEnum
463
+ });
464
+ var defineEnum = (...keys) => keys.reduce((acc, key) => {
465
+ acc[key] = key;
466
+ return acc;
467
+ }, {});
468
+ var Enum = (keys) => ({
469
+ keys: () => Object.keys(keys),
470
+ includes: (key) => Object.keys(keys).includes(key)
471
+ });
472
+
473
+ // src/enum_v2.ts
474
+ var Enum2 = (...values) => {
475
+ const obj = {};
476
+ values.forEach((value) => obj[value.toUpperCase()] = value);
477
+ obj.values = () => values;
478
+ obj.includes = (value) => values.includes(value);
479
+ return obj;
480
+ };
package/dist/esm/index.js CHANGED
@@ -1,3 +1,9 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __export = (target, all) => {
3
+ for (var name in all)
4
+ __defProp(target, name, { get: all[name], enumerable: true });
5
+ };
6
+
1
7
  // src/string.ts
2
8
  Object.defineProperty(String.prototype, "removePrefix", {
3
9
  value: function(prefix) {
@@ -392,14 +398,6 @@ var tryCatch = (func, catchClass) => {
392
398
  // src/type.ts
393
399
  var assert = (variable) => variable;
394
400
  var expand = (variable) => variable;
395
- var defineEnum = (...keys) => keys.reduce((acc, key) => {
396
- acc[key] = key;
397
- return acc;
398
- }, {});
399
- var Enum = (keys) => ({
400
- keys: () => Object.keys(keys),
401
- includes: (key) => Object.keys(keys).includes(key)
402
- });
403
401
 
404
402
  // src/object.ts
405
403
  Object.defineProperty(Object.prototype, "hasKeys", {
@@ -420,15 +418,39 @@ Object.defineProperty(Object.prototype, "inKeys", {
420
418
  writable: true,
421
419
  configurable: true
422
420
  });
421
+
422
+ // src/enum_v1.ts
423
+ var enum_v1_exports = {};
424
+ __export(enum_v1_exports, {
425
+ Enum: () => Enum,
426
+ defineEnum: () => defineEnum
427
+ });
428
+ var defineEnum = (...keys) => keys.reduce((acc, key) => {
429
+ acc[key] = key;
430
+ return acc;
431
+ }, {});
432
+ var Enum = (keys) => ({
433
+ keys: () => Object.keys(keys),
434
+ includes: (key) => Object.keys(keys).includes(key)
435
+ });
436
+
437
+ // src/enum_v2.ts
438
+ var Enum2 = (...values) => {
439
+ const obj = {};
440
+ values.forEach((value) => obj[value.toUpperCase()] = value);
441
+ obj.values = () => values;
442
+ obj.includes = (value) => values.includes(value);
443
+ return obj;
444
+ };
423
445
  export {
424
446
  CubicBezier,
425
- Enum,
447
+ Enum2 as Enum,
448
+ enum_v1_exports as EnumV1,
426
449
  QuadraticBezier,
427
450
  assert,
428
451
  css,
429
452
  cubicCurve,
430
453
  debounce,
431
- defineEnum,
432
454
  expand,
433
455
  quadraticCurve,
434
456
  sleep,
@@ -0,0 +1,43 @@
1
+ /**
2
+ * 定义一个枚举类型。
3
+ * 返回一个枚举对象,其中每个键名与值相同。
4
+ * @param keys 若干个枚举的键名
5
+ *
6
+ * @example
7
+ * const type = defineEnum("A", "B", "C")
8
+ * type === (typeof type) === {
9
+ * A: "A",
10
+ * B: "B",
11
+ * C: "C"
12
+ * }
13
+ */
14
+ export declare const defineEnum: <T extends string>(...keys: T[]) => { [K in T]: K; };
15
+ /**
16
+ * 枚举工具函数,为枚举对象提供工具方法
17
+ * @param keys 由 defineEnum 创建的枚举对象
18
+ * @returns 返回一个包含`keys`、`includes`方法的对象
19
+ *
20
+ * @example
21
+ * const 对吗 = defineEnum("对", "不对")
22
+ *
23
+ * // 获取所有枚举键
24
+ * Enum(对吗).keys() // ["对", "不对"]
25
+ *
26
+ * // 检查值是否为有效枚举值
27
+ * Enum(对吗).includes("对") // true
28
+ * Enum(对吗).includes("错") // false
29
+ */
30
+ export declare const Enum: (keys: Record<string, string>) => {
31
+ keys: () => (keyof typeof keys)[];
32
+ includes: (key: any) => boolean;
33
+ };
34
+ /**
35
+ * 提取枚举的所有键组成联合类型
36
+ * @template E 由 defineEnum 创建的枚举对象
37
+ * @returns 枚举键的联合类型
38
+ *
39
+ * @example
40
+ * const 对吗 = defineEnum("对", "不对")
41
+ * Enum<typeof 对吗> // "对" | "不对"
42
+ */
43
+ export type EnumKeys<E> = E[keyof E];
@@ -0,0 +1,28 @@
1
+ type EnumKeyValue<T extends readonly string[]> = {
2
+ [K in T[number] as Uppercase<K>]: K;
3
+ };
4
+ interface EnumMethod<T extends readonly string[]> {
5
+ values(): T;
6
+ includes<V extends any>(value: V): boolean;
7
+ }
8
+ /**
9
+ * 提取枚举对象中的枚举值
10
+ */
11
+ export type EnumOf<T extends Record<string, any>> = Extract<T[keyof T], string>;
12
+ /**
13
+ * 创建一个枚举对象
14
+ * @param 若干个枚举值,字符串
15
+ * @returns 枚举对象,包含枚举值和枚举方法,枚举键为枚举值的大写
16
+ *
17
+ * @example
18
+ * const Example = Enum("value_a", "value_b")
19
+ * // {
20
+ * // VALUE_A: "value_a",
21
+ * // VALUE_B: "value_b"
22
+ * // values(): ["value_a", "value_b"],
23
+ * // includes(value: string): boolean
24
+ * // }
25
+ *
26
+ */
27
+ export declare const Enum: <T extends readonly string[]>(...values: T) => EnumKeyValue<T> & EnumMethod<T>;
28
+ export {};
@@ -8,3 +8,5 @@ export * from "./bezier";
8
8
  export * from "./error";
9
9
  export * from "./type";
10
10
  export * from "./object";
11
+ export * as EnumV1 from "./enum_v1";
12
+ export * from "./enum_v2";
@@ -22,46 +22,3 @@ export declare const assert: <Type = any>(variable: any) => asserts variable is
22
22
  * @returns 传入的变量,且类型被拓展为指定类型
23
23
  */
24
24
  export declare const expand: <Type>(variable: any) => asserts variable is (typeof variable & Type);
25
- /**
26
- * 定义一个枚举类型。
27
- * 返回一个枚举对象,其中每个键名与值相同。
28
- * @param keys 若干个枚举的键名
29
- *
30
- * @example
31
- * const type = defineEnum("A", "B", "C")
32
- * type === (typeof type) === {
33
- * A: "A",
34
- * B: "B",
35
- * C: "C"
36
- * }
37
- */
38
- export declare const defineEnum: <T extends string>(...keys: T[]) => { [K in T]: K; };
39
- /**
40
- * 枚举工具函数,为枚举对象提供工具方法
41
- * @param keys 由 defineEnum 创建的枚举对象
42
- * @returns 返回一个包含`keys`、`includes`方法的对象
43
- *
44
- * @example
45
- * const 对吗 = defineEnum("对", "不对")
46
- *
47
- * // 获取所有枚举键
48
- * Enum(对吗).keys() // ["对", "不对"]
49
- *
50
- * // 检查值是否为有效枚举值
51
- * Enum(对吗).includes("对") // true
52
- * Enum(对吗).includes("错") // false
53
- */
54
- export declare const Enum: (keys: Record<string, string>) => {
55
- keys: () => (keyof typeof keys)[];
56
- includes: (key: any) => boolean;
57
- };
58
- /**
59
- * 提取枚举的所有键组成联合类型
60
- * @template E 由 defineEnum 创建的枚举对象
61
- * @returns 枚举键的联合类型
62
- *
63
- * @example
64
- * const 对吗 = defineEnum("对", "不对")
65
- * Enum<typeof 对吗> // "对" | "不对"
66
- */
67
- export type EnumKeys<E> = E[keyof E];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ceale/util",
3
- "version": "1.19.0",
3
+ "version": "1.20.0",
4
4
  "author": "Ceale",
5
5
  "description": "小工具集",
6
6
  "repository": {