extra-iterator 0.6.0 → 0.8.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 +25 -1
- package/dist/index.js +43 -11
- package/dist/index.test.js +33 -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,16 @@ 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
|
-
|
|
324
|
+
/**
|
|
325
|
+
* Sums the numeric value of all elements in the iterator and returns the total.
|
|
326
|
+
*
|
|
327
|
+
* @example ExtraIterator.from([5, 8, 13]).sum() // returns 26
|
|
328
|
+
*/
|
|
329
|
+
sum(): number;
|
|
308
330
|
/**
|
|
309
331
|
* Consumes the iterator and returns the number of elements it contained.
|
|
332
|
+
*
|
|
333
|
+
* @example ExtraIterator.from([1, 2, 3, 4]).count() // returns 4
|
|
310
334
|
*/
|
|
311
335
|
count(): number;
|
|
312
336
|
/**
|
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,18 @@ export class ExtraIterator extends Iterator {
|
|
|
551
585
|
collect(collectfn) {
|
|
552
586
|
return collectfn(this);
|
|
553
587
|
}
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
return 1;
|
|
562
|
-
}
|
|
563
|
-
return 0;
|
|
564
|
-
});
|
|
588
|
+
/**
|
|
589
|
+
* Sums the numeric value of all elements in the iterator and returns the total.
|
|
590
|
+
*
|
|
591
|
+
* @example ExtraIterator.from([5, 8, 13]).sum() // returns 26
|
|
592
|
+
*/
|
|
593
|
+
sum() {
|
|
594
|
+
return this.reduce((a, b) => a + Number(b), 0);
|
|
565
595
|
}
|
|
566
596
|
/**
|
|
567
597
|
* Consumes the iterator and returns the number of elements it contained.
|
|
598
|
+
*
|
|
599
|
+
* @example ExtraIterator.from([1, 2, 3, 4]).count() // returns 4
|
|
568
600
|
*/
|
|
569
601
|
count() {
|
|
570
602
|
let count = 0;
|
package/dist/index.test.js
CHANGED
|
@@ -126,11 +126,44 @@ 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));
|
|
132
151
|
expect(sum).toBe(6);
|
|
133
152
|
});
|
|
153
|
+
describe('sum', () => {
|
|
154
|
+
it('should sum numbers', () => {
|
|
155
|
+
const iter = ExtraIterator.from([5, 8, 13]);
|
|
156
|
+
expect(iter.sum()).toBe(26);
|
|
157
|
+
});
|
|
158
|
+
it('should sum the numeric value of objects', () => {
|
|
159
|
+
const iter = ExtraIterator.from([
|
|
160
|
+
{ valueOf: () => 5 },
|
|
161
|
+
{ valueOf: () => 8 },
|
|
162
|
+
{ valueOf: () => 13 },
|
|
163
|
+
]);
|
|
164
|
+
expect(iter.sum()).toBe(26);
|
|
165
|
+
});
|
|
166
|
+
});
|
|
134
167
|
it('should create a chain of responsibility function', () => {
|
|
135
168
|
const humanizeDuration = ExtraIterator.from([
|
|
136
169
|
(next, miliseconds) => miliseconds < 1000 ? `${miliseconds} miliseconds` : next(miliseconds / 1000),
|
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.8.0",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"exports": {
|
|
9
9
|
".": "./dist/index.js",
|
|
File without changes
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";
|