baja-lite 1.0.2 → 1.0.3

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/src/set-ex.ts ADDED
@@ -0,0 +1,362 @@
1
+ export class SetEx<T> extends Set {
2
+ private uniqueKey: keyof T;
3
+ private whenOnExist?: (oldData: T, newData: T) => void | null;
4
+ private whenOnNotExist?: (newData: T) => void | null;
5
+ private replaceItemWhenExits: boolean;
6
+ /**
7
+ * @param key 识别是否存在的对象的属性名
8
+ * @param onExist 当存在时作何操作? oldData/newData 哪个将添加到set,由replaceItemWhenExits决定,默认是oldData生效
9
+ * @param onNotExist 当不存在时作何操作?
10
+ * @param replaceWhenExits 当存在时是否覆盖?
11
+ * @param values 初始数组
12
+ */
13
+ constructor(
14
+ key: keyof T | {
15
+ key: keyof T;
16
+ onExist?: (oldData: T, newData: T) => void;
17
+ onNotExist?: (newData: T) => void;
18
+ replaceWhenExits?: boolean;
19
+ values?: ReadonlyArray<T> | null;
20
+ },
21
+ onExist?: (oldData: T, newData: T) => void,
22
+ replaceWhenExits = false,
23
+ values?: ReadonlyArray<T> | null,
24
+ onNotExist?: (newData: T) => void
25
+ ) {
26
+ super();
27
+ if (typeof key === 'object') {
28
+ this.whenOnExist = key.onExist;
29
+ this.uniqueKey = key.key;
30
+ this.replaceItemWhenExits = key.replaceWhenExits === true;
31
+ this.whenOnNotExist = key.onNotExist;
32
+ if (key.values) {
33
+ this.addAll(...key.values);
34
+ }
35
+ } else {
36
+ this.whenOnExist = onExist;
37
+ this.uniqueKey = key;
38
+ this.replaceItemWhenExits = replaceWhenExits;
39
+ this.whenOnNotExist = onNotExist;
40
+ if (values) {
41
+ this.addAll(...values);
42
+ }
43
+ }
44
+ }
45
+
46
+ /**
47
+ *
48
+ * 添加返回
49
+ * @param {T} value
50
+ * @returns {this} 当前对象
51
+ */
52
+ override add(value: T): this {
53
+ let flag = false;
54
+ this.forEach((item: T): any => {
55
+ if (item[this.uniqueKey] === value[this.uniqueKey]) {
56
+ flag = true;
57
+ if (this.whenOnExist) {
58
+ this.whenOnExist(item, value);
59
+ }
60
+ if (this.replaceItemWhenExits === true) {
61
+ super.delete(item);
62
+ flag = false;
63
+ }
64
+ return false;
65
+ }
66
+ });
67
+ if (flag === false) {
68
+ super.add(value);
69
+ if (this.whenOnNotExist) {
70
+ this.whenOnNotExist(value);
71
+ }
72
+ }
73
+ return this;
74
+ }
75
+ /**
76
+ * 批量添加
77
+ * @param values
78
+ * @returns 当前对象
79
+ */
80
+ addAll(...values: T[]): this {
81
+ for (const value of values) {
82
+ this.add(value);
83
+ }
84
+ return this;
85
+ }
86
+ /**
87
+ *
88
+ * 添加
89
+ * @param {T} value
90
+ * @returns {T} 添加成功的对象:可能是新加入集合的,也可能是原本存在的
91
+ */
92
+ add2(value: T): T {
93
+ let flag = false;
94
+ let tmp = value;
95
+ this.forEach((item: T): any => {
96
+ if (item[this.uniqueKey] === value[this.uniqueKey]) {
97
+ flag = true;
98
+ if (this.whenOnExist) {
99
+ this.whenOnExist(item, value);
100
+ }
101
+ if (this.replaceItemWhenExits === true) {
102
+ super.delete(item);
103
+ flag = false;
104
+ } else {
105
+ tmp = item;
106
+ }
107
+ return false;
108
+ }
109
+ });
110
+ if (flag === false) {
111
+ super.add(value);
112
+ if (this.whenOnNotExist) {
113
+ this.whenOnNotExist(value);
114
+ }
115
+ }
116
+ return tmp;
117
+ }
118
+ /**
119
+ *
120
+ * 添加并返回添加成功的对象:可能是新加入集合的,也可能是原本存在的
121
+ * @param {T} values
122
+ * @returns {T}
123
+ */
124
+ addAll2(values: T[]): T[] {
125
+ const result: T[] = [];
126
+ for (const value of values) {
127
+ result.push(this.add2(value));
128
+ }
129
+ return result;
130
+ }
131
+ /**
132
+ * 用key找到匹配的第一个对象
133
+ * @param {*} value 这是对象的关键属性,而非对象
134
+ * @returns {(T | null)}
135
+ */
136
+ find(value: T[keyof T]): T | null {
137
+ for (const item of this) {
138
+ if (item[this.uniqueKey] === value) {
139
+ return item;
140
+ }
141
+ }
142
+ return null;
143
+ }
144
+ /**
145
+ * 用key找到匹配的所有对象
146
+ * @param {*} value 这是对象的关键属性,而非对象
147
+ * @returns {T[]}
148
+ */
149
+ findAll(value: T[keyof T]): T[] {
150
+ const res = new Array<T>();
151
+ this.forEach((item) => {
152
+ if (item[this.uniqueKey] === value) {
153
+ res.push(item);
154
+ }
155
+ });
156
+ return res;
157
+ }
158
+ /**
159
+ *
160
+ * 用函数回调找到匹配的第一个对象
161
+ * @param {(item: T) => boolean} fn
162
+ * @returns {T[]}
163
+ */
164
+ filter(fn: (item: T) => boolean): T | null {
165
+ for (const item of this) {
166
+ if (fn(item) === true) {
167
+ return item;
168
+ }
169
+ }
170
+ return null;
171
+ }
172
+ /**
173
+ *
174
+ * 用函数回调找到匹配的所有对象
175
+ * @param {(item: T) => boolean} fn
176
+ * @returns {T[]}
177
+ */
178
+ filterAll(fn: (item: T) => boolean): T[] {
179
+ const res = new Array<T>();
180
+ this.forEach((item) => {
181
+ if (fn(item) === true) {
182
+ res.push(item);
183
+ }
184
+ });
185
+ return res;
186
+ }
187
+ /**
188
+ *
189
+ * 是否存在key对应的对象
190
+ * @param {*} value 这是对象的关键属性,而非对象
191
+ * @returns {boolean}
192
+ */
193
+ override has(value: T[keyof T]): boolean {
194
+ for (const item of this) {
195
+ if (item[this.uniqueKey] === value) {
196
+ return true;
197
+ }
198
+ }
199
+ return false;
200
+ }
201
+ /**
202
+ * 转为数组
203
+ * @param param0
204
+ * @returns
205
+ */
206
+ toArray({ sort, each, filter, map }: {
207
+ sort?: (a: T, b: T) => number;
208
+ each?: (a: T) => void;
209
+ filter?: (a: T) => boolean;
210
+ map?: (a: T) => T;
211
+ } = {}): T[] {
212
+ let list = Array.from(this);
213
+ if (filter) { list = list.filter(filter); }
214
+ if (sort) { list.sort(sort); }
215
+ if (each) { list.forEach(each); }
216
+ if (map) { list = list.map(map); }
217
+ return list;
218
+ }
219
+ /**
220
+ * 转为JSON对象
221
+ * @param key
222
+ * @param value
223
+ * @param param2
224
+ * @returns
225
+ */
226
+ toJSON<L = T>(key: keyof T, value?: keyof T, { sort, each, filter, map }: {
227
+ sort?: (a: T, b: T) => number;
228
+ each?: (a: T) => void;
229
+ filter?: (a: T) => boolean;
230
+ map?: (a: T) => T;
231
+ } = {}): { [k: string]: L } {
232
+ return Object.fromEntries(this.toArray({ sort, each, filter, map }).map(i => [i[key], value ? i[value] : i]));
233
+ }
234
+ /**
235
+ *
236
+ * 删除key对应的对象
237
+ * @param {*} value 这是对象的关键属性,而非对象
238
+ * @returns {boolean}
239
+ */
240
+ override delete(value: T[keyof T]): boolean {
241
+ for (const item of this) {
242
+ if (item[this.uniqueKey] === value) {
243
+ super.delete(item);
244
+ return true;
245
+ }
246
+ }
247
+ return false;
248
+ }
249
+ /**
250
+ *
251
+ * 重置
252
+ * @param {keyof T} key
253
+ * @param {(oldData: T, newData: T) => void} [onExist]
254
+ * @param {boolean} [replaceWhenExits=false]
255
+ */
256
+ reset({ key, onExist, onNotExist, replaceWhenExits }: {
257
+ key?: keyof T;
258
+ onExist?: (oldData: T, newData: T) => void | null;
259
+ onNotExist?: (newData: T) => void | null;
260
+ replaceWhenExits?: boolean;
261
+ }): this {
262
+ if (onExist !== undefined) {
263
+ this.whenOnExist = onExist;
264
+ }
265
+ if (onNotExist !== undefined) {
266
+ this.whenOnNotExist = onNotExist;
267
+ }
268
+ if (key) {
269
+ this.uniqueKey = key;
270
+ }
271
+ if (replaceWhenExits !== undefined) {
272
+ this.replaceItemWhenExits = replaceWhenExits;
273
+ }
274
+ this.clear();
275
+ return this;
276
+ }
277
+ /**
278
+ *
279
+ * @param param0 转为JSON对象,value可能是数组
280
+ * @returns
281
+ */
282
+ toJSONArray({ sort, each, filter, map }: {
283
+ sort?: (a: T, b: T) => number;
284
+ each?: (a: T) => void;
285
+ filter?: (a: T) => boolean;
286
+ map?: (a: T) => T;
287
+ } = {}) {
288
+ const result: { [k: string]: T[keyof T][] } = {};
289
+ const list = this.toArray({ sort, each, filter, map });
290
+ for (const item of list) {
291
+ for (const key in item) {
292
+ if (!result[key]) {
293
+ result[key] = [];
294
+ }
295
+ result[key]!.push(item[key]);
296
+ }
297
+ }
298
+ return result;
299
+ }
300
+ /**
301
+ * 转为hot-table支持的数组
302
+ * @param param0
303
+ * @param keys
304
+ * @returns
305
+ */
306
+ toDataGrid({ sort, each, filter, map }: {
307
+ sort?: (a: T, b: T) => number;
308
+ each?: (a: T) => void;
309
+ filter?: (a: T) => boolean;
310
+ map?: (a: T) => T;
311
+ } = {}, ...keys: (keyof T)[]) {
312
+ if (this.size === 0) { return []; }
313
+ if (keys.length === 0) { keys = Object.keys(this.values().next().value) as any; }
314
+ const result: (T[keyof T] | keyof T)[][] = [keys];
315
+ const list = this.toArray({ sort, each, filter, map });
316
+ for (const item of list) {
317
+ const one: T[keyof T][] = [];
318
+ for (const key of keys) {
319
+ one.push(item[key]);
320
+ }
321
+ result.push(one);
322
+ }
323
+ return result;
324
+ }
325
+ /**
326
+ * 转为饼图支持的数组
327
+ * @param param0
328
+ * @param keys
329
+ * @returns
330
+ */
331
+ toPieGrid({ sort, each, filter, map }: {
332
+ sort?: (a: T, b: T) => number;
333
+ each?: (a: T) => void;
334
+ filter?: (a: T) => boolean;
335
+ map?: (a: T) => T;
336
+ } = {}, ...keys: (keyof T)[]): { [k: string]: { value: T[keyof T], name: T[keyof T] }[] } {
337
+ if (this.size === 0) { return {}; }
338
+ if (keys.length === 0) { keys = Object.keys(this.values().next().value) as any; }
339
+ const result: { [k: string]: { value: T[keyof T], name: T[keyof T] }[] } = {};
340
+ const list = this.toArray({ sort, each, filter, map });
341
+ for (const item of list) {
342
+ const name = item[this.uniqueKey];
343
+ for (const key in item) {
344
+ if (!result[key]) {
345
+ result[key] = [];
346
+ }
347
+ result[key]!.push({ name, value: item[key] });
348
+ }
349
+ }
350
+ return result;
351
+ }
352
+
353
+ set onExist(onExist: ((oldData: T, newData: T) => void) | undefined) {
354
+ this.whenOnExist = onExist;
355
+ }
356
+ set key(key: keyof T) {
357
+ this.uniqueKey = key;
358
+ }
359
+ set replaceWhenExits(replaceWhenExits: boolean) {
360
+ this.replaceItemWhenExits = replaceWhenExits;
361
+ }
362
+ }