@wxhccc/es-util 1.6.0 → 2.0.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.
Files changed (39) hide show
  1. package/LICENSE +20 -20
  2. package/README.md +286 -57
  3. package/dist/array-tree-switch.d.ts +18 -2
  4. package/dist/date-time.d.ts +14 -0
  5. package/dist/event-target-emitter.d.ts +45 -0
  6. package/dist/event-target-emitter.js +147 -0
  7. package/dist/index.d.ts +4 -0
  8. package/dist/index.esm.js +499 -216
  9. package/dist/index.js +505 -218
  10. package/dist/index.min.js +2 -2
  11. package/dist/object-array.d.ts +7 -9
  12. package/dist/object-array.js +34 -16
  13. package/dist/optional.d.ts +6 -9
  14. package/dist/optional.js +136 -0
  15. package/dist/page-communicate.d.ts +27 -0
  16. package/dist/page-communicate.js +66 -0
  17. package/dist/promise.d.ts +21 -24
  18. package/dist/promise.js +24 -132
  19. package/dist/raf-timer.d.ts +36 -0
  20. package/dist/raf-timer.js +103 -0
  21. package/dist/types.d.ts +4 -0
  22. package/dist/utils.d.ts +10 -0
  23. package/dist/utils.js +30 -0
  24. package/dist/validate.js +54 -6
  25. package/dist/value-string-switch/byte-string.d.ts +9 -4
  26. package/dist/value-string-switch/index.d.ts +1 -0
  27. package/dist/value-string-switch/mask-data.d.ts +1 -2
  28. package/dist/value-string-switch/uri-b64.d.ts +8 -0
  29. package/dist/value-string-switch.js +28 -11
  30. package/package.json +19 -19
  31. package/types/array-tree-switch.d.ts +0 -49
  32. package/types/index.d.ts +0 -6
  33. package/types/object-array.d.ts +0 -33
  34. package/types/optional.d.ts +0 -50
  35. package/types/promise.d.ts +0 -44
  36. package/types/validate.d.ts +0 -2
  37. package/types/value-string-switch/byte-string.d.ts +0 -12
  38. package/types/value-string-switch/index.d.ts +0 -2
  39. package/types/value-string-switch/mask-data.d.ts +0 -22
