extra-iterator 0.2.0 → 0.3.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.
@@ -0,0 +1,56 @@
1
+ interface ArrayIsh<T> {
2
+ [index: number]: T;
3
+ length: number;
4
+ }
5
+ export type ExtraIteratorSource<T> = Iterator<T, any, any> | Iterable<T, any, any> | ArrayIsh<T>;
6
+ export declare class ExtraIterator<T> extends Iterator<T, any, any> {
7
+ static from<T>(source: ExtraIteratorSource<T>): ExtraIterator<T>;
8
+ static zip<A, B>(a: ExtraIteratorSource<A>, b: ExtraIteratorSource<B>): ExtraIterator<[A, B]>;
9
+ static zip<A, B, C>(a: ExtraIteratorSource<A>, b: ExtraIteratorSource<B>, c: ExtraIteratorSource<C>): ExtraIterator<[A, B, C]>;
10
+ static zip<A, B, C, D>(a: ExtraIteratorSource<A>, b: ExtraIteratorSource<B>, c: ExtraIteratorSource<C>, d: ExtraIteratorSource<D>): ExtraIterator<[A, B, C, D]>;
11
+ static zip<A, B, C, D, E>(a: ExtraIteratorSource<A>, b: ExtraIteratorSource<B>, c: ExtraIteratorSource<C>, d: ExtraIteratorSource<D>, e: ExtraIteratorSource<E>): ExtraIterator<[A, B, C, D, E]>;
12
+ static zip<A, B, C, D, E, F>(a: ExtraIteratorSource<A>, b: ExtraIteratorSource<B>, c: ExtraIteratorSource<C>, d: ExtraIteratorSource<D>, e: ExtraIteratorSource<E>, f: ExtraIteratorSource<F>): ExtraIterator<[A, B, C, D, E, F]>;
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
+ 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
+ static zip<T>(...iterables: ExtraIteratorSource<T>[]): ExtraIterator<T[]>;
16
+ static empty<T = any>(): ExtraIterator<T>;
17
+ static count(): ExtraIterator<number>;
18
+ static count(end: number): ExtraIterator<number>;
19
+ static count(start: number, end: number): ExtraIterator<number>;
20
+ static count(start: number, end: number, interval: number): ExtraIterator<number>;
21
+ static repeat<T>(count: number, value: T): ExtraIterator<T>;
22
+ private constructor();
23
+ private source;
24
+ next(value?: any): IteratorResult<T, any>;
25
+ map<U>(callbackfn: (value: T, index: number) => U): ExtraIterator<U>;
26
+ filter<S extends T>(predicate: (value: T, index: number) => value is S): ExtraIterator<S>;
27
+ filter(predicate: (value: T, index: number) => unknown): ExtraIterator<T>;
28
+ take(limit: number): ExtraIterator<T>;
29
+ private takeLast;
30
+ drop(count: number): ExtraIterator<T>;
31
+ flatMap<U>(callback: (value: T, index: number) => Iterator<U, unknown, undefined> | Iterable<U, unknown, undefined>): ExtraIterator<U>;
32
+ flatten(): T extends Iterable<infer U> ? ExtraIterator<U> : never;
33
+ groupBy<K extends string | symbol>(callbackfn: (value: T, index: number) => K): Record<K, T[]>;
34
+ uniq(keyProvider?: (value: T) => unknown): ExtraIterator<T>;
35
+ compact(): ExtraIterator<Exclude<T, null | undefined>>;
36
+ withEach(callbackfn: (value: T, index: number) => void): ExtraIterator<T>;
37
+ first(): T | undefined;
38
+ last(): T | undefined;
39
+ at(index: number): T | undefined;
40
+ concat(items: Iterable<T>): ExtraIterator<T>;
41
+ append(item: T): ExtraIterator<T>;
42
+ prependMany(items: Iterable<T>): ExtraIterator<T>;
43
+ prepend(item: T): ExtraIterator<T>;
44
+ takeWhile(predicate: (value: T, index: number) => boolean): ExtraIterator<T>;
45
+ dropWhile(predicate: (value: T, index: number) => boolean): ExtraIterator<T>;
46
+ chunk(size: number): ExtraIterator<T[]>;
47
+ zip<U>(other: Iterable<U>): ExtraIterator<[T, U]>;
48
+ interpose<U>(separator: U): ExtraIterator<T | U>;
49
+ interleave<U>(other: ExtraIteratorSource<U>): ExtraIterator<T | U>;
50
+ splice(startIndex: number, deleteCount: number, ...newItems: T[]): ExtraIterator<T>;
51
+ defaultIfEmpty(provider: () => T): ExtraIterator<T>;
52
+ collect<U>(collectfn: ((iter: Iterable<T>) => U)): U;
53
+ toSortedBy(...keys: (keyof T)[]): T[];
54
+ count(): number;
55
+ }
56
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,276 @@
1
+ export class ExtraIterator extends Iterator {
2
+ // TODO Consider using a lib like `make-iterator` to transform things into iterators
3
+ static from(source) {
4
+ if (!(Symbol.iterator in source) && 'length' in source) {
5
+ return new ExtraIterator(function* () {
6
+ for (let index = 0; index < source.length; index++) {
7
+ yield source[index];
8
+ }
9
+ }());
10
+ }
11
+ return new ExtraIterator(source);
12
+ }
13
+ static zip(...iterables) {
14
+ return new ExtraIterator(function* () {
15
+ for (let iterators = iterables.map(iterable => ExtraIterator.from(iterable)), results; results = iterators.map(iterator => iterator.next()),
16
+ results.every(value => !value.done);) {
17
+ yield results.map(value => value.value);
18
+ }
19
+ }().toArray());
20
+ }
21
+ static empty() {
22
+ return new ExtraIterator([]);
23
+ }
24
+ static count(...args) {
25
+ const [start, end, interval] = args.length === 0 ? [0, Infinity, 1]
26
+ : args.length === 1 ? [0, args[0], 1]
27
+ : [args[0], args[1], args[2] ?? 1];
28
+ return new ExtraIterator(function* () {
29
+ for (let counter = start; counter < end; counter += interval) {
30
+ yield counter;
31
+ }
32
+ }());
33
+ }
34
+ static repeat(count, value) {
35
+ return new ExtraIterator(function* () {
36
+ for (let index = 0; index < count; index++) {
37
+ yield value;
38
+ }
39
+ }());
40
+ }
41
+ constructor(source) {
42
+ super();
43
+ this.source = Iterator.from(source);
44
+ if (this.source.return) {
45
+ this.return = this.source.return.bind(this.source);
46
+ }
47
+ if (this.source.throw) {
48
+ this.throw = this.source.throw.bind(this.source);
49
+ }
50
+ }
51
+ source;
52
+ next(value) {
53
+ return this.source.next(value);
54
+ }
55
+ map(callbackfn) {
56
+ return ExtraIterator.from(super.map(callbackfn));
57
+ }
58
+ filter(predicate) {
59
+ return ExtraIterator.from(super.filter(predicate));
60
+ }
61
+ take(limit) {
62
+ return limit >= 0
63
+ ? ExtraIterator.from(super.take(limit))
64
+ : ExtraIterator.from(this.takeLast(-limit));
65
+ }
66
+ takeLast(count) {
67
+ const result = [];
68
+ for (let item; item = this.next(), !item.done;) {
69
+ result.push(item.value);
70
+ if (result.length > count) {
71
+ result.shift();
72
+ }
73
+ }
74
+ return result;
75
+ }
76
+ drop(count) {
77
+ return count >= 0
78
+ ? ExtraIterator.from(super.drop(count))
79
+ : ExtraIterator.from(this.toArray().toSpliced(count, -count));
80
+ }
81
+ flatMap(callback) {
82
+ return ExtraIterator.from(super.flatMap(callback));
83
+ }
84
+ flatten() {
85
+ return this.flatMap(value => value);
86
+ }
87
+ groupBy(callbackfn) {
88
+ const result = Object.create(null);
89
+ for (let index = 0, next; next = this.next(), !next.done; index++) {
90
+ const key = callbackfn(next.value, index);
91
+ if (!result[key]) {
92
+ result[key] = [];
93
+ }
94
+ result[key].push(next.value);
95
+ }
96
+ return result;
97
+ }
98
+ uniq(keyProvider = value => value) {
99
+ return ExtraIterator.from(function* () {
100
+ const seen = new Set();
101
+ for (let item; item = this.next(), !item.done;) {
102
+ const key = keyProvider(item.value);
103
+ if (!seen.has(key)) {
104
+ yield item.value;
105
+ seen.add(key);
106
+ }
107
+ }
108
+ }.call(this));
109
+ }
110
+ compact() {
111
+ const predicate = (value => value !== null && value !== undefined);
112
+ return ExtraIterator.from(this.filter(predicate));
113
+ }
114
+ withEach(callbackfn) {
115
+ return ExtraIterator.from(function* () {
116
+ for (let index = 0, next; next = this.next(), !next.done; index++) {
117
+ callbackfn(next.value, index);
118
+ yield next.value;
119
+ }
120
+ }.call(this));
121
+ }
122
+ first() {
123
+ const next = this.next();
124
+ return next.done ? undefined : next.value;
125
+ }
126
+ last() {
127
+ let previousItem = this.next();
128
+ if (previousItem.done) {
129
+ return undefined;
130
+ }
131
+ for (let currentItem; currentItem = this.next(), !currentItem.done; previousItem = currentItem)
132
+ ;
133
+ return previousItem.value;
134
+ }
135
+ at(index) {
136
+ return index === -1 ? this.last()
137
+ : index < 0 ? this.take(index).at(0)
138
+ : this.drop(index).first();
139
+ }
140
+ concat(items) {
141
+ return ExtraIterator.from(function* () {
142
+ yield* this;
143
+ yield* items;
144
+ }.call(this));
145
+ }
146
+ append(item) {
147
+ return ExtraIterator.from(function* () {
148
+ yield* this;
149
+ yield item;
150
+ }.call(this));
151
+ }
152
+ prependMany(items) {
153
+ return ExtraIterator.from(function* () {
154
+ yield* items;
155
+ yield* this;
156
+ }.call(this));
157
+ }
158
+ prepend(item) {
159
+ return ExtraIterator.from(function* () {
160
+ yield item;
161
+ yield* this;
162
+ }.call(this));
163
+ }
164
+ takeWhile(predicate) {
165
+ return ExtraIterator.from(function* () {
166
+ for (let index = 0, next; next = this.next(), !next.done; index++) {
167
+ if (!predicate(next.value, index)) {
168
+ break;
169
+ }
170
+ yield next.value;
171
+ }
172
+ }.call(this));
173
+ }
174
+ dropWhile(predicate) {
175
+ return ExtraIterator.from(function* () {
176
+ for (let index = 0, next; next = this.next(), !next.done; index++) {
177
+ if (!predicate(next.value, index)) {
178
+ yield next.value;
179
+ break;
180
+ }
181
+ }
182
+ yield* this;
183
+ }.call(this));
184
+ }
185
+ chunk(size) {
186
+ return ExtraIterator.from(function* () {
187
+ for (let next; next = this.next(), !next.done;) {
188
+ yield [next.value, ...this.take(size - 1)];
189
+ }
190
+ }.call(this));
191
+ }
192
+ zip(other) {
193
+ return ExtraIterator.from(function* () {
194
+ const otherIterator = Iterator.from(other);
195
+ for (let thisNext, otherNext; thisNext = this.next(), otherNext = otherIterator.next(), !thisNext.done && !otherNext.done;) {
196
+ yield [thisNext.value, otherNext.value];
197
+ }
198
+ }.call(this));
199
+ }
200
+ interpose(separator) {
201
+ return ExtraIterator.from(function* () {
202
+ for (let next = this.next(); !next.done;) {
203
+ yield next.value;
204
+ next = this.next();
205
+ if (!next.done) {
206
+ yield separator;
207
+ }
208
+ }
209
+ }.call(this));
210
+ }
211
+ interleave(other) {
212
+ return ExtraIterator.from(function* () {
213
+ const otherIterator = ExtraIterator.from(other);
214
+ for (let next, otherNext; next = this.next(),
215
+ otherNext = otherIterator.next(),
216
+ !next.done || !otherNext.done;) {
217
+ if (!next.done) {
218
+ yield next.value;
219
+ }
220
+ if (!otherNext.done) {
221
+ yield otherNext.value;
222
+ }
223
+ }
224
+ }.call(this));
225
+ }
226
+ splice(startIndex, deleteCount, ...newItems) {
227
+ if (startIndex < 0) {
228
+ return ExtraIterator.from(this.toArray()
229
+ .toSpliced(startIndex, deleteCount, ...newItems));
230
+ }
231
+ return ExtraIterator.from(function* () {
232
+ for (let index = 0, next; next = this.next(), !next.done; index++) {
233
+ if (index === startIndex) {
234
+ yield* newItems;
235
+ }
236
+ if (index < startIndex || index >= startIndex + deleteCount) {
237
+ yield next.value;
238
+ }
239
+ }
240
+ }.call(this));
241
+ }
242
+ defaultIfEmpty(provider) {
243
+ return ExtraIterator.from(function* () {
244
+ const result = this.next();
245
+ if (result.done) {
246
+ yield provider();
247
+ }
248
+ else {
249
+ yield result.value;
250
+ yield* this;
251
+ }
252
+ }.call(this));
253
+ }
254
+ collect(collectfn) {
255
+ return collectfn(this);
256
+ }
257
+ toSortedBy(...keys) {
258
+ return this.toArray()
259
+ .sort((a, b) => {
260
+ for (const key of keys) {
261
+ if (a[key] < b[key])
262
+ return -1;
263
+ if (a[key] > b[key])
264
+ return 1;
265
+ }
266
+ return 0;
267
+ });
268
+ }
269
+ count() {
270
+ let count = 0;
271
+ for (let next; next = this.next(), !next.done;) {
272
+ count++;
273
+ }
274
+ return count;
275
+ }
276
+ }
@@ -0,0 +1,16 @@
1
+ import { ExtraIterator } from './index.js';
2
+ export declare const toExtra: unique symbol;
3
+ declare global {
4
+ interface Iterator<T> {
5
+ [toExtra](): ExtraIterator<T>;
6
+ }
7
+ interface Array<T> {
8
+ [toExtra](): ExtraIterator<T>;
9
+ }
10
+ interface Set<T> {
11
+ [toExtra](): ExtraIterator<T>;
12
+ }
13
+ interface Map<K, V> {
14
+ [toExtra](): ExtraIterator<[K, V]>;
15
+ }
16
+ }
@@ -0,0 +1,14 @@
1
+ import { ExtraIterator } from './index.js';
2
+ export const toExtra = Symbol('toExtra');
3
+ Iterator.prototype[toExtra] ??= function () {
4
+ return ExtraIterator.from(this);
5
+ };
6
+ Array.prototype[toExtra] ??= function () {
7
+ return ExtraIterator.from(this);
8
+ };
9
+ Set.prototype[toExtra] ??= function () {
10
+ return ExtraIterator.from(this);
11
+ };
12
+ Map.prototype[toExtra] ??= function () {
13
+ return ExtraIterator.from(this);
14
+ };
package/package.json CHANGED
@@ -3,12 +3,15 @@
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.2.0",
6
+ "version": "0.3.0",
7
7
  "type": "module",
