extra-iterator 0.7.0 → 0.9.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 +24 -1
- package/dist/index.js +44 -1
- package/dist/index.test.js +22 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -187,6 +187,23 @@ export declare class ExtraIterator<T> extends Iterator<T, any, any> {
|
|
|
187
187
|
* // returns [[1, 2, 3], [4, 5, 6], [7]]
|
|
188
188
|
*/
|
|
189
189
|
chunk(size: number): ExtraIterator<T[]>;
|
|
190
|
+
/**
|
|
191
|
+
* Groups the elements in this iterator into groups of variable size.
|
|
192
|
+
*
|
|
193
|
+
* This method calls the provided predicate function for each pair of adjacent elements in this iterator. It should
|
|
194
|
+
* return `true` if the elements should belong to the same group, or `false` if they should belong to different
|
|
195
|
+
* groups.
|
|
196
|
+
*
|
|
197
|
+
* The resulting iterator yields arrays of elements that belong to the same group.
|
|
198
|
+
*
|
|
199
|
+
* @example
|
|
200
|
+
*
|
|
201
|
+
* ExtraIterator.from([1, 1, 2, 3, 3, 3, 2, 2])
|
|
202
|
+
* .chunkWith((lhs, rhs) => lhs === rhs)
|
|
203
|
+
* .toArray()
|
|
204
|
+
* // returns [[1, 1], [2], [3, 3, 3], [2, 2]]
|
|
205
|
+
*/
|
|
206
|
+
chunkWith(predicate: (lhs: T, rhs: T, index: number, chunk: [T, ...T[]]) => boolean): ExtraIterator<T[]>;
|
|
190
207
|
/**
|
|
191
208
|
* Creates a new iterator that iterates on this iterator and the proviuded other iterator, yielding arrays of pairs
|
|
192
209
|
* of elements from this iterator and the other.
|
|
@@ -321,6 +338,12 @@ export declare class ExtraIterator<T> extends Iterator<T, any, any> {
|
|
|
321
338
|
* // returns { _a: 2, _b: 4 }
|
|
322
339
|
*/
|
|
323
340
|
collect<U>(collectfn: ((iter: Iterable<T>) => U)): U;
|
|
341
|
+
/**
|
|
342
|
+
* Sums the numeric value of all elements in the iterator and returns the total.
|
|
343
|
+
*
|
|
344
|
+
* @example ExtraIterator.from([5, 8, 13]).sum() // returns 26
|
|
345
|
+
*/
|
|
346
|
+
sum(): number;
|
|
324
347
|
/**
|
|
325
348
|
* Consumes the iterator and returns the number of elements it contained.
|
|
326
349
|
*
|
|
@@ -337,7 +360,7 @@ export declare class ExtraIterator<T> extends Iterator<T, any, any> {
|
|
|
337
360
|
* ExtraIterator.from([1, 2, 3]).uniqueness() // returns true
|
|
338
361
|
* ExtraIterator.from([1, 2, 3, 1]).uniqueness() // returns false
|
|
339
362
|
*/
|
|
340
|
-
|
|
363
|
+
testUnique(mapper?: (value: T) => unknown): boolean;
|
|
341
364
|
/**
|
|
342
365
|
* Lazily executes a function over each element of this iterator as the values are iterated.
|
|
343
366
|
*
|
package/dist/index.js
CHANGED
|
@@ -311,6 +311,41 @@ export class ExtraIterator extends Iterator {
|
|
|
311
311
|
}
|
|
312
312
|
}.call(this));
|
|
313
313
|
}
|
|
314
|
+
/**
|
|
315
|
+
* Groups the elements in this iterator into groups of variable size.
|
|
316
|
+
*
|
|
317
|
+
* This method calls the provided predicate function for each pair of adjacent elements in this iterator. It should
|
|
318
|
+
* return `true` if the elements should belong to the same group, or `false` if they should belong to different
|
|
319
|
+
* groups.
|
|
320
|
+
*
|
|
321
|
+
* The resulting iterator yields arrays of elements that belong to the same group.
|
|
322
|
+
*
|
|
323
|
+
* @example
|
|
324
|
+
*
|
|
325
|
+
* ExtraIterator.from([1, 1, 2, 3, 3, 3, 2, 2])
|
|
326
|
+
* .chunkWith((lhs, rhs) => lhs === rhs)
|
|
327
|
+
* .toArray()
|
|
328
|
+
* // returns [[1, 1], [2], [3, 3, 3], [2, 2]]
|
|
329
|
+
*/
|
|
330
|
+
chunkWith(predicate) {
|
|
331
|
+
return ExtraIterator.from(function* () {
|
|
332
|
+
const first = this.next();
|
|
333
|
+
if (first.done) {
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
let chunk = [first.value];
|
|
337
|
+
for (let left = first, right, index = 0; right = this.next(), !right.done; left = right, index++) {
|
|
338
|
+
if (predicate(left.value, right.value, index, chunk)) {
|
|
339
|
+
chunk.push(left.value);
|
|
340
|
+
}
|
|
341
|
+
else {
|
|
342
|
+
yield chunk;
|
|
343
|
+
chunk = [right.value];
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
yield chunk;
|
|
347
|
+
}.call(this));
|
|
348
|
+
}
|
|
314
349
|
/**
|
|
315
350
|
* Creates a new iterator that iterates on this iterator and the proviuded other iterator, yielding arrays of pairs
|
|
316
351
|
* of elements from this iterator and the other.
|
|
@@ -585,6 +620,14 @@ export class ExtraIterator extends Iterator {
|
|
|
585
620
|
collect(collectfn) {
|
|
586
621
|
return collectfn(this);
|
|
587
622
|
}
|
|
623
|
+
/**
|
|
624
|
+
* Sums the numeric value of all elements in the iterator and returns the total.
|
|
625
|
+
*
|
|
626
|
+
* @example ExtraIterator.from([5, 8, 13]).sum() // returns 26
|
|
627
|
+
*/
|
|
628
|
+
sum() {
|
|
629
|
+
return this.reduce((a, b) => a + Number(b), 0);
|
|
630
|
+
}
|
|
588
631
|
/**
|
|
589
632
|
* Consumes the iterator and returns the number of elements it contained.
|
|
590
633
|
*
|
|
@@ -607,7 +650,7 @@ export class ExtraIterator extends Iterator {
|
|
|
607
650
|
* ExtraIterator.from([1, 2, 3]).uniqueness() // returns true
|
|
608
651
|
* ExtraIterator.from([1, 2, 3, 1]).uniqueness() // returns false
|
|
609
652
|
*/
|
|
610
|
-
|
|
653
|
+
testUnique(mapper) {
|
|
611
654
|
const seen = new Set();
|
|
612
655
|
for (let next; next = this.next(), !next.done;) {
|
|
613
656
|
const value = mapper ? mapper(next.value) : next.value;
|
package/dist/index.test.js
CHANGED
|
@@ -150,6 +150,28 @@ describe('ExtraIterator', () => {
|
|
|
150
150
|
const sum = iterator.collect(iter => Array.from(iter).reduce((a, b) => a + b, 0));
|
|
151
151
|
expect(sum).toBe(6);
|
|
152
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
|
+
});
|
|
167
|
+
describe('chunkWith', () => {
|
|
168
|
+
it('should chunk using a comparer function', () => {
|
|
169
|
+
const result = ExtraIterator.from([1, 1, 2, 3, 3, 3, 2, 2])
|
|
170
|
+
.chunkWith((lhs, rhs) => lhs === rhs)
|
|
171
|
+
.toArray();
|
|
172
|
+
expect(result).toEqual([[1, 1], [2], [3, 3, 3], [2, 2]]);
|
|
173
|
+
});
|
|
174
|
+
});
|
|
153
175
|
it('should create a chain of responsibility function', () => {
|
|
154
176
|
const humanizeDuration = ExtraIterator.from([
|
|
155
177
|
(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.9.0",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"exports": {
|
|
9
9
|
".": "./dist/index.js",
|