@zwa73/utils 1.0.91 → 1.0.94

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/dist/UtilClass.js CHANGED
@@ -1,389 +1,110 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Piper = exports.Composer = exports.SHashMap = exports.SEntry = exports.SIterator = exports.SStream = exports.SList = void 0;
4
- class SList {
5
- _arr;
6
- constructor(obj) {
7
- if (typeof obj == "number")
8
- this._arr = new Array(obj);
9
- else if (Array.isArray(obj))
10
- this._arr = obj;
11
- else
12
- this._arr = [];
13
- }
14
- /**返回数组长度
15
- * @returns {number} - 长度
16
- */
17
- size() {
18
- return this._arr.length;
19
- }
20
- /**获取指定下标的元素
21
- * @param {number} index - 下标
22
- * @returns {T} - 目标元素
23
- */
24
- get(index) {
25
- return this._arr[index];
26
- }
27
- /**设置指定下标的元素
28
- * 返回自身 改变自身
29
- * @param {number} index - 下标
30
- * @param {T} value - 值
31
- * @returns {SList<T>} - 自身
32
- */
33
- set(index, value) {
34
- this._arr[index] = value;
35
- return this;
36
- }
37
- /**连接两个数组
38
- * @param {SList<T>} list - 目标数组
39
- * @returns {SList<T>} - 新数组
40
- */
41
- concat(list) {
42
- return new SList(this._arr.concat(list._arr));
43
- }
44
- /**在数组末尾添加一个元素
45
- * 返回自身 改变自身
46
- * @param {T} value - 值
47
- * @returns {SList<T>} - 自身
48
- */
49
- push(value) {
50
- this._arr.push(value);
51
- return this;
52
- }
53
- /**在数组末尾添加一个数组
54
- * 改变自身
55
- * @param {SList<T>} list - 目标数组
56
- * @returns {SList<T>} - 自身
57
- */
58
- pushList(list) {
59
- this._arr = this._arr.concat(list._arr);
60
- return this;
3
+ exports.Stream = exports.Piper = exports.Composer = void 0;
4
+ /**函数组合器 */
5
+ class Composer {
6
+ /**组合函数列表 */
7
+ funcs = [];
8
+ constructor() { }
9
+ ;
10
+ static push(...funcs) {
11
+ const newComposer = new Composer();
12
+ newComposer.funcs = [...funcs];
13
+ return newComposer;
61
14
  }
62
- /**截取从起始点到结束点之前的一段数组
63
- * @param {number} strat - 起始点
64
- * @param {number} end - 结束点
65
- * @returns {SList<T>} - 新数组
66
- */
67
- slice(strat, end) {
68
- return new SList(this._arr.slice(strat, end));
15
+ push(...funcs) {
16
+ const newComposer = new Composer();
17
+ newComposer.funcs = [...this.funcs, ...funcs];
18
+ return newComposer;
69
19
  }
70
- /**翻转数组
71
- * 改变自身
72
- * @returns {SList<T>} - 自身
73
- */
74
- reverse() {
75
- this._arr.reverse();
76
- return this;
20
+ /**组合函数 */
21
+ compose() {
22
+ return (arg) => this.funcs.reduceRight((value, func) => func(value), arg);
77
23
  }
78
- /**将SList转换为数组
79
- * @returns {Array<T>} - 数组
80
- */
81
- toArray() {
82
- return [].concat(this._arr);
24
+ /**直接调用 */
25
+ invoke(arg) {
26
+ return this.funcs.reduceRight((value, func) => func(value), arg);
83
27
  }
84
- /**将SList转换为SStream
85
- * @param {number} concurrent - 并发数
86
- * @returns {SStream<T>} - 流
87
- */
88
- toSStream(concurrent) {
89
- return new SStream(this._arr, concurrent);
28
+ }
29
+ exports.Composer = Composer;
30
+ /**函数管道器 */
31
+ class Piper {
32
+ /**管道函数列表 */
33
+ funcs = [];
34
+ constructor() { }
35
+ ;
36
+ static pipe(...funcs) {
37
+ const newPiper = new Piper();
38
+ newPiper.funcs = [...funcs];
39
+ return newPiper;
90
40
  }
91
- /**返回指定元素在数组中首次出现的位置
92
- * @param {T} value - 目标元素
93
- * @returns {number} - 下标
94
- */
95
- indexOf(value) {
96
- return this._arr.indexOf(value);
41
+ pipe(...funcs) {
42
+ const newPiper = new Piper();
43
+ newPiper.funcs = [...this.funcs, ...funcs];
44
+ return newPiper;
97
45
  }
98
- /**返回指定元素在数组中最后一次出现的位置
99
- * @param {T} value - 目标元素
100
- * @returns {number} - 下标
101
- */
102
- lastIndexOf(value) {
103
- return this._arr.lastIndexOf(value);
46
+ /**管道函数 */
47
+ pipeline() {
48
+ return (arg) => this.funcs.reduce((value, func) => func(value), arg);
104
49
  }
105
- /**判断数组中是否包含指定元素
106
- * @param {T} value - 目标元素
107
- * @returns {boolean} - 是否包含
108
- */
109
- contains(value) {
110
- return this._arr.includes(value);
50
+ /**直接调用 */
51
+ invoke(arg) {
52
+ return this.funcs.reduce((value, func) => func(value), arg);
111
53
  }
112
- /**获取迭代器
113
- * @returns {SIterator<T>} - 迭代器
114
- */
115
- iterator() {
116
- return new SIterator(this);
54
+ }
55
+ exports.Piper = Piper;
56
+ /**并行流 */
57
+ class Stream {
58
+ /**并发数*/
59
+ _concurrent;
60
+ /**原始列表*/
61
+ _list;
62
+ /**加工函数列表*/
63
+ _operation = [];
64
+ constructor(list, concurrent = 1) {
65
+ this._list = list;
66
+ this._concurrent = concurrent;
117
67
  }
118
68
  /**平分数组
119
- * @param {number} count - 份数
120
- * @param {"average" | "chunk"} mode - 模式 average:轮询平均分 chunk:切块均分
121
- * @returns {SList<SList<T>>} - 新数组
69
+ * @param count - 份数
70
+ * @param mode - 模式 average:轮询平均分 chunk:切块均分
71
+ * @returns 新数组
122
72
  */
123
73
  divide(count, mode = "average") {
124
74
  if (count <= 0)
125
- return new SList();
75
+ return [];
126
76
  if (count == 1)
127
- return new SList([this.clone()]);
128
- let result = new SList();
77
+ return [[...this._list]];
78
+ const result = [];
129
79
  switch (mode) {
130
80
  //轮询平均分
131
- default:
132
81
  case "average":
133
82
  for (let i = 0; i < count; i++) {
134
- let clist = new SList();
135
- let size = this.size();
83
+ const clist = [];
84
+ const size = this._list.length;
136
85
  for (let j = i; j < size; j += count)
137
- clist.push(this.get(j));
86
+ clist.push(this._list[j]);
138
87
  result.push(clist);
139
88
  }
140
89
  break;
141
90
  //切块均分
142
91
  case "chunk":
143
- let chunkSize = Math.ceil(this.size() / count);
92
+ const size = this._list.length;
93
+ const chunkSize = Math.ceil(size / count);
144
94
  for (let i = 0; i < count; i++) {
145
- let start = i * chunkSize;
95
+ const start = i * chunkSize;
146
96
  let end = (i + 1) * chunkSize;
147
- if (end > this.size())
148
- end = this.size();
149
- result.push(this.slice(start, end));
97
+ if (end > size)
98
+ end = size;
99
+ result.push(this._list.slice(start, end));
150
100
  }
151
101
  break;
152
102
  }
153
103
  return result;
154
104
  }
155
- /**从index开始删除count个元素,返回删除的元素数组
156
- * 改变自身
157
- * @param {number} index - 起始点
158
- * @param {number} count - 数量
159
- * @returns {SList<T>} - 删除的元素数组
160
- */
161
- removeRange(index, count) {
162
- let narr = this._arr.splice(index, count);
163
- return new SList(narr);
164
- }
165
- /**删除对应下标下的元素,返回删除的元素
166
- * 改变自身
167
- * @param {number} i - 下标
168
- * @returns {T|null} - 删除的元素
169
- */
170
- remove(i) {
171
- if (i >= 0 && i < this.size())
172
- return this._arr.splice(i, 1)[0];
173
- return null;
174
- }
175
- /**删除第一个匹配的项目,返回自身
176
- * 改变自身
177
- * @param {T} obj - 需删除目标
178
- * @returns {SList<T>} - 自身
179
- */
180
- removeMember(obj) {
181
- let index = this.indexOf(obj);
182
- if (index > -1)
183
- this.remove(index);
184
- return this;
185
- }
186
- /**删除所有匹配的项目
187
- * 改变自身
188
- * @param {T} obj - 需删除目标
189
- * @returns {SList<T>} - 自身
190
- */
191
- removeAllMember(obj) {
192
- while (this.contains(obj))
193
- this.removeMember(obj);
194
- return this;
195
- }
196
- /**在这个下标对应的元素前添加一个元素
197
- * 改变自身
198
- * @param {number} index - 下标
199
- * @param {T} obj - 添加对象
200
- * @returns {SList<T>} - 自身
201
- */
202
- insert(index, obj) {
203
- this._arr.splice(index, 0, obj);
204
- return this;
205
- }
206
- /**在这个下标对应的元素前添加一组元素
207
- * 改变自身
208
- * @param {number} index - 下标
209
- * @param {SList<T>} obj - 添加对象
210
- * @returns {SList<T>} - 自身
211
- */
212
- insertRange(index, obj) {
213
- this._arr.splice(index, 0, ...obj._arr);
214
- return this;
215
- }
216
- /**删除并返回最后一个元素
217
- * 改变自身
218
- * @returns {T | null} - 最后一个元素
219
- */
220
- pop() {
221
- let val = this._arr.pop();
222
- return val == undefined ? null : val;
223
- }
224
- /**删除并返回第一个元素
225
- * 改变自身
226
- * @returns {T | null} - 第一个元素
227
- */
228
- shift() {
229
- let val = this._arr.shift();
230
- return val == undefined ? null : val;
231
- }
232
- /**克隆数组
233
- * @returns {SList<T>} - 新数组
234
- */
235
- clone() {
236
- return new SList([].concat(this._arr));
237
- }
238
- /**判断数组是否为空
239
- * @returns {boolean} - 是否为空
240
- */
241
- isEmpty() {
242
- return this.size() == 0;
243
- }
244
- /**删除数组中的重复元素
245
- * 改变自身
246
- * @returns {SList<T>} - 自身
247
- */
248
- removeDuplicates() {
249
- this._arr = Array.from(new Set(this._arr));
250
- return this;
251
- }
252
- /**交集
253
- * @param {SList<T>[]} lists - 数组列表
254
- * @returns {SList<T>} - 新数组
255
- */
256
- intersection(...lists) {
257
- let nlist = this.clone().removeDuplicates();
258
- for (let list of lists)
259
- nlist = list.filt((val) => nlist.contains(val));
260
- return nlist;
261
- }
262
- /**并集
263
- * @param {SList<T>[]} lists - 数组列表
264
- * @returns {SList<T>} - 新数组
265
- */
266
- union(...lists) {
267
- let nlist = this.clone().removeDuplicates();
268
- for (let list of lists) {
269
- list.each((val) => {
270
- if (!nlist.contains(val))
271
- nlist.push(val);
272
- });
273
- }
274
- return nlist;
275
- }
276
- /**返回符合条件的成员组成的新数组
277
- * @param {FiltCallback<T>} func - 条件函数
278
- * @returns {SList<T>} - 新数组
279
- */
280
- filt(func) {
281
- let nlist = new SList(this.size());
282
- let length = 0;
283
- let it = this.iterator();
284
- while (it.hasNext()) {
285
- let tmpObj = it.next();
286
- if (func(tmpObj, it.currIndex(), this)) {
287
- nlist.set(length, tmpObj);
288
- length++;
289
- }
290
- }
291
- return nlist.slice(0, length);
292
- }
293
- /**遍历数组的每一个元素
294
- * @param {EachCallback<T>} func - 遍历函数 (value: T, index: number, list: SList<T>): void
295
- * @returns {SList<T>} - 自身
296
- */
297
- each(func) {
298
- let it = this.iterator();
299
- while (it.hasNext())
300
- func(it.next(), it.currIndex(), this);
301
- return this;
302
- }
303
- /**对数组的每一个元素进行加工,返回加工完成的成员组成的新数组
304
- * @param {MapCallback<T,O>} func - 加工函数 (value: T, index: number, list: SList<T>): O
305
- * @returns {SList<O>} - 新数组
306
- */
307
- map(func) {
308
- let nlist = new SList(this.size());
309
- let it = this.iterator();
310
- while (it.hasNext())
311
- nlist.set(it.nextIndex(), func(it.next(), it.currIndex(), this));
312
- return nlist;
313
- }
314
- /**对数组进行排序
315
- * 如函数返回值大于 0 则将 x 排在 y 后面,小于 0 则将 x 排在 y 前面
316
- * 改变自身
317
- * @param {SortCallback<T>} func - 排序函数 (a: T, b: T): number
318
- * @returns {SList<T>} - 自身
319
- */
320
- sort(func) {
321
- this._arr.sort(func);
322
- return this;
323
- }
324
- /**对数组进行统计 aggregate
325
- * 遍历数组,并将每次遍历的结果与下一次遍历的元素一起传入函数,最后返回最后一次遍历的结果
326
- * @param {O} init - 初始值
327
- * @param {AggrCallback<T,O>} func - 统计函数 (value: T, accumulator: U, index: number, list: SList<T>): U
328
- * @returns {O} - 统计结果
329
- */
330
- aggr(init, func) {
331
- let it = this.iterator();
332
- let tmpObj = init;
333
- while (it.hasNext())
334
- tmpObj = func(tmpObj, it.next(), it.currIndex(), this);
335
- return tmpObj;
336
- }
337
- /**对数组进行缩减
338
- * 遍历数组,并将每次遍历的结果与下一次遍历的元素一起传入函数,最后返回最后一次遍历的结果
339
- * @param {ReduceCallback<T>} func - 缩减函数 (previousValue: T, currentValue: T, currentIndex: number, array: SList<T>):T
340
- * @param {T} init - 初始值
341
- * @returns {T} - 缩减结果
342
- */
343
- reduce(func, init) {
344
- if (this.size() === 0)
345
- return null;
346
- let it = this.iterator();
347
- let tmpObj = init === undefined ? it.next() : init;
348
- while (it.hasNext())
349
- tmpObj = func(tmpObj, it.next(), it.currIndex(), this);
350
- return tmpObj;
351
- }
352
- //重载TypeScript操作符
353
- [Symbol.iterator]() {
354
- let it = this.iterator();
355
- return {
356
- next() {
357
- if (it.hasNext()) {
358
- return { value: it.next(), done: false };
359
- }
360
- else {
361
- return { value: null, done: true };
362
- }
363
- },
364
- };
365
- }
366
- }
367
- exports.SList = SList;
368
- class SStream {
369
- /**并发数*/
370
- _concurrent;
371
- /**原始列表*/
372
- _slist;
373
- /**加工函数列表*/
374
- _operation = [];
375
- constructor(slist, concurrent = 1) {
376
- if (slist instanceof SList)
377
- this._slist = slist;
378
- else if (Array.isArray(slist))
379
- this._slist = new SList(slist);
380
- else
381
- this._slist = new SList();
382
- this._concurrent = concurrent;
383
- }
384
105
  /**映射加工
385
- * @param {SStreamOperation<T,U>} operation - 加工函数
386
- * @returns {SStream<U>} - 新流
106
+ * @param operation - 加工函数
107
+ * @returns 新流
387
108
  */
388
109
  map(operation) {
389
110
  this._operation.push(operation);
@@ -391,11 +112,11 @@ class SStream {
391
112
  }
392
113
  /**遍历
393
114
  * 返回自身
394
- * @param {SStreamOperation<T,void>} operation - 遍历函数
395
- * @returns {SStream<T>} - 自身
115
+ * @param operation - 遍历函数
116
+ * @returns 自身
396
117
  */
397
118
  each(operation) {
398
- let opera = async (item) => {
119
+ const opera = async (item) => {
399
120
  operation(item);
400
121
  return item;
401
122
  };
@@ -404,491 +125,51 @@ class SStream {
404
125
  }
405
126
  //终结操作
406
127
  /**应用加工
407
- * @returns {SStream<T>} - 自身
128
+ * @returns 自身
408
129
  */
409
- async appendOperations() {
130
+ async append() {
410
131
  if (this._operation.length == 0)
411
132
  return this;
412
- let nlist = new SList();
413
- let promiseList = [];
133
+ const promiseList = [];
414
134
  //均分处理
415
- let sliceList = this._slist.divide(this._concurrent);
135
+ const sliceList = this.divide(this._concurrent);
416
136
  for (let i = 0; i < this._concurrent; i++) {
417
- let subList = sliceList.get(i);
137
+ const subList = sliceList[i];
418
138
  if (!subList)
419
139
  continue;
420
- promiseList.push(new Promise(async () => {
421
- let result = new SList();
140
+ promiseList.push(new Promise(async (reslove) => {
141
+ const result = [];
422
142
  for (let item of subList) {
423
143
  if (!item)
424
144
  continue;
425
- for (let operation of this._operation)
145
+ for (const operation of this._operation)
426
146
  item = await operation(item);
427
147
  result.push(item);
428
148
  }
429
- nlist.set(i, result);
149
+ reslove(result);
430
150
  }));
431
151
  }
432
- await Promise.all(promiseList);
152
+ const nlist = await Promise.all(promiseList);
433
153
  //拼接结果 轮询均分
434
- let result = new SList(this._slist.size());
154
+ const result = new Array(this._list.length).fill(undefined);
435
155
  for (let i = 0; i < this._concurrent; i++) {
436
- let subList = nlist.get(i);
156
+ const subList = nlist[i];
437
157
  if (!subList)
438
158
  continue;
439
- let subSize = subList.size();
159
+ const subSize = subList.length;
440
160
  for (let j = 0; j < subSize; j++)
441
- result.set(i + j * this._concurrent, subList.get(j));
161
+ result[i + j * this._concurrent] = subList[j];
442
162
  }
443
- this._slist = result;
163
+ this._list = result;
444
164
  this._operation = [];
445
165
  return this;
446
166
  }
447
- /**转换为SList
448
- * @returns {SList<T>} - 数组
449
- */
450
- async toSList() {
451
- await this.appendOperations();
452
- return this._slist;
453
- }
454
167
  /**转换为数组
455
- * @returns {Array<T>} - 数组
168
+ * @returns 数组
456
169
  */
457
170
  async toArray() {
458
- await this.appendOperations();
459
- return this._slist.toArray();
460
- }
461
- }
462
- exports.SStream = SStream;
463
- class SIterator {
464
- _index = -1;
465
- _list;
466
- constructor(list) {
467
- this._list = list;
468
- }
469
- /**判断还有没有下一个元素
470
- * @returns {boolean} - 是否有下一个元素
471
- */
472
- hasNext() {
473
- return this._index < this.size() - 1 && this._index >= -1;
474
- }
475
- /**判断当前下标有无元素
476
- * @returns {boolean} - 是否有当前元素
477
- */
478
- hasCurr() {
479
- return this._index < this.size() && this._index >= 0;
480
- }
481
- /**判断还有没有上一个元素
482
- * @returns {boolean} - 是否有上一个元素
483
- */
484
- hasPre() {
485
- return this._index < this.size() + 1 && this._index >= 1;
486
- }
487
- /**返回下一个下标指向的数组内成员,然后下标自加1
488
- * @returns {T} - 下一个成员
489
- */
490
- next() {
491
- if (this.hasNext())
492
- return this._list.get(++this._index);
493
- else
494
- throw "一个 SIterator 迭代器的 next 函数出错,可能是在下标到底时还在尝试迭代";
495
- }
496
- /**返回上一个下标指向的数组内成员,然后下标自减1
497
- * @returns {T} - 上一个成员
498
- */
499
- pre() {
500
- if (this.hasPre())
501
- return this._list.get(--this._index);
502
- else
503
- throw "一个 SIterator 迭代器的 pre 函数出错,可能是在下标小于1时还在尝试迭代";
504
- }
505
- /**返回当前下标指向的数组内成员
506
- * @returns {T} - 当前成员
507
- */
508
- curr() {
509
- if (this.hasCurr())
510
- return this._list.get(this._index);
511
- else
512
- throw "一个 SIterator 迭代器的 curr 函数出错,可能是当前下标不包含元素/当前下标已越界";
513
- }
514
- /**返回遍历长度
515
- * @returns {number} - 遍历长度
516
- */
517
- size() {
518
- return this._list.size();
519
- }
520
- /**获取下个下标
521
- * @returns {number} - 下个下标
522
- */
523
- nextIndex() {
524
- return this._index + 1;
525
- }
526
- /**获取当前下标
527
- * @return {number} - 当前下标
528
- */
529
- currIndex() {
530
- return this._index;
531
- }
532
- /**获取上个下标
533
- * @returns {number} - 上个下标
534
- */
535
- preIndex() {
536
- return this._index - 1;
537
- }
538
- /**判断是否是最后一个
539
- * @returns {boolean} - 是否是最后一个
540
- */
541
- isLast() {
542
- return this._index >= this._list.size() - 1;
543
- }
544
- /**设置数组在迭代器当前下标中的内容
545
- * 返回自身
546
- * @param {T} val 要设置的内容
547
- * @returns {void}
548
- */
549
- set(val) {
550
- this._list.set(this._index, val);
551
- return this;
552
- }
553
- /**删除数组在当前下标的内容,然后将下标移至上一位,改变数组长度
554
- * @returns {T} - 被删除的内容
555
- */
556
- remove() {
557
- let tmp = this._list.get(this._index);
558
- this._list.remove(this._index);
559
- this._index--;
560
- return tmp;
561
- }
562
- /**在当前下标前插入元素,然后将下标移至原元素,即下一位,改变数组长度
563
- * 返回自身
564
- * @param {T} obj - 要插入的元素
565
- * @returns {SIterator<T>} - 自身
566
- */
567
- addPre(obj) {
568
- this._list.insert(this._index, obj);
569
- this._index++;
570
- return this;
571
- }
572
- /**在当前下标后插入元素,然后将下标移至新元素,即下一位,改变数组长度
573
- * 返回自身
574
- * @param {T} obj - 要插入的元素
575
- * @returns {SIterator<T>} - 自身
576
- */
577
- addNext(obj) {
578
- this._list.insert(this._index + 1, obj);
579
- this._index++;
580
- return this;
171
+ await this.append();
172
+ return this._list;
581
173
  }
582
174
  }
583
- exports.SIterator = SIterator;
584
- class SEntry {
585
- _key;
586
- _value;
587
- constructor(key, value) {
588
- this._key = key;
589
- this._value = value;
590
- }
591
- getKey() {
592
- return this._key;
593
- }
594
- getValue() {
595
- return this._value;
596
- }
597
- //重载TypeScript操作符
598
- get key() {
599
- return this.getKey();
600
- }
601
- get value() {
602
- return this._value;
603
- }
604
- }
605
- exports.SEntry = SEntry;
606
- class SHashMap {
607
- _map = new Map();
608
- /**构造函数
609
- * @param {Map<K, V>} [map] - 一个键值对集合
610
- */
611
- constructor(map) {
612
- if (map == null)
613
- return;
614
- let keys = map.keys();
615
- for (let key of keys) {
616
- let value = map.get(key);
617
- if (value != null)
618
- this.put(key, value);
619
- }
620
- }
621
- /**添加一个键值对
622
- * 返回自身
623
- * @param {SEntry<K, V>} entry - 键值对
624
- * @returns {SHashMap<K, V>} - 自身
625
- */
626
- putEntry(entry) {
627
- this._map.set(entry.getKey(), entry.getValue());
628
- return this;
629
- }
630
- /**获取指定键的值
631
- * @param {K} key - 键
632
- * @returns {V | undefined} - 值
633
- */
634
- get(key) {
635
- return this._map.get(key);
636
- }
637
- /**添加一个键值对
638
- * 返回自身
639
- * @param {K} key - 键
640
- * @param {V} value - 值
641
- * @returns {SHashMap<K, V>} - 自身
642
- */
643
- put(key, value) {
644
- this._map.set(key, value);
645
- return this;
646
- }
647
- /**判断是否存在指定键
648
- * @param {K} key - 键
649
- * @returns {boolean} - 是否存在
650
- */
651
- has(key) {
652
- return this._map.has(key);
653
- }
654
- /**获取所有键值对
655
- * @returns {SList<SEntry<K, V>>} - 键值对列表
656
- */
657
- entrys() {
658
- let list = new SList();
659
- for (const [key, value] of this._map)
660
- list.push(new SEntry(key, value));
661
- return list;
662
- }
663
- /**获取所有键
664
- * @returns {SList<K>} - 键列表
665
- */
666
- keys() {
667
- let list = new SList();
668
- let it = this._map.keys();
669
- for (let key of it)
670
- list.push(key);
671
- return list;
672
- }
673
- /**获取所有值
674
- * @returns {SList<V>} - 值列表
675
- */
676
- values() {
677
- let list = new SList();
678
- let it = this._map.values();
679
- for (let val of it)
680
- list.push(val);
681
- return list;
682
- }
683
- /**转换为entry数组,并获取迭代器
684
- * @returns {SIterator<SEntry<K, V>>} - 迭代器
685
- */
686
- iterator() {
687
- return this.entrys().iterator();
688
- }
689
- /**删除指定键的键值对
690
- * @param {K} key - 键
691
- * @returns {V | undefined} - 被删除的值
692
- */
693
- remove(key) {
694
- let out = this._map.get(key);
695
- this._map.delete(key);
696
- return out;
697
- }
698
- /**清空哈希表
699
- * @returns {SHashMap<K, V>} - 自身
700
- */
701
- clear() {
702
- this._map.clear();
703
- return this;
704
- }
705
- /**判断哈希表是否为空
706
- * @returns {boolean} - 是否为空
707
- */
708
- isEmpty() {
709
- return this.isEmpty();
710
- }
711
- /**判断是否存在指定值
712
- * @param {V} val - 值
713
- * @returns {boolean} - 是否存在
714
- */
715
- containsValue(val) {
716
- return this.values().contains(val);
717
- }
718
- /**判断是否存在指定键
719
- * @param {K} key - 键
720
- * @returns {boolean} - 是否存在
721
- */
722
- containsKey(key) {
723
- return this._map.has(key);
724
- }
725
- /**加载指定键的值,若不存在则添加默认值
726
- * 返回键值
727
- * @param {K} key - 键
728
- * @param {V} def - 默认值
729
- * @returns {V} - 值
730
- */
731
- load(key, def) {
732
- if (this.containsKey(key))
733
- return this.get(key);
734
- this.put(key, def);
735
- return def;
736
- }
737
- /**合并另一个哈希表,可选择是否覆盖已有键值对
738
- * @param {SHashMap<K, V>} nmap - 另一个哈希表
739
- * @param {boolean} [isCover=true] - 是否覆盖已有键值对,默认为 true
740
- * @returns {SHashMap<K, V>} - 自身
741
- */
742
- merge(nmap, isCover = true) {
743
- var it = nmap.iterator();
744
- while (it.hasNext()) {
745
- if (isCover)
746
- this.putEntry(it.next());
747
- else {
748
- var entry = it.next();
749
- if (!this.containsKey(entry.getKey()))
750
- this.putEntry(entry);
751
- }
752
- }
753
- return this;
754
- }
755
- /**获取哈希表大小(键的数量)
756
- * @returns {number} - 大小
757
- */
758
- size() {
759
- return this._map.size;
760
- }
761
- /**对哈希表的每一个键值对进行加工,返回加工完成的键值对组成的新哈希表
762
- * @param {MapCallback<SEntry<K, V>, SEntry<OK, OV>>} func - 加工函数
763
- * @returns {SHashMap<OK, OV>} - 新哈希表
764
- */
765
- map(func) {
766
- let nmap = new SHashMap();
767
- this.entrys()
768
- .map(func)
769
- .each((val) => nmap.putEntry(val));
770
- return nmap;
771
- }
772
- /**返回符合条件的键值对组成的新哈希表
773
- * @param {FiltCallback<SEntry<K, V>>): boolean} func - 条件函数
774
- * @returns {SHashMap<K, V>} - 新哈希表
775
- */
776
- filt(func) {
777
- let nmap = new SHashMap();
778
- this.entrys()
779
- .filt(func)
780
- .each((val) => nmap.putEntry(val));
781
- return nmap;
782
- }
783
- /**遍历哈希表的每一个键值对
784
- * @param {EachCallback<SEntry<K, V>>} func - 遍历函数
785
- * @returns {SHashMap<K, V>} - 自身
786
- */
787
- each(func) {
788
- this.entrys().each(func);
789
- return this;
790
- }
791
- /**对哈希表进行统计
792
- * @param {O} init - 初始值
793
- * @param {AggrCallback<SEntry<K, V>,O>} func - 统计函数
794
- * @returns {O} - 统计结果
795
- */
796
- aggr(init, func) {
797
- return this.entrys().aggr(init, func);
798
- }
799
- /**对哈希表进行缩减
800
- * @param {AggrCallback<SEntry<K, V>,V>} func - 缩减函数
801
- * @param {V} init - 初始值
802
- * @returns {V} - 缩减结果
803
- */
804
- reduce(func, init) {
805
- if (this.isEmpty()) {
806
- if (init === undefined)
807
- return null;
808
- return init;
809
- }
810
- let entries = this.entrys();
811
- let result;
812
- if (init === undefined) {
813
- result = entries.get(0).getValue();
814
- entries.shift();
815
- }
816
- else
817
- result = init;
818
- entries.each((val, index, list) => {
819
- result = func(result, val, index, list);
820
- });
821
- return result;
822
- }
823
- //重载TypeScript操作符
824
- [Symbol.iterator]() {
825
- let it = this.iterator();
826
- return {
827
- next() {
828
- if (it.hasNext()) {
829
- return { value: it.next(), done: false };
830
- }
831
- else {
832
- return { value: null, done: true };
833
- }
834
- },
835
- };
836
- }
837
- }
838
- exports.SHashMap = SHashMap;
839
- class SKVC {
840
- stringMap = new SHashMap();
841
- constructor() { }
842
- }
843
- /**函数组合器 */
844
- class Composer {
845
- /**组合函数列表 */
846
- funcs = [];
847
- constructor() { }
848
- ;
849
- static push(...funcs) {
850
- const newComposer = new Composer();
851
- newComposer.funcs = [...funcs];
852
- return newComposer;
853
- }
854
- push(...funcs) {
855
- const newComposer = new Composer();
856
- newComposer.funcs = [...this.funcs, ...funcs];
857
- return newComposer;
858
- }
859
- /**组合函数 */
860
- compose() {
861
- return (arg) => this.funcs.reduceRight((value, func) => func(value), arg);
862
- }
863
- /**直接调用 */
864
- invoke(arg) {
865
- return this.funcs.reduceRight((value, func) => func(value), arg);
866
- }
867
- }
868
- exports.Composer = Composer;
869
- /**函数管道器 */
870
- class Piper {
871
- /**管道函数列表 */
872
- funcs = [];
873
- constructor() { }
874
- ;
875
- static pipe(...funcs) {
876
- const newPiper = new Piper();
877
- newPiper.funcs = [...funcs];
878
- return newPiper;
879
- }
880
- pipe(...funcs) {
881
- const newPiper = new Piper();
882
- newPiper.funcs = [...this.funcs, ...funcs];
883
- return newPiper;
884
- }
885
- /**管道函数 */
886
- pipeline() {
887
- return (arg) => this.funcs.reduce((value, func) => func(value), arg);
888
- }
889
- /**直接调用 */
890
- invoke(arg) {
891
- return this.funcs.reduce((value, func) => func(value), arg);
892
- }
893
- }
894
- exports.Piper = Piper;
175
+ exports.Stream = Stream;