@wxhccc/es-util 1.7.0 → 2.0.2
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 +267 -60
- package/dist/array-tree-switch.d.ts +18 -2
- package/dist/date-time.d.ts +14 -0
- package/dist/event-target-emitter.d.ts +7 -7
- package/dist/event-target-emitter.js +38 -17
- package/dist/index.d.ts +2 -0
- package/dist/index.esm.js +342 -231
- package/dist/index.js +345 -233
- package/dist/index.min.js +2 -2
- package/dist/object-array.d.ts +2 -2
- package/dist/object-array.js +25 -17
- package/dist/optional.d.ts +6 -9
- package/dist/optional.js +136 -0
- package/dist/page-communicate.js +10 -14
- package/dist/promise.d.ts +21 -24
- package/dist/promise.js +24 -132
- package/dist/raf-timer.d.ts +36 -0
- package/dist/raf-timer.js +103 -0
- package/dist/utils.d.ts +10 -0
- package/dist/utils.js +30 -0
- package/dist/validate.js +54 -6
- package/dist/value-string-switch/byte-string.d.ts +9 -4
- package/dist/value-string-switch/index.d.ts +1 -0
- package/dist/value-string-switch/mask-data.d.ts +1 -2
- package/dist/value-string-switch/uri-b64.d.ts +8 -0
- package/dist/value-string-switch.js +28 -11
- package/package.json +17 -16
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @wxhccc/es-util
|
|
2
|
+
* @wxhccc/es-util v2.0.2
|
|
3
3
|
* (c) 2022 wxhccc
|
|
4
4
|
* @license MIT
|
|
5
5
|
*/
|
|
@@ -7,6 +7,8 @@
|
|
|
7
7
|
|
|
8
8
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
9
9
|
|
|
10
|
+
var utils = require('./utils.js');
|
|
11
|
+
|
|
10
12
|
const isStr = (val) => typeof val === 'string';
|
|
11
13
|
const NOMETHOD = Symbol('NOMETHOD');
|
|
12
14
|
/**
|
|
@@ -17,7 +19,9 @@ const NOMETHOD = Symbol('NOMETHOD');
|
|
|
17
19
|
function eventTargetEmitter(options) {
|
|
18
20
|
const watchers = new Map([[NOMETHOD, new Set()]]);
|
|
19
21
|
const { name: currentName, customHanlderCreator } = options || {};
|
|
20
|
-
const customHanlder = customHanlderCreator
|
|
22
|
+
const customHanlder = utils.isFn(customHanlderCreator)
|
|
23
|
+
? customHanlderCreator(watchers)
|
|
24
|
+
: null;
|
|
21
25
|
const splitRegExp = /\|-[a-z]+-\|/;
|
|
22
26
|
const listenerSafeRun = (listener, ...args) => {
|
|
23
27
|
try {
|
|
@@ -33,25 +37,28 @@ function eventTargetEmitter(options) {
|
|
|
33
37
|
const matchListeners = [];
|
|
34
38
|
watchers.forEach((value, key) => {
|
|
35
39
|
if (typeof key === 'string' && key.startsWith(method)) {
|
|
36
|
-
const [, eFrom
|
|
40
|
+
const [, eFrom] = key.split(splitRegExp);
|
|
37
41
|
if (!eFrom || !from || eFrom === from) {
|
|
38
42
|
matchListeners.push(...value);
|
|
39
43
|
}
|
|
40
44
|
}
|
|
41
45
|
});
|
|
42
|
-
if (matchListeners.length &&
|
|
43
|
-
|
|
46
|
+
if (matchListeners.length &&
|
|
47
|
+
(!currentName || !to || currentName === to)) {
|
|
48
|
+
matchListeners.forEach((listener) => listenerSafeRun(listener, data));
|
|
44
49
|
}
|
|
45
50
|
}
|
|
46
51
|
const noMethodListeners = watchers.get(NOMETHOD);
|
|
47
52
|
if (noMethodListeners) {
|
|
48
|
-
noMethodListeners.forEach(listener => listenerSafeRun(listener, data, payload));
|
|
53
|
+
noMethodListeners.forEach((listener) => listenerSafeRun(listener, data, payload));
|
|
49
54
|
}
|
|
50
55
|
};
|
|
51
|
-
const rootEventListener = customHanlder
|
|
56
|
+
const rootEventListener = utils.isFn(customHanlder) ? customHanlder : defaultHanlder;
|
|
52
57
|
const getFullMethodName = (method, options) => {
|
|
53
58
|
const { limitFrom = '', namespace = '' } = options || {};
|
|
54
|
-
return (limitFrom || namespace) && typeof method === 'string'
|
|
59
|
+
return (limitFrom || namespace) && typeof method === 'string'
|
|
60
|
+
? `${method}|-from-|${limitFrom}|-ns-|${namespace}`
|
|
61
|
+
: method;
|
|
55
62
|
};
|
|
56
63
|
const on = (method, listener, options) => {
|
|
57
64
|
if (typeof method === 'symbol' && method !== NOMETHOD) {
|
|
@@ -68,7 +75,7 @@ function eventTargetEmitter(options) {
|
|
|
68
75
|
if (typeof method === 'symbol' && method !== NOMETHOD) {
|
|
69
76
|
return;
|
|
70
77
|
}
|
|
71
|
-
const {
|
|
78
|
+
const { namespace } = options || {};
|
|
72
79
|
if (namespace && !method) {
|
|
73
80
|
watchers.forEach((_value, key) => {
|
|
74
81
|
if (typeof key === 'string' && key.endsWith(namespace)) {
|
|
@@ -88,12 +95,16 @@ function eventTargetEmitter(options) {
|
|
|
88
95
|
watchers.clear();
|
|
89
96
|
};
|
|
90
97
|
const createPayload = (methodOrPayload, data, target) => {
|
|
91
|
-
return isStr(methodOrPayload)
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
98
|
+
return isStr(methodOrPayload)
|
|
99
|
+
? {
|
|
100
|
+
method: methodOrPayload,
|
|
101
|
+
data,
|
|
102
|
+
...(target && isStr(target) ? { targetEmitterName: target } : {}),
|
|
103
|
+
...(currentName && isStr(currentName)
|
|
104
|
+
? { originEmitterName: currentName }
|
|
105
|
+
: {})
|
|
106
|
+
}
|
|
107
|
+
: methodOrPayload;
|
|
97
108
|
};
|
|
98
109
|
const emit = (...args) => {
|
|
99
110
|
const payload = createPayload(...args);
|
|
@@ -118,9 +129,19 @@ function eventTargetEmitter(options) {
|
|
|
118
129
|
const listeners = watchers.get(trueMethod);
|
|
119
130
|
return listeners ? Array.from(listeners) : [];
|
|
120
131
|
};
|
|
121
|
-
return {
|
|
132
|
+
return {
|
|
133
|
+
name: currentName,
|
|
134
|
+
rootEventListener,
|
|
135
|
+
on,
|
|
136
|
+
off,
|
|
137
|
+
emit,
|
|
138
|
+
createPayload,
|
|
139
|
+
removeAllListeners,
|
|
140
|
+
getMethodNames,
|
|
141
|
+
getListeners
|
|
142
|
+
};
|
|
122
143
|
}
|
|
123
144
|
|
|
124
145
|
exports.NOMETHOD = NOMETHOD;
|
|
125
|
-
exports
|
|
146
|
+
exports["default"] = eventTargetEmitter;
|
|
126
147
|
exports.eventTargetEmitter = eventTargetEmitter;
|