grofc_utils 1.2.1 → 1.4.1
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/index.js +1 -0
- package/package.json +5 -1
- package/src/event.js +139 -0
- package/src/guard.js +114 -4
- package/src/missingData.js +1 -1
- package/src/object.js +26 -3
- package/types/index.d.ts +1 -0
- package/types/src/event.d.ts +49 -0
- package/types/src/guard.d.ts +68 -0
- package/types/src/object.d.ts +17 -1
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "grofc_utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -18,6 +18,10 @@
|
|
|
18
18
|
"import": "./src/array.js",
|
|
19
19
|
"types": "./types/src/array.d.ts"
|
|
20
20
|
},
|
|
21
|
+
"./event": {
|
|
22
|
+
"import": "./src/event.js",
|
|
23
|
+
"types": "./types/src/event.d.ts"
|
|
24
|
+
},
|
|
21
25
|
"./guard": {
|
|
22
26
|
"import": "./src/guard.js",
|
|
23
27
|
"types": "./types/src/guard.d.ts"
|
package/src/event.js
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { isPlainObject, throwIfIsNotExpectedValue, throwIfIsNotFunction, throwIfIsNotPlainObject, throwIfIsNotString } from "./guard.js"
|
|
2
|
+
import { assignWithDescriptors } from "./object.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 创建一个事件发射器对象,用于管理和触发自定义事件
|
|
6
|
+
* @returns {{
|
|
7
|
+
* dispatchSync:(
|
|
8
|
+
* event:string,
|
|
9
|
+
* ing:(
|
|
10
|
+
* ingEvent:{
|
|
11
|
+
* target,name:string,type:string|null,preData:object,phase:"ing",data:object
|
|
12
|
+
* })=>void,
|
|
13
|
+
* options:{target,name:string,type:string|null,status:object,data:object}
|
|
14
|
+
* )=>object
|
|
15
|
+
* on:(event:string,phase:"before"|"begin"|"end"|"after",callback:(event:{
|
|
16
|
+
* target,name:string,type:string|null,preData:object,data:object
|
|
17
|
+
* })=>void)=>void
|
|
18
|
+
* off:(event:string,phase:"before"|"begin"|"end"|"after",callback:(event:{
|
|
19
|
+
* target,name:string,type:string|null,preData:object,data:object
|
|
20
|
+
* })=>void)=>void
|
|
21
|
+
* }} 包含dispatchSync, on, off方法的对象
|
|
22
|
+
*/
|
|
23
|
+
export function createEventEmitter() {
|
|
24
|
+
const listenerMap = new Map()
|
|
25
|
+
const dispatchSync = (event, ing = () => { }, options = {}) => {
|
|
26
|
+
throwIfIsNotString(event, "event");
|
|
27
|
+
throwIfIsNotFunction(ing, "ing");
|
|
28
|
+
throwIfIsNotPlainObject(options, "options");
|
|
29
|
+
const { type = null, target = null, data = {}, status = { cancellable: true }, ...customData } = options;
|
|
30
|
+
const baseEvent = {
|
|
31
|
+
get target() {
|
|
32
|
+
return target
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
get name() {
|
|
36
|
+
return event
|
|
37
|
+
},
|
|
38
|
+
get type() {
|
|
39
|
+
return type
|
|
40
|
+
},
|
|
41
|
+
get preData() {
|
|
42
|
+
return { ...data }
|
|
43
|
+
},
|
|
44
|
+
get customData() {
|
|
45
|
+
return { ...customData }
|
|
46
|
+
},
|
|
47
|
+
get status() {
|
|
48
|
+
return { ...status }
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
// 触发before阶段监听器,如果返回false且事件可取消则中断执行
|
|
52
|
+
const beforeEvent = assignWithDescriptors({
|
|
53
|
+
get phase() {
|
|
54
|
+
return "before"
|
|
55
|
+
}
|
|
56
|
+
}, baseEvent)
|
|
57
|
+
const beforeCallbackList = listenerMap.get(event + ":before") ?? [];
|
|
58
|
+
for (const callback of beforeCallbackList) {
|
|
59
|
+
if (callback(beforeEvent) === false && beforeEvent.status.cancellable) return;
|
|
60
|
+
}
|
|
61
|
+
// 触发begin阶段监听器
|
|
62
|
+
const beginEvent = assignWithDescriptors({
|
|
63
|
+
get phase() {
|
|
64
|
+
return "begin"
|
|
65
|
+
}
|
|
66
|
+
}, baseEvent);
|
|
67
|
+
const beginCallbackList = listenerMap.get(event + ":begin") ?? [];
|
|
68
|
+
for (const callback of beginCallbackList) {
|
|
69
|
+
callback(beginEvent)
|
|
70
|
+
}
|
|
71
|
+
// 执行ing阶段函数并获取当前数据
|
|
72
|
+
const ingEvent = assignWithDescriptors({
|
|
73
|
+
get phase() {
|
|
74
|
+
return "ing"
|
|
75
|
+
}
|
|
76
|
+
}, baseEvent)
|
|
77
|
+
const rawCurrentData = ing(ingEvent);
|
|
78
|
+
const currentData = typeof rawCurrentData === "object" && rawCurrentData !== null ? { ...rawCurrentData } : {};
|
|
79
|
+
// 触发end阶段监听器
|
|
80
|
+
const endEvent = assignWithDescriptors({
|
|
81
|
+
get phase() {
|
|
82
|
+
return "end"
|
|
83
|
+
},
|
|
84
|
+
get currentData() {
|
|
85
|
+
return { ...currentData }
|
|
86
|
+
}
|
|
87
|
+
}, baseEvent)
|
|
88
|
+
const endCallbackList = listenerMap.get(event + ":end") ?? []
|
|
89
|
+
for (const callback of endCallbackList) {
|
|
90
|
+
callback(endEvent)
|
|
91
|
+
}
|
|
92
|
+
// 触发after阶段监听器,并处理可能的递归事件调用
|
|
93
|
+
const afterEvent = assignWithDescriptors({
|
|
94
|
+
get phase() {
|
|
95
|
+
return "after"
|
|
96
|
+
},
|
|
97
|
+
get currentData() {
|
|
98
|
+
return { ...currentData }
|
|
99
|
+
}
|
|
100
|
+
}, baseEvent);
|
|
101
|
+
const afterCallbackList = listenerMap.get(event + ":after") ?? [];
|
|
102
|
+
for (const callback of afterCallbackList) {
|
|
103
|
+
const result = callback(afterEvent)
|
|
104
|
+
if (!isPlainObject(result)) continue;
|
|
105
|
+
const { event, ing, options } = result;
|
|
106
|
+
if (typeof event !== "string") continue;
|
|
107
|
+
dispatchSync(event, ing, options)
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
const on = (event, phase, callback) => {
|
|
111
|
+
throwIfIsNotString(event, "event");
|
|
112
|
+
throwIfIsNotExpectedValue(phase, "phase", "before", "begin", "end", "after");
|
|
113
|
+
throwIfIsNotFunction(callback, "callback");
|
|
114
|
+
const eventName = event + ":" + phase;
|
|
115
|
+
const funcList = listenerMap.get(eventName)
|
|
116
|
+
if (Array.isArray(funcList)) {
|
|
117
|
+
funcList.push(callback)
|
|
118
|
+
} else {
|
|
119
|
+
listenerMap.set(eventName, [callback]);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
const off = (event, phase, callback) => {
|
|
123
|
+
throwIfIsNotString(event, "event");
|
|
124
|
+
throwIfIsNotExpectedValue(phase, "phase", "before", "begin", "end", "after");
|
|
125
|
+
throwIfIsNotFunction(callback, "callback");
|
|
126
|
+
const eventName = event + ":" + phase;
|
|
127
|
+
const list = listenerMap.get(eventName);
|
|
128
|
+
if (list) {
|
|
129
|
+
const index = list.indexOf(callback);
|
|
130
|
+
if (index !== -1) list.splice(index, 1);
|
|
131
|
+
if (list.length === 0) listenerMap.delete(eventName);
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
return {
|
|
135
|
+
dispatchSync,
|
|
136
|
+
on,
|
|
137
|
+
off
|
|
138
|
+
}
|
|
139
|
+
}
|
package/src/guard.js
CHANGED
|
@@ -5,7 +5,6 @@
|
|
|
5
5
|
*/
|
|
6
6
|
const getType = v => v === null ? "null" : typeof v
|
|
7
7
|
|
|
8
|
-
|
|
9
8
|
class GuardError extends Error {
|
|
10
9
|
/**
|
|
11
10
|
*
|
|
@@ -46,10 +45,51 @@ const throwRangeErrorGiveValue = (variable, name = "variable", ...acceptableRang
|
|
|
46
45
|
if (acceptableRangeDescs.length === 1) throw RangeError(`Expected ${name} to be ${acceptableRangeDescs}, but got ${variable}.`)
|
|
47
46
|
else if (acceptableRangeDescs.length > 1) throw RangeError(`Expected ${name} to be ${acceptableRangeDescs.slice(0, -1).join(" ,")} or ${acceptableRangeDescs[acceptableRangeDescs.length - 1]}, but got ${variable}.`)
|
|
48
47
|
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 检查给定对象是否为普通对象
|
|
51
|
+
* 普通对象是指通过对象字面量 {} 或 new Object() 创建的对象,
|
|
52
|
+
* 不包括数组、函数以及其他自定义构造函数创建的实例
|
|
53
|
+
* @param {*} obj - 需要检查的对象
|
|
54
|
+
* @returns {boolean} 如果是普通对象返回 true,否则返回 false
|
|
55
|
+
*/
|
|
56
|
+
export function isPlainObject(obj) {
|
|
57
|
+
if (typeof obj !== "object" || obj === null) return false;
|
|
58
|
+
const prototype = Object.getPrototypeOf(obj);
|
|
59
|
+
if (prototype !== Object.prototype && prototype !== null) return false;
|
|
60
|
+
return true
|
|
61
|
+
}
|
|
62
|
+
|
|
49
63
|
// ------------------------------------------------
|
|
50
64
|
// 值校验守卫函数
|
|
51
65
|
// ------------------------------------------------
|
|
52
|
-
|
|
66
|
+
/**
|
|
67
|
+
* 检查变量是否为期望值之一,如果不是则抛出类型错误
|
|
68
|
+
* @param {*} variable - 要检查的变量
|
|
69
|
+
* @param {string} name - 变量名称(用于错误消息),默认值为"variable"
|
|
70
|
+
* @param {...*} expectedValues - 期望值列表
|
|
71
|
+
* @throws {TypeError} 当变量不是期望值之一时抛出类型错误
|
|
72
|
+
*/
|
|
73
|
+
export function throwIfIsNotExpectedValue(variable, name = "variable", ...expectedValues) {
|
|
74
|
+
safeGuardExecute(throwIfIsNotNonEmptyArray, ...expectedValues)
|
|
75
|
+
if (!expectedValues.includes(variable)) {
|
|
76
|
+
throwTypeErrorGiveType(variable, name, ...expectedValues);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* 检查变量是否为不期望的值之一,如果是则抛出类型错误
|
|
81
|
+
* @param {*} variable - 要检查的变量
|
|
82
|
+
* @param {string} name - 变量名称(用于错误消息),默认值为"variable"
|
|
83
|
+
* @param {...*} unexpectedValues - 不期望的值列表
|
|
84
|
+
* @throws {TypeError} 当变量是不期望的值之一时抛出类型错误
|
|
85
|
+
*/
|
|
86
|
+
export function throwIfIsUnExpectedValue(variable, name = "variable", ...unexpectedValues) {
|
|
87
|
+
safeGuardExecute(throwIfIsNotNonEmptyArray, ...unexpectedValues)
|
|
88
|
+
if (unexpectedValues.includes(variable)) {
|
|
89
|
+
const [f, ...upexpedValuesWf] = unexpectedValues;
|
|
90
|
+
throwTypeErrorGiveType(variable, name, `not ${f}`, ...upexpedValuesWf);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
53
93
|
/**
|
|
54
94
|
* 检查变量是否为空值(不为 null 或 undefined)
|
|
55
95
|
* @param {*} variable - 要检查的变量
|
|
@@ -231,6 +271,7 @@ export function throwIfIsNotBigInt(variable, name = "variable") {
|
|
|
231
271
|
// ------------------------------------------------
|
|
232
272
|
// 对象类型守卫函数
|
|
233
273
|
// ------------------------------------------------
|
|
274
|
+
|
|
234
275
|
/**
|
|
235
276
|
* 检查变量是否为普通对象(非 null,非数组)
|
|
236
277
|
* @param {*} variable - 要检查的变量
|
|
@@ -238,7 +279,7 @@ export function throwIfIsNotBigInt(variable, name = "variable") {
|
|
|
238
279
|
* @throws {TypeError} 当变量不是普通对象时抛出类型错误
|
|
239
280
|
*/
|
|
240
281
|
export function throwIfIsNotPlainObject(variable, name = "variable") {
|
|
241
|
-
if (
|
|
282
|
+
if (!isPlainObject(variable)) {
|
|
242
283
|
throwTypeErrorGiveType(variable, name, "a plain object");
|
|
243
284
|
}
|
|
244
285
|
}
|
|
@@ -381,18 +422,39 @@ export function throwIfIsNotIterableObject(variable, name = "") {
|
|
|
381
422
|
//------------------------------------------------
|
|
382
423
|
// 数组类型守卫函数
|
|
383
424
|
// ------------------------------------------------
|
|
425
|
+
/**
|
|
426
|
+
* 检查变量是否为数组,如果不是则抛出类型错误
|
|
427
|
+
* @param {*} variable - 要检查的变量
|
|
428
|
+
* @param {string} name - 变量名称(用于错误消息),默认值为"variable"
|
|
429
|
+
* @throws {TypeError} 当变量不是数组时抛出类型错误
|
|
430
|
+
*/
|
|
384
431
|
export function throwIfIsNotArray(variable, name = "variable") {
|
|
385
432
|
if (!Array.isArray(variable)) {
|
|
386
433
|
throwTypeErrorGiveType(variable, name, "an array");
|
|
387
434
|
}
|
|
388
435
|
}
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* 检查变量是否为非空数组,如果不是则抛出错误
|
|
439
|
+
* @param {*} variable - 要检查的变量
|
|
440
|
+
* @param {string} name - 变量名称(用于错误消息),默认值为"variable"
|
|
441
|
+
* @throws {TypeError} 当变量不是数组时抛出类型错误
|
|
442
|
+
* @throws {Error} 当变量是空数组时抛出错误
|
|
443
|
+
*/
|
|
389
444
|
export function throwIfIsNotNonEmptyArray(variable, name = "variable") {
|
|
390
445
|
throwIfIsNotArray(variable, name);
|
|
391
446
|
if (variable.length === 0) {
|
|
392
|
-
|
|
447
|
+
throw new Error(`Expected ${variable} to have at least one item, but got zero.`)
|
|
393
448
|
}
|
|
394
449
|
}
|
|
395
450
|
|
|
451
|
+
/**
|
|
452
|
+
* 检查变量是否为仅包含字符串的数组,如果不是则抛出类型错误
|
|
453
|
+
* @param {*} variable - 要检查的变量
|
|
454
|
+
* @param {string} name - 变量名称(用于错误消息),默认值为"variable"
|
|
455
|
+
* @param {string} generalTerm - 通用术语(用于错误消息),默认值为`all elements of ${name || "array"}`
|
|
456
|
+
* @throws {TypeError} 当变量不是数组或包含非字符串元素时抛出类型错误
|
|
457
|
+
*/
|
|
396
458
|
export function throwIfIsNotStringArray(variable, name = "variable", generalTerm = `all elements of ${name || "array"}`) {
|
|
397
459
|
throwIfIsNotArray(variable, name);
|
|
398
460
|
const acceptType = "strings";
|
|
@@ -402,6 +464,54 @@ export function throwIfIsNotStringArray(variable, name = "variable", generalTerm
|
|
|
402
464
|
}
|
|
403
465
|
}
|
|
404
466
|
}
|
|
467
|
+
/**
|
|
468
|
+
* 检查变量是否为仅包含BigInt值的数组,如果不是则抛出类型错误
|
|
469
|
+
* @param {*} variable - 需要检查的变量
|
|
470
|
+
* @param {string} [name="variable"] - 变量名称,用于错误消息显示
|
|
471
|
+
* @param {string} [generalTerm=`all elements of ${name || "array"}`] - 通用术语,用于错误消息显示
|
|
472
|
+
* @throws {TypeError} 当变量不是数组或包含非BigInt元素时抛出类型错误
|
|
473
|
+
*/
|
|
474
|
+
export function throwIfIsNotBigIntArray(variable, name = "variable", generalTerm = `all elements of ${name || "array"}`) {
|
|
475
|
+
throwIfIsNotArray(variable, name);
|
|
476
|
+
const acceptType = "bigints";
|
|
477
|
+
for (const e of variable) {
|
|
478
|
+
if (typeof e !== "bigint") {
|
|
479
|
+
throwTypeErrorForArray(generalTerm, acceptType, `a non-bigint value of type ${getType(e)}`)
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
/**
|
|
484
|
+
* 检查变量是否为仅包含Symbol值的数组,如果不是则抛出类型错误
|
|
485
|
+
* @param {*} variable - 需要检查的变量
|
|
486
|
+
* @param {string} [name="variable"] - 变量名称,用于错误消息显示
|
|
487
|
+
* @param {string} [generalTerm=`all elements of ${name || "array"}`] - 通用术语,用于错误消息显示
|
|
488
|
+
* @throws {TypeError} 当变量不是数组或包含非Symbol元素时抛出类型错误
|
|
489
|
+
*/
|
|
490
|
+
export function throwIfIsNotSymbolArray(variable, name = "variable", generalTerm = `all elements of ${name || "array"}`) {
|
|
491
|
+
throwIfIsNotArray(variable, name);
|
|
492
|
+
const acceptType = "symbols";
|
|
493
|
+
for (const e of variable) {
|
|
494
|
+
if (typeof e !== "symbol") {
|
|
495
|
+
throwTypeErrorForArray(generalTerm, acceptType, `a non-symbol value of type ${getType(e)}`)
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
/**
|
|
500
|
+
* 检查变量是否为仅包含普通对象的数组,如果不是则抛出类型错误
|
|
501
|
+
* @param {*} variable - 需要检查的变量
|
|
502
|
+
* @param {string} [name="variable"] - 变量名称,用于错误消息显示
|
|
503
|
+
* @param {string} [generalTerm=`all elements of ${name || "array"}`] - 通用术语,用于错误消息显示
|
|
504
|
+
* @throws {TypeError} 当变量不是数组或包含非普通对象元素时抛出类型错误
|
|
505
|
+
*/
|
|
506
|
+
export function throwIfIsNotPlainObjectArray(variable, name = "variable", generalTerm = `all elements of ${name || "array"}`) {
|
|
507
|
+
throwIfIsNotArray(variable, name);
|
|
508
|
+
const acceptType = "plain objects";
|
|
509
|
+
for (const e of variable) {
|
|
510
|
+
if (isPlainObject(variable)) {
|
|
511
|
+
throwTypeErrorForArray(generalTerm, acceptType, `a non-plain object value of type ${e}`)
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
}
|
|
405
515
|
export function throwIfIsNotNumberArray(variable, name = "variable", generalTerm = `all elements of ${name || "array"}`) {
|
|
406
516
|
throwIfIsNotArray(variable, name);
|
|
407
517
|
const acceptType = "numbers";
|
package/src/missingData.js
CHANGED
package/src/object.js
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
|
-
import { throwIfIsNotPlainObject, throwIfKeyMissing } from "./guard";
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { throwIfIsNotPlainObject, throwIfIsNotPlainObjectArray, throwIfKeyMissing } from "./guard.js";
|
|
2
|
+
/**
|
|
3
|
+
* 将对象的指定属性设置为只读
|
|
4
|
+
* @param {Object} obj - 需要修改的对象
|
|
5
|
+
* @param {string} name - 需要设为只读的属性名
|
|
6
|
+
* @throws {TypeError} 当obj不是普通对象或name不是字符串时会抛出类型错误
|
|
7
|
+
* @throws {Error} 当对象中找不到指定属性时会抛出错误
|
|
8
|
+
*/
|
|
9
|
+
export function makePropertyReadOnly(obj, name) {
|
|
4
10
|
throwIfIsNotPlainObject(obj);
|
|
5
11
|
throwIfKeyMissing(obj, name, "obj");
|
|
6
12
|
const descriptor = Object.getOwnPropertyDescriptor(obj, name);
|
|
@@ -10,4 +16,21 @@ export function makeReadOnlyProperty(obj, name) {
|
|
|
10
16
|
configurable: false,
|
|
11
17
|
enumerable: descriptor.enumerable ?? true
|
|
12
18
|
})
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 使用属性描述符将一个或多个源对象的属性分配到目标对象中
|
|
23
|
+
* @param {Object} target - 目标对象,将接收来自其他对象的属性
|
|
24
|
+
* @param {...Object} sources - 源对象列表,其所有可枚举和不可枚举属性都将被复制到目标对象中
|
|
25
|
+
* @throws {TypeError} 当target不是普通对象时抛出类型错误
|
|
26
|
+
* @throws {TypeError} 当sources中的任何一个元素不是普通对象时抛出类型错误
|
|
27
|
+
* @returns {void}
|
|
28
|
+
*/
|
|
29
|
+
export function assignWithDescriptors(target, ...sources) {
|
|
30
|
+
throwIfIsNotPlainObject(target);
|
|
31
|
+
throwIfIsNotPlainObjectArray(sources);
|
|
32
|
+
for (const obj of sources) {
|
|
33
|
+
Object.defineProperties(target, Object.getOwnPropertyDescriptors(obj))
|
|
34
|
+
}
|
|
35
|
+
return target;
|
|
13
36
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 创建一个事件发射器对象,用于管理和触发自定义事件
|
|
3
|
+
* @returns {{
|
|
4
|
+
* dispatchSync:(
|
|
5
|
+
* event:string,
|
|
6
|
+
* ing:(
|
|
7
|
+
* ingEvent:{
|
|
8
|
+
* target,name:string,type:string|null,preData:object,phase:"ing",data:object
|
|
9
|
+
* })=>void,
|
|
10
|
+
* options:{target,name:string,type:string|null,status:object,data:object}
|
|
11
|
+
* )=>object
|
|
12
|
+
* on:(event:string,phase:"before"|"begin"|"end"|"after",callback:(event:{
|
|
13
|
+
* target,name:string,type:string|null,preData:object,data:object
|
|
14
|
+
* })=>void)=>void
|
|
15
|
+
* off:(event:string,phase:"before"|"begin"|"end"|"after",callback:(event:{
|
|
16
|
+
* target,name:string,type:string|null,preData:object,data:object
|
|
17
|
+
* })=>void)=>void
|
|
18
|
+
* }} 包含dispatchSync, on, off方法的对象
|
|
19
|
+
*/
|
|
20
|
+
export function createEventEmitter(): {
|
|
21
|
+
dispatchSync: (event: string, ing: (ingEvent: {
|
|
22
|
+
target: any;
|
|
23
|
+
name: string;
|
|
24
|
+
type: string | null;
|
|
25
|
+
preData: object;
|
|
26
|
+
phase: "ing";
|
|
27
|
+
data: object;
|
|
28
|
+
}) => void, options: {
|
|
29
|
+
target: any;
|
|
30
|
+
name: string;
|
|
31
|
+
type: string | null;
|
|
32
|
+
status: object;
|
|
33
|
+
data: object;
|
|
34
|
+
}) => object;
|
|
35
|
+
on: (event: string, phase: "before" | "begin" | "end" | "after", callback: (event: {
|
|
36
|
+
target: any;
|
|
37
|
+
name: string;
|
|
38
|
+
type: string | null;
|
|
39
|
+
preData: object;
|
|
40
|
+
data: object;
|
|
41
|
+
}) => void) => void;
|
|
42
|
+
off: (event: string, phase: "before" | "begin" | "end" | "after", callback: (event: {
|
|
43
|
+
target: any;
|
|
44
|
+
name: string;
|
|
45
|
+
type: string | null;
|
|
46
|
+
preData: object;
|
|
47
|
+
data: object;
|
|
48
|
+
}) => void) => void;
|
|
49
|
+
};
|
package/types/src/guard.d.ts
CHANGED
|
@@ -1,3 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 检查给定对象是否为普通对象
|
|
3
|
+
* 普通对象是指通过对象字面量 {} 或 new Object() 创建的对象,
|
|
4
|
+
* 不包括数组、函数以及其他自定义构造函数创建的实例
|
|
5
|
+
* @param {*} obj - 需要检查的对象
|
|
6
|
+
* @returns {boolean} 如果是普通对象返回 true,否则返回 false
|
|
7
|
+
*/
|
|
8
|
+
export function isPlainObject(obj: any): boolean;
|
|
9
|
+
/**
|
|
10
|
+
* 检查变量是否为期望值之一,如果不是则抛出类型错误
|
|
11
|
+
* @param {*} variable - 要检查的变量
|
|
12
|
+
* @param {string} name - 变量名称(用于错误消息),默认值为"variable"
|
|
13
|
+
* @param {...*} expectedValues - 期望值列表
|
|
14
|
+
* @throws {TypeError} 当变量不是期望值之一时抛出类型错误
|
|
15
|
+
*/
|
|
16
|
+
export function throwIfIsNotExpectedValue(variable: any, name?: string, ...expectedValues: any[]): void;
|
|
17
|
+
/**
|
|
18
|
+
* 检查变量是否为不期望的值之一,如果是则抛出类型错误
|
|
19
|
+
* @param {*} variable - 要检查的变量
|
|
20
|
+
* @param {string} name - 变量名称(用于错误消息),默认值为"variable"
|
|
21
|
+
* @param {...*} unexpectedValues - 不期望的值列表
|
|
22
|
+
* @throws {TypeError} 当变量是不期望的值之一时抛出类型错误
|
|
23
|
+
*/
|
|
24
|
+
export function throwIfIsUnExpectedValue(variable: any, name?: string, ...unexpectedValues: any[]): void;
|
|
1
25
|
/**
|
|
2
26
|
* 检查变量是否为空值(不为 null 或 undefined)
|
|
3
27
|
* @param {*} variable - 要检查的变量
|
|
@@ -171,9 +195,53 @@ export function throwIfIsNotIterable(variable: any, name?: string): void;
|
|
|
171
195
|
* @throws {TypeError} 当变量不是可迭代对象时抛出类型错误
|
|
172
196
|
*/
|
|
173
197
|
export function throwIfIsNotIterableObject(variable: any, name?: string): void;
|
|
198
|
+
/**
|
|
199
|
+
* 检查变量是否为数组,如果不是则抛出类型错误
|
|
200
|
+
* @param {*} variable - 要检查的变量
|
|
201
|
+
* @param {string} name - 变量名称(用于错误消息),默认值为"variable"
|
|
202
|
+
* @throws {TypeError} 当变量不是数组时抛出类型错误
|
|
203
|
+
*/
|
|
174
204
|
export function throwIfIsNotArray(variable: any, name?: string): void;
|
|
205
|
+
/**
|
|
206
|
+
* 检查变量是否为非空数组,如果不是则抛出错误
|
|
207
|
+
* @param {*} variable - 要检查的变量
|
|
208
|
+
* @param {string} name - 变量名称(用于错误消息),默认值为"variable"
|
|
209
|
+
* @throws {TypeError} 当变量不是数组时抛出类型错误
|
|
210
|
+
* @throws {Error} 当变量是空数组时抛出错误
|
|
211
|
+
*/
|
|
175
212
|
export function throwIfIsNotNonEmptyArray(variable: any, name?: string): void;
|
|
213
|
+
/**
|
|
214
|
+
* 检查变量是否为仅包含字符串的数组,如果不是则抛出类型错误
|
|
215
|
+
* @param {*} variable - 要检查的变量
|
|
216
|
+
* @param {string} name - 变量名称(用于错误消息),默认值为"variable"
|
|
217
|
+
* @param {string} generalTerm - 通用术语(用于错误消息),默认值为`all elements of ${name || "array"}`
|
|
218
|
+
* @throws {TypeError} 当变量不是数组或包含非字符串元素时抛出类型错误
|
|
219
|
+
*/
|
|
176
220
|
export function throwIfIsNotStringArray(variable: any, name?: string, generalTerm?: string): void;
|
|
221
|
+
/**
|
|
222
|
+
* 检查变量是否为仅包含BigInt值的数组,如果不是则抛出类型错误
|
|
223
|
+
* @param {*} variable - 需要检查的变量
|
|
224
|
+
* @param {string} [name="variable"] - 变量名称,用于错误消息显示
|
|
225
|
+
* @param {string} [generalTerm=`all elements of ${name || "array"}`] - 通用术语,用于错误消息显示
|
|
226
|
+
* @throws {TypeError} 当变量不是数组或包含非BigInt元素时抛出类型错误
|
|
227
|
+
*/
|
|
228
|
+
export function throwIfIsNotBigIntArray(variable: any, name?: string, generalTerm?: string): void;
|
|
229
|
+
/**
|
|
230
|
+
* 检查变量是否为仅包含Symbol值的数组,如果不是则抛出类型错误
|
|
231
|
+
* @param {*} variable - 需要检查的变量
|
|
232
|
+
* @param {string} [name="variable"] - 变量名称,用于错误消息显示
|
|
233
|
+
* @param {string} [generalTerm=`all elements of ${name || "array"}`] - 通用术语,用于错误消息显示
|
|
234
|
+
* @throws {TypeError} 当变量不是数组或包含非Symbol元素时抛出类型错误
|
|
235
|
+
*/
|
|
236
|
+
export function throwIfIsNotSymbolArray(variable: any, name?: string, generalTerm?: string): void;
|
|
237
|
+
/**
|
|
238
|
+
* 检查变量是否为仅包含普通对象的数组,如果不是则抛出类型错误
|
|
239
|
+
* @param {*} variable - 需要检查的变量
|
|
240
|
+
* @param {string} [name="variable"] - 变量名称,用于错误消息显示
|
|
241
|
+
* @param {string} [generalTerm=`all elements of ${name || "array"}`] - 通用术语,用于错误消息显示
|
|
242
|
+
* @throws {TypeError} 当变量不是数组或包含非普通对象元素时抛出类型错误
|
|
243
|
+
*/
|
|
244
|
+
export function throwIfIsNotPlainObjectArray(variable: any, name?: string, generalTerm?: string): void;
|
|
177
245
|
export function throwIfIsNotNumberArray(variable: any, name?: string, generalTerm?: string): void;
|
|
178
246
|
/**
|
|
179
247
|
* 检查变量是否为仅包含非NaN数字的数组,如果不是则抛出类型错误
|
package/types/src/object.d.ts
CHANGED
|
@@ -1 +1,17 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* 将对象的指定属性设置为只读
|
|
3
|
+
* @param {Object} obj - 需要修改的对象
|
|
4
|
+
* @param {string} name - 需要设为只读的属性名
|
|
5
|
+
* @throws {TypeError} 当obj不是普通对象或name不是字符串时会抛出类型错误
|
|
6
|
+
* @throws {Error} 当对象中找不到指定属性时会抛出错误
|
|
7
|
+
*/
|
|
8
|
+
export function makePropertyReadOnly(obj: Object, name: string): void;
|
|
9
|
+
/**
|
|
10
|
+
* 使用属性描述符将一个或多个源对象的属性分配到目标对象中
|
|
11
|
+
* @param {Object} target - 目标对象,将接收来自其他对象的属性
|
|
12
|
+
* @param {...Object} sources - 源对象列表,其所有可枚举和不可枚举属性都将被复制到目标对象中
|
|
13
|
+
* @throws {TypeError} 当target不是普通对象时抛出类型错误
|
|
14
|
+
* @throws {TypeError} 当sources中的任何一个元素不是普通对象时抛出类型错误
|
|
15
|
+
* @returns {void}
|
|
16
|
+
*/
|
|
17
|
+
export function assignWithDescriptors(target: Object, ...sources: Object[]): void;
|