@@ -0,0 +1,147 @@
1
+ /*!
2
+ * @wxhccc/es-util v2.0.1
3
+ * (c) 2022 wxhccc
4
+ * @license MIT
5
+ */
6
+ 'use strict';
7
+
8
+ Object.defineProperty(exports, '__esModule', { value: true });
9
+
10
+ var utils = require('./utils.js');
11
+
12
+ const isStr = (val) => typeof val === 'string';
13
+ const NOMETHOD = Symbol('NOMETHOD');
14
+ /**
15
+ * 创建emitter形式的实例,可以通过实例封装事件对象,比如websocket,得到一个更便捷的方法,具体可参考page-communicate.js文件内容
16
+ * @param options 配置项
17
+ * @returns
18
+ */
19
+ function eventTargetEmitter(options) {
20
+ const watchers = new Map([[NOMETHOD, new Set()]]);
21
+ const { name: currentName, customHanlderCreator } = options || {};
22
+ const customHanlder = utils.isFn(customHanlderCreator)
23
+ ? customHanlderCreator(watchers)
24
+ : null;
25
+ const splitRegExp = /\|-[a-z]+-\|/;
26
+ const listenerSafeRun = (listener, ...args) => {
27
+ try {
28
+ listener(...args);
29
+ }
30
+ catch (e) {
31
+ console.error(e);
32
+ }
33
+ };
34
+ const defaultHanlder = (payload) => {
35
+ const { method, data, originEmitterName: from, targetEmitterName: to } = payload || [];
36
+ if (method && typeof method === 'string') {
37
+ const matchListeners = [];
38
+ watchers.forEach((value, key) => {
39
+ if (typeof key === 'string' && key.startsWith(method)) {
40
+ const [, eFrom] = key.split(splitRegExp);
41
+ if (!eFrom || !from || eFrom === from) {
42
+ matchListeners.push(...value);
43
+ }
44
+ }
45
+ });
46
+ if (matchListeners.length &&
47
+ (!currentName || !to || currentName === to)) {
48
+ matchListeners.forEach((listener) => listenerSafeRun(listener, data));
49
+ }
50
+ }
51
+ const noMethodListeners = watchers.get(NOMETHOD);
52
+ if (noMethodListeners) {
53
+ noMethodListeners.forEach((listener) => listenerSafeRun(listener, data, payload));
54
+ }
55
+ };
56
+ const rootEventListener = utils.isFn(customHanlder) ? customHanlder : defaultHanlder;
57
+ const getFullMethodName = (method, options) => {
58
+ const { limitFrom = '', namespace = '' } = options || {};
59
+ return (limitFrom || namespace) && typeof method === 'string'
60
+ ? `${method}|-from-|${limitFrom}|-ns-|${namespace}`
61
+ : method;
62
+ };
63
+ const on = (method, listener, options) => {
64
+ if (typeof method === 'symbol' && method !== NOMETHOD) {
65
+ return;
66
+ }
67
+ const trueMethod = getFullMethodName(method, options);
68
+ if (!watchers.has(trueMethod)) {
69
+ watchers.set(trueMethod, new Set());
70
+ }
71
+ const listeners = watchers.get(trueMethod);
72
+ listeners?.add(listener);
73
+ };
74
+ const off = (method, callback, options) => {
75
+ if (typeof method === 'symbol' && method !== NOMETHOD) {
76
+ return;
77
+ }
78
+ const { namespace } = options || {};
79
+ if (namespace && !method) {
80
+ watchers.forEach((_value, key) => {
81
+ if (typeof key === 'string' && key.endsWith(namespace)) {
82
+ watchers.delete(key);
83
+ }
84
+ });
85
+ return;
86
+ }
87
+ const trueMethod = getFullMethodName(method, options);
88
+ if (!callback) {
89
+ watchers.delete(trueMethod);
90
+ return;
91
+ }
92
+ watchers.get(trueMethod)?.delete(callback);
93
+ };
94
+ const removeAllListeners = () => {
95
+ watchers.clear();
96
+ };
97
+ const createPayload = (methodOrPayload, data, target) => {
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;
108
+ };
109
+ const emit = (...args) => {
110
+ const payload = createPayload(...args);
111
+ rootEventListener(payload);
112
+ };
113
+ /** 获取当前所有监听方法的方法名,默认仅返回函数名,设置all为true可以返回具体来源限制和命名空间字段 */
114
+ const getMethodNames = (all) => {
115
+ if (!all) {
116
+ const result = [];
117
+ watchers.forEach((_v, key) => isStr(key) && result.push(key.split(splitRegExp)));
118
+ return result;
119
+ }
120
+ const result = [];
121
+ watchers.forEach((_v, key) => isStr(key) && result.push(key));
122
+ return result;
123
+ };
124
+ const getListeners = (method, options) => {
125
+ if (typeof method === 'symbol') {
126
+ return method === NOMETHOD ? Array.from(watchers.get(NOMETHOD) || []) : [];
127
+ }
128
+ const trueMethod = getFullMethodName(method, options);
129
+ const listeners = watchers.get(trueMethod);
130
+ return listeners ? Array.from(listeners) : [];
131
+ };
132
+ return {
133
+ name: currentName,
134
+ rootEventListener,
135
+ on,
136
+ off,
137
+ emit,
138
+ createPayload,
139
+ removeAllListeners,
140
+ getMethodNames,
141
+ getListeners
142
+ };
143
+ }
144
+
145
+ exports.NOMETHOD = NOMETHOD;
146
+ exports["default"] = eventTargetEmitter;
147
+ exports.eventTargetEmitter = eventTargetEmitter;
package/dist/index.d.ts CHANGED
@@ -4,3 +4,7 @@ export * from './object-array';
4
4
  export * from './value-string-switch';
5
5
  export * from './promise';
6
6
  export * from './optional';
7
+ export * from './event-target-emitter';
8
+ export * from './page-communicate';
9
+ export * from './raf-timer';
10
+ export * from './date-time';