@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 +8 -3
- package/dist/cjs/index.js +20 -36
- package/dist/esm/index.js +20 -36
- package/dist/types/class.d.ts +31 -30
- package/dist/types/type.d.ts +15 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
- removeAllSuffixes()
|
|
11
11
|
- toCamelCase()
|
|
12
12
|
- toKebabCase()
|
|
13
|
-
-
|
|
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
|
-
|
|
142
|
-
|
|
143
|
-
|
|
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) ===
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
|
|
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 ===
|
|
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
|
-
|
|
168
|
-
|
|
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)
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
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
|
-
|
|
106
|
-
|
|
107
|
-
|
|
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) ===
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
|
|
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 ===
|
|
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
|
-
|
|
132
|
-
|
|
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)
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
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,
|
package/dist/types/class.d.ts
CHANGED
|
@@ -1,32 +1,33 @@
|
|
|
1
1
|
import type { AnyClass } from "./type";
|
|
2
|
-
declare
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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 {};
|
package/dist/types/type.d.ts
CHANGED
|
@@ -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];
|