@ceale/util 1.19.0 → 1.21.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
@@ -10,7 +10,7 @@
10
10
  - removeAllSuffixes()
11
11
  - toCamelCase()
12
12
  - toKebabCase()
13
- - Function.prototype
13
+ - namespace: `ClassUtil`
14
14
  - isDirectSubclass()
15
15
  - isSubclass()
16
16
  - isDirectInstance()
@@ -44,11 +44,16 @@
44
44
  - `type.ts`
45
45
  - assert()
46
46
  - expand()
47
+ - `type` anyobject
48
+ - `type` AnyClass
49
+ - `type` RequireOne
50
+ - `enum_v2.ts`:
51
+ - Enum()
52
+ - EnumOf<>
53
+ - `enum_v1.ts`
47
54
  - defineEnum()
48
55
  - Enum()
49
56
  - EnumKeys<>
50
- - `type` anyobject
51
- - `type` AnyClass
52
57
  - Object.prototype
53
58
  - hasKeys()
54
59
  - inKeys()
package/dist/cjs/index.js CHANGED
@@ -20,14 +20,15 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
+ ClassUtil: () => ClassUtil,
23
24
  CubicBezier: () => CubicBezier,
24
- Enum: () => Enum,
25
+ Enum: () => Enum2,
26
+ EnumV1: () => enum_v1_exports,
25
27
  QuadraticBezier: () => QuadraticBezier,
26
28
  assert: () => assert,
27
29
  css: () => css,
28
30
  cubicCurve: () => cubicCurve,
29
31
  debounce: () => debounce,
30
- defineEnum: () => defineEnum,
31
32
  expand: () => expand,
32
33
  quadraticCurve: () => quadraticCurve,
33
34
  sleep: () => sleep,
@@ -138,55 +139,38 @@ var uri;
138
139
  })(uri || (uri = {}));
139
140
 
140
141
  // src/class.ts