8
8
  "exports": {
9
9
  ".": "./dist/index.js",
10
10
  "./to-extra": "./dist/to-extra.js"
11
11
  },
12
+ "files": [
13
+ "dist"
14
+ ],
12
15
  "scripts": {
13
16
  "test": "node --test **/*.test.js",
14
17
  "build": "tsc",
package/src/index.ts DELETED
@@ -1,306 +0,0 @@
1
- interface ArrayIsh<T> {
2
- [index: number]: T;
3
- length: number;
4
- }
5
-
6
- export class ExtraIterator<T> extends Iterator<T, any, any> {
7
- // TODO Consider using a lib like `make-iterator` to transform things into iterators
8
- static override from<T>(
9
- source: Iterator<T, any, any> | Iterable<T, any, any> | ArrayIsh<T>,
10
- ): ExtraIterator<T> {
11
- if (!(Symbol.iterator in source) && 'length' in source) {
12
- return new ExtraIterator(function*() {
13
- for (let index = 0; index < source.length; index++) {
14
- yield source[index]!;
15
- }
16
- }());
17
- }
18
- return new ExtraIterator(source);
19
- }
20
-
21
- static fromKeys<T extends {}>(subject: T): ExtraIterator<keyof T> {
22
- return new ExtraIterator(Object.keys(subject) as (keyof T)[]);
23
- }
24
-
25
- static fromValues<T extends {}>(subject: T): ExtraIterator<T[keyof T]> {
26
- return new ExtraIterator(Object.values(subject) as T[keyof T][]);
27
- }
28
-
29
- static fromEntries<T extends {}>(subject: T): ExtraIterator<[keyof T, T[keyof T]]> {
30
- return new ExtraIterator(Object.entries(subject) as [keyof T, T[keyof T]][]);
31
- }
32
-
33
- static empty<T = any>(): ExtraIterator<T> {
34
- return new ExtraIterator([]);
35
- }
36
-
37
- static count(max: number): ExtraIterator<number>;
38
- static count(start: number, end: number): ExtraIterator<number>;
39
- static count(start: number, end: number, interval: number): ExtraIterator<number>;
40
- static count(start: number, end?: number, interval?: number): ExtraIterator<number> {
41
- if (typeof end === 'undefined') {
42
- end = start;
43
- start = 0;
44
- }
45
- interval ??= 1;
46
- return new ExtraIterator(function*() {
47
- for (let counter = start; counter < end; counter += interval) {
48
- yield counter;
49
- }
50
- }());
51
- }
52
-
53
- static repeat<T>(count: number, value: T): ExtraIterator<T> {
54
- return new ExtraIterator(function*() {
55
- for (let index = 0; index < count; index++) {
56
- yield value;
57
- }
58
- }());
59
- }
60
-
61
- private constructor(source: Iterator<T, any, any> | Iterable<T, any, any>) {
62
- super();
63
- this.source = Iterator.from<T>(source);
64
- if (this.source.return) {
65
- this.return = this.source.return.bind(this.source);
66
- }
67
- if (this.source.throw) {
68
- this.throw = this.source.throw.bind(this.source);
69
- }
70
- }
71
-
72
- private source: IteratorObject<T, any, any>;
73
-
74
- override next(value?: any): IteratorResult<T, any> {
75
- return this.source.next(value);
76
- }
77
-
78
- override map<U>(callbackfn: (value: T, index: number) => U): ExtraIterator<U> {
79
- return ExtraIterator.from(super.map(callbackfn));
80
- }
81
-
82
- override filter<S extends T>(predicate: (value: T, index: number) => value is S): ExtraIterator<S>;
83
- override filter(predicate: (value: T, index: number) => unknown): ExtraIterator<T>;
84
- override filter(predicate: (value: T, index: number) => unknown): ExtraIterator<T> {
85
- return ExtraIterator.from(super.filter(predicate));
86
- }
87
-
88
- override take(limit: number): ExtraIterator<T> {
89
- return limit >= 0
90
- ? ExtraIterator.from(super.take(limit))
91
- : ExtraIterator.from(this.takeLast(-limit));
92
- }
93
-
94
- private takeLast(count: number): T[] {
95
- const result: T[] = [];
96
- for (let item; item = this.next(), !item.done;) {
97
- result.push(item.value);
98
- if (result.length > count) {
99
- result.shift();
100
- }
101
- }
102
- return result;
103
- }
104
-
105
- override drop(count: number): ExtraIterator<T> {
106
- return count >= 0
107
- ? ExtraIterator.from(super.drop(count))
108
- : ExtraIterator.from(this.toArray().toSpliced(count, -count));
109
- }
110
-
111
- override flatMap<U>(callback: (value: T, index: number) => Iterator<U, unknown, undefined> | Iterable<U, unknown, undefined>): ExtraIterator<U> {
112
- return ExtraIterator.from(super.flatMap(callback));
113
- }
114
-
115
- flatten(): T extends Iterable<infer U> ? ExtraIterator<U> : never {
116
- return this.flatMap(value => value as any) as any;
117
- }
118
-
119
- groupBy<K extends string|symbol>(callbackfn: (value: T, index: number) => K): Record<K, T[]> {
120
- const result: Record<K, T[]> = Object.create(null);
121
- for (let index = 0, next; next = this.next(), !next.done; index++) {
122
- const key = callbackfn(next.value, index);
123
- if (!result[key]) {
124
- result[key] = [];
125
- }
126
- result[key].push(next.value);
127
- }
128
- return result;
129
- }
130
-
131
- uniq(keyProvider: (value: T) => unknown = value => value): ExtraIterator<T> {
132
- return ExtraIterator.from(function*(this: ExtraIterator<T>) {
133
- const seen = new Set<unknown>();
134
- for (let item; item = this.next(), !item.done;) {
135
- const key = keyProvider(item.value);
136
- if (!seen.has(key)) {
137
- yield item.value;
138
- seen.add(key);
139
- }
140
- }
141
- }.call(this));
142
- }
143
-
144
- compact(): ExtraIterator<Exclude<T, null|undefined>> {
145
- const predicate = (value => value !== null && value !== undefined) as
146
- (value: T) => value is Exclude<T, null|undefined>;
147
- return ExtraIterator.from(this.filter(predicate));
148
- }
149
-
150
- withEach(callbackfn: (value: T, index: number) => void): ExtraIterator<T> {
151
- return ExtraIterator.from(function*(this: ExtraIterator<T>) {
152
- for (let index = 0, next; next = this.next(), !next.done; index++) {
153
- callbackfn(next.value, index);
154
- yield next.value;
155
- }
156
- }.call(this));
157
- }
158
-
159
- first(): T|undefined {
160
- const next = this.next();
161
- return next.done ? undefined : next.value;
162
- }
163
-
164
- last(): T|undefined {
165
- let previousItem = this.next();
166
- if (previousItem.done) {
167
- return undefined;
168
- }
169
- for (
170
- let currentItem;
171
- currentItem = this.next(), !currentItem.done;
172
- previousItem = currentItem
173
- );
174
- return previousItem.value;
175
- }
176
-
177
- at(index: number): T|undefined {
178
- return index === -1 ? this.last()
179
- : index < 0 ? this.take(index).at(0)
180
- : this.drop(index).first();
181
- }
182
-
183
- appendMany(items: Iterable<T>): ExtraIterator<T> {
184
- return ExtraIterator.from(function*(this: ExtraIterator<T>) {
185
- yield* this;
186
- yield* items;
187
- }.call(this));
188
- }
189
-
190
- appendOne(item: T): ExtraIterator<T> {
191
- return ExtraIterator.from(function*(this: ExtraIterator<T>) {
192
- yield* this;
193
- yield item;
194
- }.call(this));
195
- }
196
-
197
- prependMany(items: Iterable<T>): ExtraIterator<T> {
198
- return ExtraIterator.from(function*(this: ExtraIterator<T>) {
199
- yield* items;
200
- yield* this;
201
- }.call(this));
202
- }
203
-
204
- prependOne(item: T): ExtraIterator<T> {
205
- return ExtraIterator.from(function*(this: ExtraIterator<T>) {
206
- yield item;
207
- yield* this;
208
- }.call(this));
209
- }
210
-
211
- takeWhile(predicate: (value: T, index: number) => boolean): ExtraIterator<T> {
212
- return ExtraIterator.from(function*(this: ExtraIterator<T>) {
213
- for (let index = 0, next; next = this.next(), !next.done; index++) {
214
- if (!predicate(next.value, index)) {
215
- break;
216
- }
217
- yield next.value;
218
- }
219
- }.call(this));
220
- }
221
-
222
- dropWhile(predicate: (value: T, index: number) => boolean): ExtraIterator<T> {
223
- return ExtraIterator.from(function*(this: ExtraIterator<T>) {
224
- for (let index = 0, next; next = this.next(), !next.done; index++) {
225
- if (!predicate(next.value, index)) {
226
- yield next.value;
227
- break;
228
- }
229
- }
230
- yield* this;
231
- }.call(this));
232
- }
233
-
234
- chunk(size: number): ExtraIterator<T[]> {
235
- return ExtraIterator.from(function*(this: ExtraIterator<T>) {
236
- for (let next; next = this.next(), !next.done;) {
237
- yield [next.value, ...this.take(size - 1)];
238
- }
239
- }.call(this));
240
- }
241
-
242
- /**
243
- * Pairs with another iterable to form an iterator of pairs.
244
- * // TODO Expand to support more than 2 iterables
245
- */
246
- zip<U>(other: Iterable<U>): ExtraIterator<[T, U]> {
247
- return ExtraIterator.from(
248
- function*(this: ExtraIterator<T>): Generator<[T, U]> {
249
- const otherIterator = Iterator.from(other);
250
- for (
251
- let thisNext: IteratorResult<T>, otherNext: IteratorResult<U>;
252
- thisNext = this.next(), otherNext = otherIterator.next(), !thisNext.done && !otherNext.done;
253
- ) {
254
- yield [thisNext.value, otherNext.value];
255
- }
256
- }.call(this)
257
- );
258
- }
259
-
260
- /**
261
- * Transforms from an iterator of pairs into a pair of iterators.
262
- * // TODO Expand to support more than 2 iterables
263
- */
264
- unzip(): T extends [infer U, infer V] ? [ExtraIterator<U>, ExtraIterator<V>] : never {
265
- return [
266
- this.map(value => (value as [T, T])[0]),
267
- this.map(value => (value as [T, T])[1]),
268
- ] as any;
269
- }
270
-
271
- splice(startIndex: number, deleteCount: number, ...newItems: T[]): ExtraIterator<T> {
272
- if (startIndex < 0) {
273
- return ExtraIterator.from(this.toArray()
274
- .toSpliced(startIndex, deleteCount, ...newItems));
275
- }
276
- return ExtraIterator.from(function*(this: ExtraIterator<T>) {
277
- for (let index = 0, next; next = this.next(), !next.done; index++) {
278
- if (index === startIndex) {
279
- yield* newItems;
280
- }
281
- if (index < startIndex || index >= startIndex + deleteCount) {
282
- yield next.value;
283
- }
284
- }
285
- }.call(this));
286
- }
287
-
288
- with(index: number, value: T): ExtraIterator<T> {
289
- return this.splice(index, 1, value);
290
- }
291
-
292
- collect<U>(collectfn: ((iter: Iterable<T>) => U)): U {
293
- return collectfn(this);
294
- }
295
-
296
- toSortedBy(...keys: (keyof T)[]): T[] {
297
- return this.toArray()
298
- .sort((a, b) => {
299
- for (const key of keys) {
300
- if (a[key] < b[key]) return -1;
301
- if (a[key] > b[key]) return 1;
302
- }
303
- return 0;
304
- });
305
- }
306
- }
package/src/to-extra.ts DELETED
@@ -1,34 +0,0 @@
1
- import { ExtraIterator } from './index.js';
2
-
3
- export const toExtra = Symbol('toExtra');
4
-
5
- declare global {
6
- interface Iterator<T> {
7
- [toExtra](): ExtraIterator<T>;
8
- }
9
- interface Array<T> {
10
- [toExtra](): ExtraIterator<T>;
11
- }
12
- interface Set<T> {
13
- [toExtra](): ExtraIterator<T>;
14
- }
15
- interface Map<K, V> {
16
- [toExtra](): ExtraIterator<[K, V]>;
17
- }
18
- }
19
-
20
- Iterator.prototype[toExtra] ??= function<T>(): ExtraIterator<T> {
21
- return ExtraIterator.from(this);
22
- }
23
-
24
- Array.prototype[toExtra] ??= function<T>(): ExtraIterator<T> {
25
- return ExtraIterator.from(this);
26
- }
27
-
28
- Set.prototype[toExtra] ??= function<T>(): ExtraIterator<T> {
29
- return ExtraIterator.from(this);
30
- }
31
-
32
- Map.prototype[toExtra] ??= function<K, V>(): ExtraIterator<[K, V]> {
33
- return ExtraIterator.from(this);
34
- }
package/tsconfig.json DELETED
@@ -1,113 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- /* Visit https://aka.ms/tsconfig to read more about this file */
4
-
5
- /* Projects */
6
- // "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
7
- // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
8
- // "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
9
- // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
10
- // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
11
- // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
12
-
13
- /* Language and Environment */
14
- "target": "ESNext", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
15
- // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
16
- // "jsx": "preserve", /* Specify what JSX code is generated. */
17
- // "libReplacement": true, /* Enable lib replacement. */
18
- // "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
19
- // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
20
- // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
21
- // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
22
- // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
23
- // "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
24
- // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
25
- // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
26
- // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
27
-
28
- /* Modules */
29
- "module": "ESNext", /* Specify what module code is generated. */
30
- "rootDir": "./src", /* Specify the root folder within your source files. */
31
- // "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */
32
- // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
33
- // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
34
- // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
35
- // "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
36
- // "types": [], /* Specify type package names to be included without being referenced in a source file. */
37
- // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
38
- // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
39
- // "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */
40
- // "rewriteRelativeImportExtensions": true, /* Rewrite '.ts', '.tsx', '.mts', and '.cts' file extensions in relative import paths to their JavaScript equivalent in output files. */
41
- // "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
42
- // "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
43
- // "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
44
- // "noUncheckedSideEffectImports": true, /* Check side effect imports. */
45
- // "resolveJsonModule": true, /* Enable importing .json files. */
46
- // "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
47
- // "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
48
-
49
- /* JavaScript Support */
50
- // "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
51
- // "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
52
- // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
53
-
54
- /* Emit */
55
- "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
56
- // "declarationMap": true, /* Create sourcemaps for d.ts files. */
57
- // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
58
- // "sourceMap": true, /* Create source map files for emitted JavaScript files. */
59
- // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
60
- // "noEmit": true, /* Disable emitting files from a compilation. */
61
- // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
62
- "outDir": "./dist", /* Specify an output folder for all emitted files. */
63
- // "removeComments": true, /* Disable emitting comments. */
64
- // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
65
- // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
66
- // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
67
- // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
68
- // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
69
- // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
70
- // "newLine": "crlf", /* Set the newline character for emitting files. */
71
- // "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
72
- // "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
73
- // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
74
- // "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
75
- // "declarationDir": "./", /* Specify the output directory for generated declaration files. */
76
-
77
- /* Interop Constraints */
78
- "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
79
- // "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
80
- // "isolatedDeclarations": true, /* Require sufficient annotation on exports so other tools can trivially generate declaration files. */
81
- // "erasableSyntaxOnly": true, /* Do not allow runtime constructs that are not part of ECMAScript. */
82
- // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
83
- "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
84
- // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
85
- "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
86
-
87
- /* Type Checking */
88
- "strict": true, /* Enable all strict type-checking options. */
89
- "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
90
- "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
91
- "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
92
- "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
93
- "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
94
- "strictBuiltinIteratorReturn": true, /* Built-in iterators are instantiated with a 'TReturn' type of 'undefined' instead of 'any'. */
95
- "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
96
- "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
97
- "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
98
- "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
99
- "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
100
- "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
101
- "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
102
- "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
103
- "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
104
- "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
105
- // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
106
- "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
107
- "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
108
-
109
- /* Completeness */
110
- // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
111
- "skipLibCheck": true /* Skip type checking all .d.ts files. */
112
- }
113
- }