extra-iterator 0.8.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 +18 -1
- package/dist/index.js +36 -1
- package/dist/index.test.js +8 -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.
|
|
@@ -343,7 +360,7 @@ export declare class ExtraIterator<T> extends Iterator<T, any, any> {
|
|
|
343
360
|
* ExtraIterator.from([1, 2, 3]).uniqueness() // returns true
|
|
344
361
|
* ExtraIterator.from([1, 2, 3, 1]).uniqueness() // returns false
|
|
345
362
|
*/
|
|
346
|
-
|
|
363
|
+
testUnique(mapper?: (value: T) => unknown): boolean;
|
|
347
364
|
/**
|
|
348
365
|
* Lazily executes a function over each element of this iterator as the values are iterated.
|
|
349
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.
|
|
@@ -615,7 +650,7 @@ export class ExtraIterator extends Iterator {
|
|
|
615
650
|
* ExtraIterator.from([1, 2, 3]).uniqueness() // returns true
|
|
616
651
|
* ExtraIterator.from([1, 2, 3, 1]).uniqueness() // returns false
|
|
617
652
|
*/
|
|
618
|
-
|
|
653
|
+
testUnique(mapper) {
|
|
619
654
|
const seen = new Set();
|
|
620
655
|
for (let next; next = this.next(), !next.done;) {
|
|
621
656
|
const value = mapper ? mapper(next.value) : next.value;
|
package/dist/index.test.js
CHANGED
|
@@ -164,6 +164,14 @@ describe('ExtraIterator', () => {
|
|
|
164
164
|
expect(iter.sum()).toBe(26);
|
|
165
165
|
});
|
|
166
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
|
+
});
|
|
167
175
|
it('should create a chain of responsibility function', () => {
|
|
168
176
|
const humanizeDuration = ExtraIterator.from([
|
|
169
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",
|