@ceale/util 1.20.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,6 +20,7 @@ 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
25
  Enum: () => Enum2,
25
26
  EnumV1: () => enum_v1_exports,
@@ -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;
package/dist/esm/index.js CHANGED
@@ -102,55 +102,38 @@ var uri;
102
102
  })(uri || (uri = {}));
103
103
 
104
104
  // src/class.ts
105
- Object.defineProperty(Function.prototype, "isDirectSubclass", {
106
- value: function(potentialChild) {
107
- 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") {
108
109
  return false;
109
110
  }
110
- return Object.getPrototypeOf(potentialChild) === this;
111
- },
112
- writable: true,
113
- configurable: true,
114
- enumerable: false
115
- });
116
- Object.defineProperty(Function.prototype, "isSubclass", {
117
- value: function(potentialDescendant) {
118
- 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") {
119
115
  return false;
120
116
  }
121
117
  let current = potentialDescendant;
122
118
  while (typeof current === "function") {
123
- if (current === this) {
119
+ if (current === parent) {
124
120
  return true;
125
121
  }
126
122
  current = Object.getPrototypeOf(current);
127
- if (current === Function.prototype) break;
123
+ if (!current || current === Function.prototype) break;
128
124
  }
129
125
  return false;
130
- },
131
- writable: true,
132
- configurable: true,
133
- enumerable: false
134
- });
135
- Object.defineProperty(Function.prototype, "isDirectInstance", {
136
- value: function(potentialInstance) {
137
- 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") {
138
129
  return false;
139
130
  }
140
- return Object.getPrototypeOf(potentialInstance).constructor === this;
141
- },
142
- writable: true,
143
- configurable: true,
144
- enumerable: false
145
- });
146
- Object.defineProperty(Function.prototype, "isInstance", {
147
- value: function(potentialInstance) {
148
- return typeof this === "function" && potentialInstance instanceof this;
149
- },
150
- writable: true,
151
- configurable: true,
152
- enumerable: false
153
- });
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 = {}));
154
137
 
155
138
  // src/css.ts
156
139
  var css;
@@ -443,6 +426,7 @@ var Enum2 = (...values) => {
443
426
  return obj;
444
427
  };
445
428
  export {
429
+ ClassUtil,
446
430
  CubicBezier,
447
431
  Enum2 as Enum,
448
432
  enum_v1_exports as EnumV1,
@@ -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 {};
@@ -22,3 +22,18 @@ 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
+ * @example
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 - 需要处理的基础对象接口。其中不应有可选属性,有可能会导致未预料的情况。
36
+ */
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.20.0",
3
+ "version": "1.21.0",
4
4
  "author": "Ceale",
5
5
  "description": "小工具集",
6
6
  "repository": {