extra-iterator 0.6.0 → 0.7.0
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/index.d.ts +19 -1
- package/dist/index.js +36 -12
- package/dist/index.test.js +19 -0
- package/package.json +1 -1
- package/dist/chain-of-responsibility.d.ts +0 -0
- package/dist/chain-of-responsibility.js +0 -1
package/dist/index.d.ts
CHANGED
|
@@ -283,6 +283,9 @@ export declare class ExtraIterator<T> extends Iterator<T, any, any> {
|
|
|
283
283
|
* The returned object is composed of keys generated by calling the provided callback function on each element of
|
|
284
284
|
* this iterator, and the value for each key is an array containing all the elements that were assigned to that key.
|
|
285
285
|
*
|
|
286
|
+
* This method is similar to {@link toMap}, but the returned object is a plain, null-prototype, object, instead of a
|
|
287
|
+
* `Map`.
|
|
288
|
+
*
|
|
286
289
|
* @example
|
|
287
290
|
*
|
|
288
291
|
* ExtraIterator.from([1, 2, 3, 4, 5])
|
|
@@ -291,6 +294,20 @@ export declare class ExtraIterator<T> extends Iterator<T, any, any> {
|
|
|
291
294
|
* // returns { even: [2, 4], odd: [1, 3, 5] }
|
|
292
295
|
*/
|
|
293
296
|
groupBy<K extends string | symbol>(callbackfn: (value: T, index: number) => K): Record<K, T[]>;
|
|
297
|
+
/**
|
|
298
|
+
* Groups elements into separate arrays and returns a Map containing each group.
|
|
299
|
+
*
|
|
300
|
+
* The returned Map is composed of keys generated by calling the provided callback function on each element of this
|
|
301
|
+
* iterator, and the value for each key is an array containing all the elements to which the callback function
|
|
302
|
+
* returned that key.
|
|
303
|
+
*
|
|
304
|
+
* This method is similart to {@link groupBy}, but the returned object is a Map instead of a plain object.
|
|
305
|
+
*/
|
|
306
|
+
toMap<K extends string | symbol>(callbackfn: (value: T, index: number) => K): Map<K, T[]>;
|
|
307
|
+
/**
|
|
308
|
+
* Creates a set containing all the values yielded by this iterator.
|
|
309
|
+
*/
|
|
310
|
+
toSet(): Set<T>;
|
|
294
311
|
toChainOfResponsibilityFunction<ResultType = void | Promise<void>, ParamsType extends any[] = []>(invokeHandler: (handler: T, next: (...args: ParamsType) => ResultType, ...args: ParamsType) => ResultType): (...args: ParamsType) => ResultType;
|
|
295
312
|
/**
|
|
296
313
|
* Consumes the iterator and returns a value determined by calling the provided function using the iterator as
|
|
@@ -304,9 +321,10 @@ export declare class ExtraIterator<T> extends Iterator<T, any, any> {
|
|
|
304
321
|
* // returns { _a: 2, _b: 4 }
|
|
305
322
|
*/
|
|
306
323
|
collect<U>(collectfn: ((iter: Iterable<T>) => U)): U;
|
|
307
|
-
toSortedBy(...keys: (keyof T)[]): T[];
|
|
308
324
|
/**
|
|
309
325
|
* Consumes the iterator and returns the number of elements it contained.
|
|
326
|
+
*
|
|
327
|
+
* @example ExtraIterator.from([1, 2, 3, 4]).count() // returns 4
|
|
310
328
|
*/
|
|
311
329
|
count(): number;
|
|
312
330
|
/**
|
package/dist/index.js
CHANGED
|
@@ -504,6 +504,9 @@ export class ExtraIterator extends Iterator {
|
|
|
504
504
|
* The returned object is composed of keys generated by calling the provided callback function on each element of
|
|
505
505
|
* this iterator, and the value for each key is an array containing all the elements that were assigned to that key.
|
|
506
506
|
*
|
|
507
|
+
* This method is similar to {@link toMap}, but the returned object is a plain, null-prototype, object, instead of a
|
|
508
|
+
* `Map`.
|
|
509
|
+
*
|
|
507
510
|
* @example
|
|
508
511
|
*
|
|
509
512
|
* ExtraIterator.from([1, 2, 3, 4, 5])
|
|
@@ -522,6 +525,37 @@ export class ExtraIterator extends Iterator {
|
|
|
522
525
|
}
|
|
523
526
|
return result;
|
|
524
527
|
}
|
|
528
|
+
/**
|
|
529
|
+
* Groups elements into separate arrays and returns a Map containing each group.
|
|
530
|
+
*
|
|
531
|
+
* The returned Map is composed of keys generated by calling the provided callback function on each element of this
|
|
532
|
+
* iterator, and the value for each key is an array containing all the elements to which the callback function
|
|
533
|
+
* returned that key.
|
|
534
|
+
*
|
|
535
|
+
* This method is similart to {@link groupBy}, but the returned object is a Map instead of a plain object.
|
|
536
|
+
*/
|
|
537
|
+
toMap(callbackfn) {
|
|
538
|
+
const result = new Map();
|
|
539
|
+
for (let index = 0, next; next = this.next(), !next.done; index++) {
|
|
540
|
+
const key = callbackfn(next.value, index);
|
|
541
|
+
const array = result.get(key);
|
|
542
|
+
if (!array) {
|
|
543
|
+
result.set(key, [next.value]);
|
|
544
|
+
}
|
|
545
|
+
else {
|
|
546
|
+
array.push(next.value);
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
return result;
|
|
550
|
+
}
|
|
551
|
+
/**
|
|
552
|
+
* Creates a set containing all the values yielded by this iterator.
|
|
553
|
+
*/
|
|
554
|
+
toSet() {
|
|
555
|
+
const result = new Set();
|
|
556
|
+
this.forEach(value => result.add(value));
|
|
557
|
+
return result;
|
|
558
|
+
}
|
|
525
559
|
toChainOfResponsibilityFunction(invokeHandler) {
|
|
526
560
|
const handlers = this.toArray();
|
|
527
561
|
return (...initialArgs) => {
|
|
@@ -551,20 +585,10 @@ export class ExtraIterator extends Iterator {
|
|
|
551
585
|
collect(collectfn) {
|
|
552
586
|
return collectfn(this);
|
|
553
587
|
}
|
|
554
|
-
toSortedBy(...keys) {
|
|
555
|
-
return this.toArray()
|
|
556
|
-
.sort((a, b) => {
|
|
557
|
-
for (const key of keys) {
|
|
558
|
-
if (a[key] < b[key])
|
|
559
|
-
return -1;
|
|
560
|
-
if (a[key] > b[key])
|
|
561
|
-
return 1;
|
|
562
|
-
}
|
|
563
|
-
return 0;
|
|
564
|
-
});
|
|
565
|
-
}
|
|
566
588
|
/**
|
|
567
589
|
* Consumes the iterator and returns the number of elements it contained.
|
|
590
|
+
*
|
|
591
|
+
* @example ExtraIterator.from([1, 2, 3, 4]).count() // returns 4
|
|
568
592
|
*/
|
|
569
593
|
count() {
|
|
570
594
|
let count = 0;
|
package/dist/index.test.js
CHANGED
|
@@ -126,6 +126,25 @@ describe('ExtraIterator', () => {
|
|
|
126
126
|
b: ['banana'],
|
|
127
127
|
});
|
|
128
128
|
});
|
|
129
|
+
it('should convert to a Map', () => {
|
|
130
|
+
const iterator = ExtraIterator.from([{ key: 'a', value: 1 }, { key: 'b', value: 2 }, { key: 'a', value: 3 }]);
|
|
131
|
+
const map = iterator.toMap(item => item.key);
|
|
132
|
+
expect(map).toBeInstanceOf(Map);
|
|
133
|
+
expect(map).toEqual(new Map([
|
|
134
|
+
['a', [{ key: 'a', value: 1 }, { key: 'a', value: 3 }]],
|
|
135
|
+
['b', [{ key: 'b', value: 2 }]],
|
|
136
|
+
]));
|
|
137
|
+
});
|
|
138
|
+
it('should convert to a Set', () => {
|
|
139
|
+
const iterator = ExtraIterator.from([3, 1, 3, 2, 2, 1, 3]);
|
|
140
|
+
const set = iterator.toSet();
|
|
141
|
+
expect(set).toBeInstanceOf(Set);
|
|
142
|
+
expect(set.size).toBe(3);
|
|
143
|
+
expect(set.has(1)).toBe(true);
|
|
144
|
+
expect(set.has(2)).toBe(true);
|
|
145
|
+
expect(set.has(3)).toBe(true);
|
|
146
|
+
expect(set).toEqual(new Set([3, 1, 2]));
|
|
147
|
+
});
|
|
129
148
|
it('should collect values using a custom collector', () => {
|
|
130
149
|
const iterator = ExtraIterator.from([1, 2, 3]);
|
|
131
150
|
const sum = iterator.collect(iter => Array.from(iter).reduce((a, b) => a + b, 0));
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"description": "An extension of the Iterator class with additional utility helper functions.",
|
|
4
4
|
"author": "Leonardo Raele <leonardoraele@gmail.com>",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"version": "0.
|
|
6
|
+
"version": "0.7.0",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"exports": {
|
|
9
9
|
".": "./dist/index.js",
|
|
File without changes
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";
|