extra-iterator 0.5.0 → 0.6.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 +32 -12
- package/dist/index.js +50 -9
- package/dist/index.test.js +15 -2
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -13,8 +13,8 @@ export declare class ExtraIterator<T> extends Iterator<T, any, any> {
|
|
|
13
13
|
static zip<A, B, C, D, E, F, G>(a: ExtraIteratorSource<A>, b: ExtraIteratorSource<B>, c: ExtraIteratorSource<C>, d: ExtraIteratorSource<D>, e: ExtraIteratorSource<E>, f: ExtraIteratorSource<F>, g: ExtraIteratorSource<G>): ExtraIterator<[A, B, C, D, E, F, G]>;
|
|
14
14
|
static zip<A, B, C, D, E, F, G, H>(a: ExtraIteratorSource<A>, b: ExtraIteratorSource<B>, c: ExtraIteratorSource<C>, d: ExtraIteratorSource<D>, e: ExtraIteratorSource<E>, f: ExtraIteratorSource<F>, g: ExtraIteratorSource<G>, h: ExtraIteratorSource<H>): ExtraIterator<[A, B, C, D, E, F, G, H]>;
|
|
15
15
|
/**
|
|
16
|
-
* Creates
|
|
17
|
-
* provided iterators.
|
|
16
|
+
* Creates a new iterator that iterates over all the provided iterators simultaneously. The returned iterator yields
|
|
17
|
+
* arrays containing the values yielded by each of the provided iterators.
|
|
18
18
|
*
|
|
19
19
|
* @example ExtraIterator.zip([1, 2, 3], ['a', 'b', 'c']).toArray() // returns [ [1, 'a'], [2, 'b'], [3, 'c'] ]
|
|
20
20
|
*/
|
|
@@ -26,23 +26,35 @@ export declare class ExtraIterator<T> extends Iterator<T, any, any> {
|
|
|
26
26
|
*/
|
|
27
27
|
static empty<T = any>(): ExtraIterator<T>;
|
|
28
28
|
/**
|
|
29
|
-
* Creates an iterator that yields incrementing numbers
|
|
30
|
-
* `start` (default 0) and ending at `end`. (exclusive)
|
|
29
|
+
* Creates an iterator that yields incrementing numbers.
|
|
31
30
|
*
|
|
32
|
-
*
|
|
31
|
+
* > ⚠ This iterator is infinite. Use {@link take} method if you want a specific number of values.
|
|
33
32
|
*
|
|
34
|
-
* @example ExtraIterator.count(5).toArray() // returns [0, 1, 2, 3, 4]
|
|
33
|
+
* @example ExtraIterator.count().take(5).toArray() // returns [0, 1, 2, 3, 4]
|
|
35
34
|
*/
|
|
36
|
-
static count(
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
35
|
+
static count({ start, interval }?: {
|
|
36
|
+
start?: number | undefined;
|
|
37
|
+
interval?: number | undefined;
|
|
38
|
+
}): ExtraIterator<number>;
|
|
40
39
|
/**
|
|
41
|
-
* Creates an iterator that yields the provided value
|
|
40
|
+
* Creates an iterator that repeatedly yields the provided value.
|
|
41
|
+
*
|
|
42
|
+
* > ⚠ This iterator is infinite. Use {@link take} method if you want a specific number of values.
|
|
42
43
|
*
|
|
43
44
|
* @example ExtraIterator.repeat(3, 'a').toArray() // returns ['a', 'a', 'a']
|
|
44
45
|
*/
|
|
45
|
-
static repeat<T>(
|
|
46
|
+
static repeat<T>(value: T): ExtraIterator<T>;
|
|
47
|
+
/**
|
|
48
|
+
* Generates random cryptographically strong random numbers.
|
|
49
|
+
*
|
|
50
|
+
* > ⚠ This iterator is infinite. Use {@link take} method if you want a specific number of values.
|
|
51
|
+
*
|
|
52
|
+
* @param param0
|
|
53
|
+
* @returns
|
|
54
|
+
*/
|
|
55
|
+
static random({ bufferSize }?: {
|
|
56
|
+
bufferSize?: number | undefined;
|
|
57
|
+
}): ExtraIterator<number>;
|
|
46
58
|
private constructor();
|
|
47
59
|
private source;
|
|
48
60
|
next(value?: any): IteratorResult<T, any>;
|
|
@@ -243,6 +255,14 @@ export declare class ExtraIterator<T> extends Iterator<T, any, any> {
|
|
|
243
255
|
* Returns an iterator the provided element if this iterator is empty; otherwise, it returns this iterator.
|
|
244
256
|
*/
|
|
245
257
|
defaultIfEmpty(provider: () => T): ExtraIterator<T>;
|
|
258
|
+
/**
|
|
259
|
+
* Creates a new iterator that yields the values of this iterator and then reiterates over the same values and
|
|
260
|
+
* yields each of them again, a number of times determined by the {@param times} parameter. If omitted, loops
|
|
261
|
+
* infinitely.
|
|
262
|
+
*
|
|
263
|
+
* @example ExtraIterator.from([1, 2, 3]).loop(3).toArray() // returns [1, 2, 3, 1, 2, 3, 1, 2, 3]
|
|
264
|
+
*/
|
|
265
|
+
loop(times?: number): ExtraIterator<T>;
|
|
246
266
|
/**
|
|
247
267
|
* Returns the first element of the iterator, or `undefined` if the iterator is empty.
|
|
248
268
|
*/
|
package/dist/index.js
CHANGED
|
@@ -29,28 +29,51 @@ export class ExtraIterator extends Iterator {
|
|
|
29
29
|
static empty() {
|
|
30
30
|
return new ExtraIterator([]);
|
|
31
31
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
32
|
+
/**
|
|
33
|
+
* Creates an iterator that yields incrementing numbers.
|
|
34
|
+
*
|
|
35
|
+
* > ⚠ This iterator is infinite. Use {@link take} method if you want a specific number of values.
|
|
36
|
+
*
|
|
37
|
+
* @example ExtraIterator.count().take(5).toArray() // returns [0, 1, 2, 3, 4]
|
|
38
|
+
*/
|
|
39
|
+
static count({ start = 0, interval = 1 } = {}) {
|
|
36
40
|
return new ExtraIterator(function* () {
|
|
37
|
-
|
|
38
|
-
yield
|
|
41
|
+
while (true) {
|
|
42
|
+
yield start;
|
|
43
|
+
start += interval;
|
|
39
44
|
}
|
|
40
45
|
}());
|
|
41
46
|
}
|
|
42
47
|
/**
|
|
43
|
-
* Creates an iterator that yields the provided value
|
|
48
|
+
* Creates an iterator that repeatedly yields the provided value.
|
|
49
|
+
*
|
|
50
|
+
* > ⚠ This iterator is infinite. Use {@link take} method if you want a specific number of values.
|
|
44
51
|
*
|
|
45
52
|
* @example ExtraIterator.repeat(3, 'a').toArray() // returns ['a', 'a', 'a']
|
|
46
53
|
*/
|
|
47
|
-
static repeat(
|
|
54
|
+
static repeat(value) {
|
|
48
55
|
return new ExtraIterator(function* () {
|
|
49
|
-
|
|
56
|
+
while (true) {
|
|
50
57
|
yield value;
|
|
51
58
|
}
|
|
52
59
|
}());
|
|
53
60
|
}
|
|
61
|
+
/**
|
|
62
|
+
* Generates random cryptographically strong random numbers.
|
|
63
|
+
*
|
|
64
|
+
* > ⚠ This iterator is infinite. Use {@link take} method if you want a specific number of values.
|
|
65
|
+
*
|
|
66
|
+
* @param param0
|
|
67
|
+
* @returns
|
|
68
|
+
*/
|
|
69
|
+
static random({ bufferSize = 1024 } = {}) {
|
|
70
|
+
const buffer = new Uint8Array(bufferSize);
|
|
71
|
+
return new ExtraIterator(function* () {
|
|
72
|
+
globalThis.crypto.getRandomValues(buffer);
|
|
73
|
+
yield* new Float64Array(buffer);
|
|
74
|
+
}())
|
|
75
|
+
.loop();
|
|
76
|
+
}
|
|
54
77
|
// =================================================================================================================
|
|
55
78
|
// PRIVATES
|
|
56
79
|
// =================================================================================================================
|
|
@@ -423,6 +446,24 @@ export class ExtraIterator extends Iterator {
|
|
|
423
446
|
}
|
|
424
447
|
}.call(this));
|
|
425
448
|
}
|
|
449
|
+
/**
|
|
450
|
+
* Creates a new iterator that yields the values of this iterator and then reiterates over the same values and
|
|
451
|
+
* yields each of them again, a number of times determined by the {@param times} parameter. If omitted, loops
|
|
452
|
+
* infinitely.
|
|
453
|
+
*
|
|
454
|
+
* @example ExtraIterator.from([1, 2, 3]).loop(3).toArray() // returns [1, 2, 3, 1, 2, 3, 1, 2, 3]
|
|
455
|
+
*/
|
|
456
|
+
loop(times = Infinity) {
|
|
457
|
+
return ExtraIterator.from(function* () {
|
|
458
|
+
const values = this.toArray();
|
|
459
|
+
if (values.length === 0) {
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
for (let i = 0; i < times; i++) {
|
|
463
|
+
yield* values;
|
|
464
|
+
}
|
|
465
|
+
}.call(this));
|
|
466
|
+
}
|
|
426
467
|
// =================================================================================================================
|
|
427
468
|
// AGGREGATING FUNCTIONS
|
|
428
469
|
// -----------------------------------------------------------------------------------------------------------------
|
package/dist/index.test.js
CHANGED
|
@@ -14,13 +14,22 @@ describe('ExtraIterator', () => {
|
|
|
14
14
|
expect(zipped.toArray()).toEqual([[1, 'a'], [2, 'b'], [3, 'c']]);
|
|
15
15
|
});
|
|
16
16
|
it('should count from 0 to a given number', () => {
|
|
17
|
-
const iterator = ExtraIterator.count(5);
|
|
17
|
+
const iterator = ExtraIterator.count().take(5);
|
|
18
18
|
expect(iterator.toArray()).toEqual([0, 1, 2, 3, 4]);
|
|
19
19
|
});
|
|
20
|
+
it('should count in 2s, starting from 5', () => {
|
|
21
|
+
const iterator = ExtraIterator.count({ start: 5, interval: 2 }).take(5);
|
|
22
|
+
expect(iterator.toArray()).toEqual([5, 7, 9, 11, 13]);
|
|
23
|
+
});
|
|
20
24
|
it('should repeat a value a given number of times', () => {
|
|
21
|
-
const iterator = ExtraIterator.repeat(
|
|
25
|
+
const iterator = ExtraIterator.repeat('x').take(3);
|
|
22
26
|
expect(iterator.toArray()).toEqual(['x', 'x', 'x']);
|
|
23
27
|
});
|
|
28
|
+
it('should yield random values', () => {
|
|
29
|
+
const values = ExtraIterator.random().take(5).toArray();
|
|
30
|
+
expect(values.length).toBe(5);
|
|
31
|
+
expect(values.every(value => typeof value === 'number')).toBe(true);
|
|
32
|
+
});
|
|
24
33
|
it('should filter values based on a predicate', () => {
|
|
25
34
|
const iterator = ExtraIterator.from([1, 2, 3, 4]).filter(x => x % 2 === 0);
|
|
26
35
|
expect(iterator.toArray()).toEqual([2, 4]);
|
|
@@ -141,4 +150,8 @@ describe('ExtraIterator', () => {
|
|
|
141
150
|
const chain = handlers.toChainOfResponsibilityFunction(next => next());
|
|
142
151
|
expect(() => chain()).toThrow();
|
|
143
152
|
});
|
|
153
|
+
it('should repeat values', () => {
|
|
154
|
+
const iterator = ExtraIterator.from([1, 2, 3]).loop(3);
|
|
155
|
+
expect(iterator.toArray()).toEqual([1, 2, 3, 1, 2, 3, 1, 2, 3]);
|
|
156
|
+
});
|
|
144
157
|
});
|
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.6.0",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"exports": {
|
|
9
9
|
".": "./dist/index.js",
|