141
- Object.defineProperty(Function.prototype, "isDirectSubclass", {
142
- value: function(potentialChild) {
143
- if (typeof potentialChild !== "function" || typeof this !== "function") {
142
+ var ClassUtil;
143
+ ((ClassUtil2) => {
144
+ ClassUtil2.isDirectSubclass = (parent, potentialChild) => {
145
+ if (typeof potentialChild !== "function" || typeof parent !== "function") {
144
146
  return false;
145
147
  }
146
- return Object.getPrototypeOf(potentialChild) === this;
147
- },
148
- writable: true,
149
- configurable: true,
150
- enumerable: false
151
- });
152
- Object.defineProperty(Function.prototype, "isSubclass", {
153
- value: function(potentialDescendant) {
154
- if (typeof potentialDescendant !== "function" || typeof this !== "function") {
148
+ return Object.getPrototypeOf(potentialChild) === parent;
149
+ };
150
+ ClassUtil2.isSubclass = (parent, potentialDescendant) => {
151
+ if (typeof potentialDescendant !== "function" || typeof parent !== "function") {
155
152
  return false;
156
153
  }
157
154
  let current = potentialDescendant;
158
155
  while (typeof current === "function") {
159
- if (current === this) {
156
+ if (current === parent) {
160
157
  return true;
161
158
  }
162
159
  current = Object.getPrototypeOf(current);
163
- if (current === Function.prototype) break;
160
+ if (!current || current === Function.prototype) break;
164
161
  }
165
162
  return false;
166
- },
167
- writable: true,
168
- configurable: true,
169
- enumerable: false
170
- });
171
- Object.defineProperty(Function.prototype, "isDirectInstance", {
172
- value: function(potentialInstance) {
173
- if (typeof this !== "function" || potentialInstance === null || typeof potentialInstance !== "object") {
163
+ };
164
+ ClassUtil2.isDirectInstance = (targetClass, potentialInstance) => {
165
+ if (typeof targetClass !== "function" || potentialInstance === null || typeof potentialInstance !== "object") {
174
166
  return false;
175
167
  }
176
- return Object.getPrototypeOf(potentialInstance).constructor === this;
177
- },
178
- writable: true,
179
- configurable: true,
180
- enumerable: false
181
- });
182
- Object.defineProperty(Function.prototype, "isInstance", {
183
- value: function(potentialInstance) {
184
- return typeof this === "function" && potentialInstance instanceof this;
185
- },
186
- writable: true,
187
- configurable: true,
188
- enumerable: false
189
- });
168
+ return Object.getPrototypeOf(potentialInstance)?.constructor === targetClass;
169
+ };
170
+ ClassUtil2.isInstance = (targetClass, potentialInstance) => {
171
+ return typeof targetClass === "function" && potentialInstance instanceof targetClass;
172
+ };
173
+ })(ClassUtil || (ClassUtil = {}));
190
174
 
191
175
  // src/css.ts
192
176
  var css;
@@ -434,14 +418,6 @@ var tryCatch = (func, catchClass) => {
434
418
  // src/type.ts
435
419
  var assert = (variable) => variable;
436
420
  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
421
 
446
422
  // src/object.ts
447
423
  Object.defineProperty(Object.prototype, "hasKeys", {
@@ -462,3 +438,27 @@ Object.defineProperty(Object.prototype, "inKeys", {
462
438
  writable: true,
463
439
  configurable: true
464
440
  });
441
+
442
+ // src/enum_v1.ts
443
+ var enum_v1_exports = {};
444
+ __export(enum_v1_exports, {
445
+ Enum: () => Enum,
446
+ defineEnum: () => defineEnum
447
+ });
448
+ var defineEnum = (...keys) => keys.reduce((acc, key) => {
449
+ acc[key] = key;
450
+ return acc;
451
+ }, {});
452
+ var Enum = (keys) => ({
453
+ keys: () => Object.keys(keys),
454
+ includes: (key) => Object.keys(keys).includes(key)
455
+ });
456
+
457
+ // src/enum_v2.ts
458
+ var Enum2 = (...values) => {
459
+ const obj = {};
460
+ values.forEach((value) => obj[value.toUpperCase()] = value);
461
+ obj.values = () => values;
462
+ obj.includes = (value) => values.includes(value);
463
+ return obj;
464
+ };
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) {
@@ -96,55 +102,38 @@ var uri;
96
102
  })(uri || (uri = {}));
97
103
 
98
104
  // src/class.ts
99
- Object.defineProperty(Function.prototype, "isDirectSubclass", {
100
- value: function(potentialChild) {
101
- if (typeof potentialChild !== "function" || typeof this !== "function") {
105
+ var ClassUtil;
106
+ ((ClassUtil2) => {
107
+ ClassUtil2.isDirectSubclass = (parent, potentialChild) => {
108
+ if (typeof potentialChild !== "function" || typeof parent !== "function") {
102
109
  return false;
103
110
  }
104
- return Object.getPrototypeOf(potentialChild) === this;
105
- },
106
- writable: true,
107
- configurable: true,
108
- enumerable: false
109
- });
110
- Object.defineProperty(Function.prototype, "isSubclass", {
111
- value: function(potentialDescendant) {
112
- if (typeof potentialDescendant !== "function" || typeof this !== "function") {
111
+ return Object.getPrototypeOf(potentialChild) === parent;
112
+ };
113
+ ClassUtil2.isSubclass = (parent, potentialDescendant) => {
114
+ if (typeof potentialDescendant !== "function" || typeof parent !== "function") {
113
115
  return false;
114
116
  }
115
117
  let current = potentialDescendant;
116
118
  while (typeof current === "function") {
117
- if (current === this) {
119
+ if (current === parent) {
118
120
  return true;
119
121
  }
120
122
  current = Object.getPrototypeOf(current);
121
- if (current === Function.prototype) break;
123
+ if (!current || current === Function.prototype) break;
122
124
  }
123
125
  return false;
124
- },
125
- writable: true,
126
- configurable: true,
127
- enumerable: false
128
- });
129
- Object.defineProperty(Function.prototype, "isDirectInstance", {
130
- value: function(potentialInstance) {
131
- if (typeof this !== "function" || potentialInstance === null || typeof potentialInstance !== "object") {
126
+ };
127
+ ClassUtil2.isDirectInstance = (targetClass, potentialInstance) => {
128
+ if (typeof targetClass !== "function" || potentialInstance === null || typeof potentialInstance !== "object") {
132
129
  return false;
133
130
  }
134
- return Object.getPrototypeOf(potentialInstance).constructor === this;
135
- },
136
- writable: true,
137
- configurable: true,
138
- enumerable: false
139
- });
140
- Object.defineProperty(Function.prototype, "isInstance", {
141
- value: function(potentialInstance) {
142
- return typeof this === "function" && potentialInstance instanceof this;
143
- },
144
- writable: true,
145
- configurable: true,
146
- enumerable: false
147
- });
131
+ return Object.getPrototypeOf(potentialInstance)?.constructor === targetClass;
132
+ };
133
+ ClassUtil2.isInstance = (targetClass, potentialInstance) => {
134
+ return typeof targetClass === "function" && potentialInstance instanceof targetClass;
135
+ };
136
+ })(ClassUtil || (ClassUtil = {}));
148
137
 
149
138
  // src/css.ts
150
139
  var css;
@@ -392,14 +381,6 @@ var tryCatch = (func, catchClass) => {
392
381
  // src/type.ts
393
382
  var assert = (variable) => variable;
394
383
  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
384
 
404
385
  // src/object.ts
405
386
  Object.defineProperty(Object.prototype, "hasKeys", {
@@ -420,15 +401,40 @@ Object.defineProperty(Object.prototype, "inKeys", {
420
401
  writable: true,
421
402
  configurable: true
422
403
  });
404
+
405
+ // src/enum_v1.ts
406
+ var enum_v1_exports = {};
407
+ __export(enum_v1_exports, {
408
+ Enum: () => Enum,
409
+ defineEnum: () => defineEnum
410
+ });
411
+ var defineEnum = (...keys) => keys.reduce((acc, key) => {
412
+ acc[key] = key;
413
+ return acc;
414
+ }, {});
415
+ var Enum = (keys) => ({
416
+ keys: () => Object.keys(keys),
417
+ includes: (key) => Object.keys(keys).includes(key)
418
+ });
419
+
420
+ // src/enum_v2.ts
421
+ var Enum2 = (...values) => {
422
+ const obj = {};
423
+ values.forEach((value) => obj[value.toUpperCase()] = value);
424
+ obj.values = () => values;
425
+ obj.includes = (value) => values.includes(value);
426
+ return obj;
427
+ };
423
428
  export {
429
+ ClassUtil,
424
430
  CubicBezier,
425
- Enum,
431
+ Enum2 as Enum,
432
+ enum_v1_exports as EnumV1,
426
433
  QuadraticBezier,
427
434
  assert,
428
435
  css,
429
436
  cubicCurve,
430
437
  debounce,
431
- defineEnum,
432
438
  expand,
433
439
  quadraticCurve,
434
440
  sleep,
@@ -1,32 +1,33 @@
1
1
  import type { AnyClass } from "./type";
2
- declare global {
3
- interface Function {
4
- /**
5
- * 检查 `potentialChild` 是否是当前类的直接子类
6
- * @param potentialChild 潜在的子类构造函数
7
- * @returns 如果是直接子类,则返回 `true`;否则返回 `false`
8
- */
9
- isDirectSubclass(this: AnyClass, potentialChild: unknown): boolean;
10
- /**
11
- * 检查 `potentialDescendant` 是否是当前类的子孙类或当前类本身
12
- * @param potentialDescendant 潜在的子孙类或类本身
13
- * @returns 如果是子孙类或类本身,则返回 `true`;否则返回 `false`
14
- */
15
- isSubclass(this: AnyClass, potentialDescendant: unknown): boolean;
16
- /**
17
- * 检查 `potentialInstance` 是否由当前类直接构造
18
- * 判断依据: `Object.getPrototypeOf(potentialInstance).constructor === this`
19
- * @param potentialInstance 潜在的实例对象
20
- * @returns 如果是由当前类直接创建的实例,则返回 `true`;否则返回 `false`
21
- */
22
- isDirectInstance(this: AnyClass, potentialInstance: unknown): boolean;
23
- /**
24
- * 检查 `potentialInstance` 是否是当前类或其任何子类的实例
25
- * 内部使用 `instanceof` 操作符
26
- * @param potentialInstance 潜在的实例对象
27
- * @returns 如果是实例,则返回 `true`;否则返回 `false`
28
- */
29
- isInstance(this: AnyClass, potentialInstance: unknown): boolean;
30
- }
2
+ export declare namespace ClassUtil {
3
+ /**
4
+ * 检查 `potentialChild` 是否是当前类的直接子类
5
+ * @param parent 当前类(父类)
6
+ * @param potentialChild 潜在的子类构造函数
7
+ * @returns 如果是直接子类,则返回 `true`;否则返回 `false`
8
+ */
9
+ const isDirectSubclass: (parent: AnyClass, potentialChild: unknown) => boolean;
10
+ /**
11
+ * 检查 `potentialDescendant` 是否是当前类的子孙类或当前类本身
12
+ * @param parent 当前类
13
+ * @param potentialDescendant 潜在的子孙类或类本身
14
+ * @returns 如果是子孙类或类本身,则返回 `true`;否则返回 `false`
15
+ */
16
+ const isSubclass: (parent: AnyClass, potentialDescendant: unknown) => boolean;
17
+ /**
18
+ * 检查 `potentialInstance` 是否由当前类直接构造
19
+ * 判断依据: `Object.getPrototypeOf(potentialInstance).constructor === targetClass`
20
+ * @param targetClass 当前类
21
+ * @param potentialInstance 潜在的实例对象
22
+ * @returns 如果是由当前类直接创建的实例,则返回 `true`;否则返回 `false`
23
+ */
24
+ const isDirectInstance: (targetClass: AnyClass, potentialInstance: unknown) => boolean;
25
+ /**
26
+ * 检查 `potentialInstance` 是否是当前类或其任何子类的实例
27
+ * 内部使用 `instanceof` 操作符
28
+ * @param targetClass 当前类
29
+ * @param potentialInstance 潜在的实例对象
30
+ * @returns 如果是实例,则返回 `true`;否则返回 `false`
31
+ */
32
+ const isInstance: (targetClass: AnyClass, potentialInstance: unknown) => boolean;
31
33
  }
32
- export {};
@@ -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";
@@ -23,45 +23,17 @@ export declare const assert: <Type = any>(variable: any) => asserts variable is
23
23
  */
24
24
  export declare const expand: <Type>(variable: any) => asserts variable is (typeof variable & Type);
25
25
  /**
26
- * 定义一个枚举类型。
27
- * 返回一个枚举对象,其中每个键名与值相同。
28
- * @param keys 若干个枚举的键名
29
- *
26
+ * 强制对象类型中至少包含一个指定的属性。
30
27
  * @example
31
- * const type = defineEnum("A", "B", "C")
32
- * type === (typeof type) === {
33
- * A: "A",
34
- * B: "B",
35
- * C: "C"
36
- * }
28
+ * ```ts
29
+ * interface User { id: string; name: string; age: number; }
30
+ * // 有效:包含一个或多个属性
31
+ * const update: RequireOne<User> = { name: 'Gemini' };
32
+ * // 错误:不允许空对象
33
+ * const invalid: RequireOne<User> = {};
34
+ * ```
35
+ * @template T - 需要处理的基础对象接口。其中不应有可选属性,有可能会导致未预料的情况。
37
36
  */
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];
37
+ export type RequireOne<T> = {
38
+ [K in keyof T]: Required<Pick<T, K>> & Partial<Omit<T, K>>;
39
+ }[keyof T];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ceale/util",
3
- "version": "1.19.0",
3
+ "version": "1.21.0",
4
4
  "author": "Ceale",
5
5
  "description": "小工具集",
6
6
  "repository": {