@naturalcycles/js-lib 14.74.0 → 14.75.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/seq/seq.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { AbortableMapper, AbortablePredicate, END } from '../types';
1
+ import { AbortableAsyncMapper, AbortableAsyncPredicate, AbortableMapper, AbortablePredicate, END } from '../types';
2
2
  /**
3
3
  * Inspired by Kotlin Sequences.
4
4
  * Similar to arrays, but with lazy evaluation, abortable.
@@ -7,14 +7,14 @@ import { AbortableMapper, AbortablePredicate, END } from '../types';
7
7
  *
8
8
  * @experimental
9
9
  */
10
- export declare class Seq<T> implements Iterable<T> {
10
+ export declare class Sequence<T> implements Iterable<T> {
11
11
  private nextFn;
12
12
  private constructor();
13
13
  [Symbol.iterator](): Iterator<T>;
14
- static create<T>(initialValue: T | typeof END, nextFn: AbortableMapper<T, T>): Seq<T>;
15
- static range(minIncl: number, maxExcl: number, step?: number): Seq<number>;
16
- static from<T>(a: Iterable<T>): Seq<T>;
17
- static empty<T = any>(): Seq<T>;
14
+ static create<T>(initialValue: T | typeof END, nextFn: AbortableMapper<T, T>): Sequence<T>;
15
+ static range(minIncl: number, maxExcl: number, step?: number): Sequence<number>;
16
+ static from<T>(a: Iterable<T>): Sequence<T>;
17
+ static empty<T = any>(): Sequence<T>;
18
18
  private currentValue;
19
19
  private sentInitialValue;
20
20
  private i;
@@ -27,4 +27,26 @@ export declare class Seq<T> implements Iterable<T> {
27
27
  /**
28
28
  * Convenience function to create a Sequence.
29
29
  */
30
- export declare function _seq<T>(initialValue: T | typeof END, nextFn: AbortableMapper<T, T>): Seq<T>;
30
+ export declare function _seq<T>(initialValue: T | typeof END, nextFn: AbortableMapper<T, T>): Sequence<T>;
31
+ /**
32
+ * Experimental.
33
+ * Feasibility to be proven.
34
+ *
35
+ * @experimental
36
+ */
37
+ export declare class AsyncSequence<T> implements AsyncIterable<T> {
38
+ private nextFn;
39
+ private constructor();
40
+ [Symbol.asyncIterator](): AsyncIterator<T>;
41
+ static create<T>(initialValue: T | typeof END, nextFn: AbortableAsyncMapper<T, T>): AsyncSequence<T>;
42
+ static from<T>(a: AsyncIterable<T>): Promise<AsyncSequence<T>>;
43
+ static empty<T = any>(): AsyncSequence<T>;
44
+ private currentValue;
45
+ private sentInitialValue;
46
+ private i;
47
+ next(): Promise<T | typeof END>;
48
+ find(predicate: AbortableAsyncPredicate<T>): Promise<T | undefined>;
49
+ some(predicate: AbortableAsyncPredicate<T>): Promise<boolean>;
50
+ every(predicate: AbortableAsyncPredicate<T>): Promise<boolean>;
51
+ toArray(): Promise<T[]>;
52
+ }
package/dist/seq/seq.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports._seq = exports.Seq = void 0;
3
+ exports.AsyncSequence = exports._seq = exports.Sequence = void 0;
4
4
  const types_1 = require("../types");
5
5
  /**
6
6
  * Inspired by Kotlin Sequences.
@@ -10,7 +10,7 @@ const types_1 = require("../types");
10
10
  *
11
11
  * @experimental
12
12
  */
13
- class Seq {
13
+ class Sequence {
14
14
  constructor(initialValue, nextFn) {
15
15
  this.nextFn = nextFn;
16
16
  this.sentInitialValue = false;
@@ -26,18 +26,18 @@ class Seq {
26
26
  };
27
27
  }
28
28
  static create(initialValue, nextFn) {
29
- return new Seq(initialValue, nextFn);
29
+ return new Sequence(initialValue, nextFn);
30
30
  }
31
31
  static range(minIncl, maxExcl, step = 1) {
32
32
  const max = maxExcl - step;
33
- return new Seq(minIncl, n => (n < max ? n + step : types_1.END));
33
+ return new Sequence(minIncl, n => (n < max ? n + step : types_1.END));
34
34
  }
35
35
  static from(a) {
36
36
  const it = a[Symbol.iterator]();
37
37
  const v = it.next();
38
38
  if (v.done)
39
- return new Seq(types_1.END, () => { });
40
- return new Seq(v.value, () => {
39
+ return new Sequence(types_1.END, () => { });
40
+ return new Sequence(v.value, () => {
41
41
  const v = it.next();
42
42
  if (v.done)
43
43
  return types_1.END;
@@ -45,7 +45,7 @@ class Seq {
45
45
  });
46
46
  }
47
47
  static empty() {
48
- return new Seq(types_1.END, () => { });
48
+ return new Sequence(types_1.END, () => { });
49
49
  }
50
50
  next() {
51
51
  if (this.currentValue === types_1.END)
@@ -131,11 +131,118 @@ class Seq {
131
131
  }
132
132
  }
133
133
  }
134
- exports.Seq = Seq;
134
+ exports.Sequence = Sequence;
135
135
  /**
136
136
  * Convenience function to create a Sequence.
137
137
  */
138
138
  function _seq(initialValue, nextFn) {
139
- return Seq.create(initialValue, nextFn);
139
+ return Sequence.create(initialValue, nextFn);
140
140
  }
141
141
  exports._seq = _seq;
142
+ /* eslint-disable no-await-in-loop */
143
+ /**
144
+ * Experimental.
145
+ * Feasibility to be proven.
146
+ *
147
+ * @experimental
148
+ */
149
+ class AsyncSequence {
150
+ constructor(initialValue, nextFn) {
151
+ this.nextFn = nextFn;
152
+ this.sentInitialValue = false;
153
+ this.i = -1;
154
+ this.currentValue = initialValue;
155
+ }
156
+ [Symbol.asyncIterator]() {
157
+ return {
158
+ next: async () => {
159
+ const value = await this.next();
160
+ return value === types_1.END ? { done: true, value: undefined } : { value };
161
+ },
162
+ };
163
+ }
164
+ static create(initialValue, nextFn) {
165
+ return new AsyncSequence(initialValue, nextFn);
166
+ }
167
+ static async from(a) {
168
+ const it = a[Symbol.asyncIterator]();
169
+ const v = await it.next();
170
+ if (v.done)
171
+ return new AsyncSequence(types_1.END, () => { });
172
+ return new AsyncSequence(v.value, async () => {
173
+ const v = await it.next();
174
+ if (v.done)
175
+ return types_1.END;
176
+ return v.value;
177
+ });
178
+ }
179
+ static empty() {
180
+ return new AsyncSequence(types_1.END, () => { });
181
+ }
182
+ async next() {
183
+ if (this.currentValue === types_1.END)
184
+ return types_1.END;
185
+ this.i++;
186
+ let v;
187
+ if (!this.sentInitialValue) {
188
+ this.sentInitialValue = true;
189
+ v = this.currentValue;
190
+ }
191
+ else {
192
+ v = await this.nextFn(this.currentValue, this.i);
193
+ }
194
+ // console.log(`_seq`, v)
195
+ if (v === types_1.SKIP)
196
+ return await this.next();
197
+ return (this.currentValue = v);
198
+ }
199
+ // Final functions - return final value, not a chained sequence
200
+ async find(predicate) {
201
+ do {
202
+ const v = await this.next();
203
+ if (v === types_1.END)
204
+ return; // not found, end of sequence
205
+ const r = await predicate(v, this.i);
206
+ if (r === types_1.END)
207
+ return;
208
+ if (r)
209
+ return v;
210
+ // otherwise proceed
211
+ } while (true); // eslint-disable-line no-constant-condition
212
+ }
213
+ async some(predicate) {
214
+ do {
215
+ const v = await this.next();
216
+ if (v === types_1.END)
217
+ return false;
218
+ const r = await predicate(v, this.i);
219
+ if (r === types_1.END)
220
+ return false;
221
+ if (r)
222
+ return true;
223
+ } while (true); // eslint-disable-line no-constant-condition
224
+ }
225
+ async every(predicate) {
226
+ do {
227
+ const v = await this.next();
228
+ if (v === types_1.END)
229
+ return true;
230
+ const r = await predicate(v, this.i);
231
+ if (r === types_1.END)
232
+ return true;
233
+ if (!r)
234
+ return false;
235
+ } while (true); // eslint-disable-line no-constant-condition
236
+ }
237
+ async toArray() {
238
+ const a = [];
239
+ // eslint-disable-next-line no-constant-condition
240
+ while (true) {
241
+ const v = await this.next();
242
+ if (v === types_1.END)
243
+ return a;
244
+ a.push(v);
245
+ }
246
+ }
247
+ }
248
+ exports.AsyncSequence = AsyncSequence;
@@ -1,4 +1,4 @@
1
- import { END, SKIP } from '../types';
1
+ import { END, SKIP, } from '../types';
2
2
  /**
3
3
  * Inspired by Kotlin Sequences.
4
4
  * Similar to arrays, but with lazy evaluation, abortable.
@@ -7,7 +7,7 @@ import { END, SKIP } from '../types';
7
7
  *
8
8
  * @experimental
9
9
  */
10
- export class Seq {
10
+ export class Sequence {
11
11
  constructor(initialValue, nextFn) {
12
12
  this.nextFn = nextFn;
13
13
  this.sentInitialValue = false;
@@ -23,18 +23,18 @@ export class Seq {
23
23
  };
24
24
  }
25
25
  static create(initialValue, nextFn) {
26
- return new Seq(initialValue, nextFn);
26
+ return new Sequence(initialValue, nextFn);
27
27
  }
28
28
  static range(minIncl, maxExcl, step = 1) {
29
29
  const max = maxExcl - step;
30
- return new Seq(minIncl, n => (n < max ? n + step : END));
30
+ return new Sequence(minIncl, n => (n < max ? n + step : END));
31
31
  }
32
32
  static from(a) {
33
33
  const it = a[Symbol.iterator]();
34
34
  const v = it.next();
35
35
  if (v.done)
36
- return new Seq(END, () => { });
37
- return new Seq(v.value, () => {
36
+ return new Sequence(END, () => { });
37
+ return new Sequence(v.value, () => {
38
38
  const v = it.next();
39
39
  if (v.done)
40
40
  return END;
@@ -42,7 +42,7 @@ export class Seq {
42
42
  });
43
43
  }
44
44
  static empty() {
45
- return new Seq(END, () => { });
45
+ return new Sequence(END, () => { });
46
46
  }
47
47
  next() {
48
48
  if (this.currentValue === END)
@@ -132,5 +132,111 @@ export class Seq {
132
132
  * Convenience function to create a Sequence.
133
133
  */
134
134
  export function _seq(initialValue, nextFn) {
135
- return Seq.create(initialValue, nextFn);
135
+ return Sequence.create(initialValue, nextFn);
136
+ }
137
+ /* eslint-disable no-await-in-loop */
138
+ /**
139
+ * Experimental.
140
+ * Feasibility to be proven.
141
+ *
142
+ * @experimental
143
+ */
144
+ export class AsyncSequence {
145
+ constructor(initialValue, nextFn) {
146
+ this.nextFn = nextFn;
147
+ this.sentInitialValue = false;
148
+ this.i = -1;
149
+ this.currentValue = initialValue;
150
+ }
151
+ [Symbol.asyncIterator]() {
152
+ return {
153
+ next: async () => {
154
+ const value = await this.next();
155
+ return value === END ? { done: true, value: undefined } : { value };
156
+ },
157
+ };
158
+ }
159
+ static create(initialValue, nextFn) {
160
+ return new AsyncSequence(initialValue, nextFn);
161
+ }
162
+ static async from(a) {
163
+ const it = a[Symbol.asyncIterator]();
164
+ const v = await it.next();
165
+ if (v.done)
166
+ return new AsyncSequence(END, () => { });
167
+ return new AsyncSequence(v.value, async () => {
168
+ const v = await it.next();
169
+ if (v.done)
170
+ return END;
171
+ return v.value;
172
+ });
173
+ }
174
+ static empty() {
175
+ return new AsyncSequence(END, () => { });
176
+ }
177
+ async next() {
178
+ if (this.currentValue === END)
179
+ return END;
180
+ this.i++;
181
+ let v;
182
+ if (!this.sentInitialValue) {
183
+ this.sentInitialValue = true;
184
+ v = this.currentValue;
185
+ }
186
+ else {
187
+ v = await this.nextFn(this.currentValue, this.i);
188
+ }
189
+ // console.log(`_seq`, v)
190
+ if (v === SKIP)
191
+ return await this.next();
192
+ return (this.currentValue = v);
193
+ }
194
+ // Final functions - return final value, not a chained sequence
195
+ async find(predicate) {
196
+ do {
197
+ const v = await this.next();
198
+ if (v === END)
199
+ return; // not found, end of sequence
200
+ const r = await predicate(v, this.i);
201
+ if (r === END)
202
+ return;
203
+ if (r)
204
+ return v;
205
+ // otherwise proceed
206
+ } while (true); // eslint-disable-line no-constant-condition
207
+ }
208
+ async some(predicate) {
209
+ do {
210
+ const v = await this.next();
211
+ if (v === END)
212
+ return false;
213
+ const r = await predicate(v, this.i);
214
+ if (r === END)
215
+ return false;
216
+ if (r)
217
+ return true;
218
+ } while (true); // eslint-disable-line no-constant-condition
219
+ }
220
+ async every(predicate) {
221
+ do {
222
+ const v = await this.next();
223
+ if (v === END)
224
+ return true;
225
+ const r = await predicate(v, this.i);
226
+ if (r === END)
227
+ return true;
228
+ if (!r)
229
+ return false;
230
+ } while (true); // eslint-disable-line no-constant-condition
231
+ }
232
+ async toArray() {
233
+ const a = [];
234
+ // eslint-disable-next-line no-constant-condition
235
+ while (true) {
236
+ const v = await this.next();
237
+ if (v === END)
238
+ return a;
239
+ a.push(v);
240
+ }
241
+ }
136
242
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.74.0",
3
+ "version": "14.75.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
package/src/seq/seq.ts CHANGED
@@ -1,4 +1,11 @@
1
- import { AbortableMapper, AbortablePredicate, END, SKIP } from '../types'
1
+ import {
2
+ AbortableAsyncMapper,
3
+ AbortableAsyncPredicate,
4
+ AbortableMapper,
5
+ AbortablePredicate,
6
+ END,
7
+ SKIP,
8
+ } from '../types'
2
9
 
3
10
  /**
4
11
  * Inspired by Kotlin Sequences.
@@ -8,7 +15,7 @@ import { AbortableMapper, AbortablePredicate, END, SKIP } from '../types'
8
15
  *
9
16
  * @experimental
10
17
  */
11
- export class Seq<T> implements Iterable<T> {
18
+ export class Sequence<T> implements Iterable<T> {
12
19
  private constructor(initialValue: T | typeof END, private nextFn: AbortableMapper<T, T>) {
13
20
  this.currentValue = initialValue
14
21
  }
@@ -22,29 +29,29 @@ export class Seq<T> implements Iterable<T> {
22
29
  }
23
30
  }
24
31
 
25
- static create<T>(initialValue: T | typeof END, nextFn: AbortableMapper<T, T>): Seq<T> {
26
- return new Seq(initialValue, nextFn)
32
+ static create<T>(initialValue: T | typeof END, nextFn: AbortableMapper<T, T>): Sequence<T> {
33
+ return new Sequence(initialValue, nextFn)
27
34
  }
28
35
 
29
- static range(minIncl: number, maxExcl: number, step = 1): Seq<number> {
36
+ static range(minIncl: number, maxExcl: number, step = 1): Sequence<number> {
30
37
  const max = maxExcl - step
31
- return new Seq(minIncl, n => (n < max ? n + step : END))
38
+ return new Sequence(minIncl, n => (n < max ? n + step : END))
32
39
  }
33
40
 
34
- static from<T>(a: Iterable<T>): Seq<T> {
41
+ static from<T>(a: Iterable<T>): Sequence<T> {
35
42
  const it = a[Symbol.iterator]()
36
43
  const v = it.next()
37
- if (v.done) return new Seq<any>(END, () => {})
44
+ if (v.done) return new Sequence<any>(END, () => {})
38
45
 
39
- return new Seq(v.value, () => {
46
+ return new Sequence(v.value, () => {
40
47
  const v = it.next()
41
48
  if (v.done) return END
42
49
  return v.value
43
50
  })
44
51
  }
45
52
 
46
- static empty<T = any>(): Seq<T> {
47
- return new Seq(END as any, () => {})
53
+ static empty<T = any>(): Sequence<T> {
54
+ return new Sequence(END as any, () => {})
48
55
  }
49
56
 
50
57
  private currentValue: T | typeof END
@@ -138,6 +145,120 @@ export class Seq<T> implements Iterable<T> {
138
145
  /**
139
146
  * Convenience function to create a Sequence.
140
147
  */
141
- export function _seq<T>(initialValue: T | typeof END, nextFn: AbortableMapper<T, T>): Seq<T> {
142
- return Seq.create(initialValue, nextFn)
148
+ export function _seq<T>(initialValue: T | typeof END, nextFn: AbortableMapper<T, T>): Sequence<T> {
149
+ return Sequence.create(initialValue, nextFn)
150
+ }
151
+
152
+ /* eslint-disable no-await-in-loop */
153
+
154
+ /**
155
+ * Experimental.
156
+ * Feasibility to be proven.
157
+ *
158
+ * @experimental
159
+ */
160
+ export class AsyncSequence<T> implements AsyncIterable<T> {
161
+ private constructor(initialValue: T | typeof END, private nextFn: AbortableAsyncMapper<T, T>) {
162
+ this.currentValue = initialValue
163
+ }
164
+
165
+ [Symbol.asyncIterator](): AsyncIterator<T> {
166
+ return {
167
+ next: async () => {
168
+ const value = await this.next()
169
+ return value === END ? { done: true, value: undefined } : { value }
170
+ },
171
+ }
172
+ }
173
+
174
+ static create<T>(
175
+ initialValue: T | typeof END,
176
+ nextFn: AbortableAsyncMapper<T, T>,
177
+ ): AsyncSequence<T> {
178
+ return new AsyncSequence(initialValue, nextFn)
179
+ }
180
+
181
+ static async from<T>(a: AsyncIterable<T>): Promise<AsyncSequence<T>> {
182
+ const it = a[Symbol.asyncIterator]()
183
+ const v = await it.next()
184
+ if (v.done) return new AsyncSequence<any>(END, () => {})
185
+
186
+ return new AsyncSequence(v.value, async () => {
187
+ const v = await it.next()
188
+ if (v.done) return END
189
+ return v.value
190
+ })
191
+ }
192
+
193
+ static empty<T = any>(): AsyncSequence<T> {
194
+ return new AsyncSequence(END as any, () => {})
195
+ }
196
+
197
+ private currentValue: T | typeof END
198
+ private sentInitialValue = false
199
+ private i = -1
200
+
201
+ async next(): Promise<T | typeof END> {
202
+ if (this.currentValue === END) return END
203
+
204
+ this.i++
205
+
206
+ let v: T | typeof SKIP | typeof END
207
+
208
+ if (!this.sentInitialValue) {
209
+ this.sentInitialValue = true
210
+ v = this.currentValue
211
+ } else {
212
+ v = await this.nextFn(this.currentValue, this.i)
213
+ }
214
+
215
+ // console.log(`_seq`, v)
216
+
217
+ if (v === SKIP) return await this.next()
218
+
219
+ return (this.currentValue = v)
220
+ }
221
+
222
+ // Final functions - return final value, not a chained sequence
223
+ async find(predicate: AbortableAsyncPredicate<T>): Promise<T | undefined> {
224
+ do {
225
+ const v = await this.next()
226
+ if (v === END) return // not found, end of sequence
227
+ const r = await predicate(v, this.i)
228
+ if (r === END) return
229
+ if (r) return v
230
+ // otherwise proceed
231
+ } while (true) // eslint-disable-line no-constant-condition
232
+ }
233
+
234
+ async some(predicate: AbortableAsyncPredicate<T>): Promise<boolean> {
235
+ do {
236
+ const v = await this.next()
237
+ if (v === END) return false
238
+ const r = await predicate(v, this.i)
239
+ if (r === END) return false
240
+ if (r) return true
241
+ } while (true) // eslint-disable-line no-constant-condition
242
+ }
243
+
244
+ async every(predicate: AbortableAsyncPredicate<T>): Promise<boolean> {
245
+ do {
246
+ const v = await this.next()
247
+ if (v === END) return true
248
+ const r = await predicate(v, this.i)
249
+ if (r === END) return true
250
+ if (!r) return false
251
+ } while (true) // eslint-disable-line no-constant-condition
252
+ }
253
+
254
+ async toArray(): Promise<T[]> {
255
+ const a: T[] = []
256
+
257
+ // eslint-disable-next-line no-constant-condition
258
+ while (true) {
259
+ const v = await this.next()
260
+ if (v === END) return a
261
+ a.push(v)
262
+ }
263
+ }
143
264
  }