extra-iterator 0.4.1 → 0.5.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 +12 -0
- package/dist/index.js +22 -0
- package/dist/index.test.js +4 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -200,6 +200,18 @@ export declare class ExtraIterator<T> extends Iterator<T, any, any> {
|
|
|
200
200
|
* // returns [1, 'a', 2, 'a', 3, 'a', 4]
|
|
201
201
|
*/
|
|
202
202
|
interpose<U>(separator: U): ExtraIterator<T | U>;
|
|
203
|
+
/**
|
|
204
|
+
* Creates a new iterator that yields the values of this iterator interposed by separator values produced by calling
|
|
205
|
+
* the callback function provided as argument.
|
|
206
|
+
*
|
|
207
|
+
* @example
|
|
208
|
+
*
|
|
209
|
+
* ExtraIterator.from([2, 3, 5, 8])
|
|
210
|
+
* .interposeWith((lhs, rhs) => (lhs + rhs) / 2)
|
|
211
|
+
* .toArray()
|
|
212
|
+
* // returns [2, 2.5, 3, 4, 5, 6.5, 8]
|
|
213
|
+
*/
|
|
214
|
+
interposeWith<U>(separatorProvider: (lhs: T, rhs: T, index: number) => U): ExtraIterator<T | U>;
|
|
203
215
|
/**
|
|
204
216
|
* Creates a new iterator that yields the values of this iterator and the values of the provided iterator
|
|
205
217
|
* interleaved (alternating). The elements of this iterator always come before the elements of the other iterator.
|
package/dist/index.js
CHANGED
|
@@ -330,6 +330,28 @@ export class ExtraIterator extends Iterator {
|
|
|
330
330
|
}
|
|
331
331
|
}.call(this));
|
|
332
332
|
}
|
|
333
|
+
/**
|
|
334
|
+
* Creates a new iterator that yields the values of this iterator interposed by separator values produced by calling
|
|
335
|
+
* the callback function provided as argument.
|
|
336
|
+
*
|
|
337
|
+
* @example
|
|
338
|
+
*
|
|
339
|
+
* ExtraIterator.from([2, 3, 5, 8])
|
|
340
|
+
* .interposeWith((lhs, rhs) => (lhs + rhs) / 2)
|
|
341
|
+
* .toArray()
|
|
342
|
+
* // returns [2, 2.5, 3, 4, 5, 6.5, 8]
|
|
343
|
+
*/
|
|
344
|
+
interposeWith(separatorProvider) {
|
|
345
|
+
return ExtraIterator.from(function* () {
|
|
346
|
+
for (let previous = this.next(), next, index = 0; !previous.done; previous = next, index++) {
|
|
347
|
+
yield previous.value;
|
|
348
|
+
next = this.next();
|
|
349
|
+
if (!next.done) {
|
|
350
|
+
yield separatorProvider(previous.value, next.value, index);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}.call(this));
|
|
354
|
+
}
|
|
333
355
|
/**
|
|
334
356
|
* Creates a new iterator that yields the values of this iterator and the values of the provided iterator
|
|
335
357
|
* interleaved (alternating). The elements of this iterator always come before the elements of the other iterator.
|
package/dist/index.test.js
CHANGED
|
@@ -57,6 +57,10 @@ describe('ExtraIterator', () => {
|
|
|
57
57
|
const iterator = ExtraIterator.from([1, 2, 3]).interpose(0);
|
|
58
58
|
expect(iterator.toArray()).toEqual([1, 0, 2, 0, 3]);
|
|
59
59
|
});
|
|
60
|
+
it('should interpose with a function', () => {
|
|
61
|
+
const iterator = ExtraIterator.from([1, 2, 3, 5, 8, 13]).interposeWith((a, b) => (a + b) / 2);
|
|
62
|
+
expect(iterator.toArray()).toEqual([1, 1.5, 2, 2.5, 3, 4, 5, 6.5, 8, 10.5, 13]);
|
|
63
|
+
});
|
|
60
64
|
it('should chunk values into groups of a given size', () => {
|
|
61
65
|
const iterator = ExtraIterator.from([1, 2, 3, 4]).chunk(2);
|
|
62
66
|
expect(iterator.toArray()).toEqual([[1, 2], [3, 4]]);
|
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.5.0",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"exports": {
|
|
9
9
|
".": "./dist/index.js",
|