@naturalcycles/js-lib 14.191.0 → 14.192.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 DELETED
@@ -1,54 +0,0 @@
1
- import type { AbortableAsyncMapper, AbortableAsyncPredicate, AbortableMapper, AbortablePredicate } from '../types';
2
- import { END } from '../types';
3
- /**
4
- * Inspired by Kotlin Sequences.
5
- * Similar to arrays, but with lazy evaluation, abortable.
6
- * Can be useful when it's not feasible/performant to create an array of values to iterate upfront
7
- * (e.g to construct 1000 Dayjs instances only to find that 2 of them were needed).
8
- *
9
- * @experimental
10
- */
11
- export declare class Sequence<T> implements Iterable<T> {
12
- private nextFn;
13
- private constructor();
14
- [Symbol.iterator](): Iterator<T>;
15
- static create<T>(initialValue: T | typeof END, nextFn: AbortableMapper<T, T>): Sequence<T>;
16
- static range(minIncl: number, maxExcl: number, step?: number): Sequence<number>;
17
- static from<T>(a: Iterable<T>): Sequence<T>;
18
- static empty<T = any>(): Sequence<T>;
19
- private currentValue;
20
- private sentInitialValue;
21
- private i;
22
- next(): T | typeof END;
23
- find(predicate: AbortablePredicate<T>): T | undefined;
24
- some(predicate: AbortablePredicate<T>): boolean;
25
- every(predicate: AbortablePredicate<T>): boolean;
26
- toArray(): T[];
27
- forEach(fn: (v: T, i: number) => void): void;
28
- }
29
- /**
30
- * Convenience function to create a Sequence.
31
- */
32
- export declare function _seq<T>(initialValue: T | typeof END, nextFn: AbortableMapper<T, T>): Sequence<T>;
33
- /**
34
- * Experimental.
35
- * Feasibility to be proven.
36
- *
37
- * @experimental
38
- */
39
- export declare class AsyncSequence<T> implements AsyncIterable<T> {
40
- private nextFn;
41
- private constructor();
42
- [Symbol.asyncIterator](): AsyncIterator<T>;
43
- static create<T>(initialValue: T | typeof END, nextFn: AbortableAsyncMapper<T, T>): AsyncSequence<T>;
44
- static from<T>(a: AsyncIterable<T>): Promise<AsyncSequence<T>>;
45
- static empty<T = any>(): AsyncSequence<T>;
46
- private currentValue;
47
- private sentInitialValue;
48
- private i;
49
- next(): Promise<T | typeof END>;
50
- find(predicate: AbortableAsyncPredicate<T>): Promise<T | undefined>;
51
- some(predicate: AbortableAsyncPredicate<T>): Promise<boolean>;
52
- every(predicate: AbortableAsyncPredicate<T>): Promise<boolean>;
53
- toArray(): Promise<T[]>;
54
- }
package/dist/seq/seq.js DELETED
@@ -1,257 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AsyncSequence = exports._seq = exports.Sequence = void 0;
4
- const types_1 = require("../types");
5
- /**
6
- * Inspired by Kotlin Sequences.
7
- * Similar to arrays, but with lazy evaluation, abortable.
8
- * Can be useful when it's not feasible/performant to create an array of values to iterate upfront
9
- * (e.g to construct 1000 Dayjs instances only to find that 2 of them were needed).
10
- *
11
- * @experimental
12
- */
13
- class Sequence {
14
- constructor(initialValue, nextFn) {
15
- this.nextFn = nextFn;
16
- this.sentInitialValue = false;
17
- this.i = -1;
18
- this.currentValue = initialValue;
19
- }
20
- [Symbol.iterator]() {
21
- return {
22
- next: () => {
23
- const value = this.next();
24
- return value === types_1.END ? { done: true, value: undefined } : { value };
25
- },
26
- };
27
- }
28
- static create(initialValue, nextFn) {
29
- return new Sequence(initialValue, nextFn);
30
- }
31
- static range(minIncl, maxExcl, step = 1) {
32
- const max = maxExcl - step;
33
- return new Sequence(minIncl, n => (n < max ? n + step : types_1.END));
34
- }
35
- static from(a) {
36
- const it = a[Symbol.iterator]();
37
- const v = it.next();
38
- if (v.done)
39
- return new Sequence(types_1.END, () => { });
40
- return new Sequence(v.value, () => {
41
- const v = it.next();
42
- if (v.done)
43
- return types_1.END;
44
- return v.value;
45
- });
46
- }
47
- static empty() {
48
- return new Sequence(types_1.END, () => { });
49
- }
50
- next() {
51
- if (this.currentValue === types_1.END)
52
- return types_1.END;
53
- this.i++;
54
- let v;
55
- if (!this.sentInitialValue) {
56
- this.sentInitialValue = true;
57
- v = this.currentValue;
58
- }
59
- else {
60
- v = this.nextFn(this.currentValue, this.i);
61
- }
62
- // console.log(`_seq`, v)
63
- if (v === types_1.SKIP)
64
- return this.next();
65
- return (this.currentValue = v);
66
- }
67
- // Chainable functions - return another (chained) Sequence
68
- // map<OUT>(mapper: Mapper<T, OUT | typeof SKIP | typeof END>): Seq<OUT> {
69
- // if (this.currentValue === END) return this as any
70
- //
71
- // // Iterate until first valid value, to have as `initialValue` of the new Sequence
72
- // let v: OUT | typeof SKIP | typeof END
73
- //
74
- // while (true) {
75
- // v = mapper(this.currentValue, ++this.i)
76
- // if (v === SKIP) continue
77
- // if (v === END) return this as any
78
- // }
79
- //
80
- // return new Seq<OUT>(v as OUT, (current, i) => {
81
- // const v = mapper(current, i)
82
- //
83
- // })
84
- // }
85
- // Final functions - return final value, not a chained sequence
86
- find(predicate) {
87
- do {
88
- const v = this.next();
89
- if (v === types_1.END)
90
- return; // not found, end of sequence
91
- const r = predicate(v, this.i);
92
- if (r === types_1.END)
93
- return;
94
- if (r)
95
- return v;
96
- // otherwise proceed
97
- } while (true); // eslint-disable-line no-constant-condition
98
- }
99
- some(predicate) {
100
- do {
101
- const v = this.next();
102
- if (v === types_1.END)
103
- return false;
104
- const r = predicate(v, this.i);
105
- if (r === types_1.END)
106
- return false;
107
- if (r)
108
- return true;
109
- } while (true); // eslint-disable-line no-constant-condition
110
- }
111
- every(predicate) {
112
- do {
113
- const v = this.next();
114
- if (v === types_1.END)
115
- return true;
116
- const r = predicate(v, this.i);
117
- if (r === types_1.END)
118
- return true;
119
- if (!r)
120
- return false;
121
- } while (true); // eslint-disable-line no-constant-condition
122
- }
123
- toArray() {
124
- const a = [];
125
- // eslint-disable-next-line no-constant-condition
126
- while (true) {
127
- const v = this.next();
128
- if (v === types_1.END)
129
- return a;
130
- a.push(v);
131
- }
132
- }
133
- forEach(fn) {
134
- let i = -1;
135
- // eslint-disable-next-line no-constant-condition
136
- while (true) {
137
- const v = this.next();
138
- if (v === types_1.END)
139
- return;
140
- fn(v, ++i);
141
- }
142
- }
143
- }
144
- exports.Sequence = Sequence;
145
- /**
146
- * Convenience function to create a Sequence.
147
- */
148
- function _seq(initialValue, nextFn) {
149
- return Sequence.create(initialValue, nextFn);
150
- }
151
- exports._seq = _seq;
152
- /**
153
- * Experimental.
154
- * Feasibility to be proven.
155
- *
156
- * @experimental
157
- */
158
- class AsyncSequence {
159
- constructor(initialValue, nextFn) {
160
- this.nextFn = nextFn;
161
- this.sentInitialValue = false;
162
- this.i = -1;
163
- this.currentValue = initialValue;
164
- }
165
- [Symbol.asyncIterator]() {
166
- return {
167
- next: async () => {
168
- const value = await this.next();
169
- return value === types_1.END ? { done: true, value: undefined } : { value };
170
- },
171
- };
172
- }
173
- static create(initialValue, nextFn) {
174
- return new AsyncSequence(initialValue, nextFn);
175
- }
176
- static async from(a) {
177
- const it = a[Symbol.asyncIterator]();
178
- const v = await it.next();
179
- if (v.done)
180
- return new AsyncSequence(types_1.END, () => { });
181
- return new AsyncSequence(v.value, async () => {
182
- const v = await it.next();
183
- if (v.done)
184
- return types_1.END;
185
- return v.value;
186
- });
187
- }
188
- static empty() {
189
- return new AsyncSequence(types_1.END, () => { });
190
- }
191
- async next() {
192
- if (this.currentValue === types_1.END)
193
- return types_1.END;
194
- this.i++;
195
- let v;
196
- if (!this.sentInitialValue) {
197
- this.sentInitialValue = true;
198
- v = this.currentValue;
199
- }
200
- else {
201
- v = await this.nextFn(this.currentValue, this.i);
202
- }
203
- // console.log(`_seq`, v)
204
- if (v === types_1.SKIP)
205
- return await this.next();
206
- return (this.currentValue = v);
207
- }
208
- // Final functions - return final value, not a chained sequence
209
- async find(predicate) {
210
- do {
211
- const v = await this.next();
212
- if (v === types_1.END)
213
- return; // not found, end of sequence
214
- const r = await predicate(v, this.i);
215
- if (r === types_1.END)
216
- return;
217
- if (r)
218
- return v;
219
- // otherwise proceed
220
- } while (true); // eslint-disable-line no-constant-condition
221
- }
222
- async some(predicate) {
223
- do {
224
- const v = await this.next();
225
- if (v === types_1.END)
226
- return false;
227
- const r = await predicate(v, this.i);
228
- if (r === types_1.END)
229
- return false;
230
- if (r)
231
- return true;
232
- } while (true); // eslint-disable-line no-constant-condition
233
- }
234
- async every(predicate) {
235
- do {
236
- const v = await this.next();
237
- if (v === types_1.END)
238
- return true;
239
- const r = await predicate(v, this.i);
240
- if (r === types_1.END)
241
- return true;
242
- if (!r)
243
- return false;
244
- } while (true); // eslint-disable-line no-constant-condition
245
- }
246
- async toArray() {
247
- const a = [];
248
- // eslint-disable-next-line no-constant-condition
249
- while (true) {
250
- const v = await this.next();
251
- if (v === types_1.END)
252
- return a;
253
- a.push(v);
254
- }
255
- }
256
- }
257
- exports.AsyncSequence = AsyncSequence;
@@ -1,251 +0,0 @@
1
- import { END, SKIP } from '../types';
2
- /**
3
- * Inspired by Kotlin Sequences.
4
- * Similar to arrays, but with lazy evaluation, abortable.
5
- * Can be useful when it's not feasible/performant to create an array of values to iterate upfront
6
- * (e.g to construct 1000 Dayjs instances only to find that 2 of them were needed).
7
- *
8
- * @experimental
9
- */
10
- export class Sequence {
11
- constructor(initialValue, nextFn) {
12
- this.nextFn = nextFn;
13
- this.sentInitialValue = false;
14
- this.i = -1;
15
- this.currentValue = initialValue;
16
- }
17
- [Symbol.iterator]() {
18
- return {
19
- next: () => {
20
- const value = this.next();
21
- return value === END ? { done: true, value: undefined } : { value };
22
- },
23
- };
24
- }
25
- static create(initialValue, nextFn) {
26
- return new Sequence(initialValue, nextFn);
27
- }
28
- static range(minIncl, maxExcl, step = 1) {
29
- const max = maxExcl - step;
30
- return new Sequence(minIncl, n => (n < max ? n + step : END));
31
- }
32
- static from(a) {
33
- const it = a[Symbol.iterator]();
34
- const v = it.next();
35
- if (v.done)
36
- return new Sequence(END, () => { });
37
- return new Sequence(v.value, () => {
38
- const v = it.next();
39
- if (v.done)
40
- return END;
41
- return v.value;
42
- });
43
- }
44
- static empty() {
45
- return new Sequence(END, () => { });
46
- }
47
- next() {
48
- if (this.currentValue === END)
49
- return END;
50
- this.i++;
51
- let v;
52
- if (!this.sentInitialValue) {
53
- this.sentInitialValue = true;
54
- v = this.currentValue;
55
- }
56
- else {
57
- v = this.nextFn(this.currentValue, this.i);
58
- }
59
- // console.log(`_seq`, v)
60
- if (v === SKIP)
61
- return this.next();
62
- return (this.currentValue = v);
63
- }
64
- // Chainable functions - return another (chained) Sequence
65
- // map<OUT>(mapper: Mapper<T, OUT | typeof SKIP | typeof END>): Seq<OUT> {
66
- // if (this.currentValue === END) return this as any
67
- //
68
- // // Iterate until first valid value, to have as `initialValue` of the new Sequence
69
- // let v: OUT | typeof SKIP | typeof END
70
- //
71
- // while (true) {
72
- // v = mapper(this.currentValue, ++this.i)
73
- // if (v === SKIP) continue
74
- // if (v === END) return this as any
75
- // }
76
- //
77
- // return new Seq<OUT>(v as OUT, (current, i) => {
78
- // const v = mapper(current, i)
79
- //
80
- // })
81
- // }
82
- // Final functions - return final value, not a chained sequence
83
- find(predicate) {
84
- do {
85
- const v = this.next();
86
- if (v === END)
87
- return; // not found, end of sequence
88
- const r = predicate(v, this.i);
89
- if (r === END)
90
- return;
91
- if (r)
92
- return v;
93
- // otherwise proceed
94
- } while (true); // eslint-disable-line no-constant-condition
95
- }
96
- some(predicate) {
97
- do {
98
- const v = this.next();
99
- if (v === END)
100
- return false;
101
- const r = predicate(v, this.i);
102
- if (r === END)
103
- return false;
104
- if (r)
105
- return true;
106
- } while (true); // eslint-disable-line no-constant-condition
107
- }
108
- every(predicate) {
109
- do {
110
- const v = this.next();
111
- if (v === END)
112
- return true;
113
- const r = predicate(v, this.i);
114
- if (r === END)
115
- return true;
116
- if (!r)
117
- return false;
118
- } while (true); // eslint-disable-line no-constant-condition
119
- }
120
- toArray() {
121
- const a = [];
122
- // eslint-disable-next-line no-constant-condition
123
- while (true) {
124
- const v = this.next();
125
- if (v === END)
126
- return a;
127
- a.push(v);
128
- }
129
- }
130
- forEach(fn) {
131
- let i = -1;
132
- // eslint-disable-next-line no-constant-condition
133
- while (true) {
134
- const v = this.next();
135
- if (v === END)
136
- return;
137
- fn(v, ++i);
138
- }
139
- }
140
- }
141
- /**
142
- * Convenience function to create a Sequence.
143
- */
144
- export function _seq(initialValue, nextFn) {
145
- return Sequence.create(initialValue, nextFn);
146
- }
147
- /**
148
- * Experimental.
149
- * Feasibility to be proven.
150
- *
151
- * @experimental
152
- */
153
- export class AsyncSequence {
154
- constructor(initialValue, nextFn) {
155
- this.nextFn = nextFn;
156
- this.sentInitialValue = false;
157
- this.i = -1;
158
- this.currentValue = initialValue;
159
- }
160
- [Symbol.asyncIterator]() {
161
- return {
162
- next: async () => {
163
- const value = await this.next();
164
- return value === END ? { done: true, value: undefined } : { value };
165
- },
166
- };
167
- }
168
- static create(initialValue, nextFn) {
169
- return new AsyncSequence(initialValue, nextFn);
170
- }
171
- static async from(a) {
172
- const it = a[Symbol.asyncIterator]();
173
- const v = await it.next();
174
- if (v.done)
175
- return new AsyncSequence(END, () => { });
176
- return new AsyncSequence(v.value, async () => {
177
- const v = await it.next();
178
- if (v.done)
179
- return END;
180
- return v.value;
181
- });
182
- }
183
- static empty() {
184
- return new AsyncSequence(END, () => { });
185
- }
186
- async next() {
187
- if (this.currentValue === END)
188
- return END;
189
- this.i++;
190
- let v;
191
- if (!this.sentInitialValue) {
192
- this.sentInitialValue = true;
193
- v = this.currentValue;
194
- }
195
- else {
196
- v = await this.nextFn(this.currentValue, this.i);
197
- }
198
- // console.log(`_seq`, v)
199
- if (v === SKIP)
200
- return await this.next();
201
- return (this.currentValue = v);
202
- }
203
- // Final functions - return final value, not a chained sequence
204
- async find(predicate) {
205
- do {
206
- const v = await this.next();
207
- if (v === END)
208
- return; // not found, end of sequence
209
- const r = await predicate(v, this.i);
210
- if (r === END)
211
- return;
212
- if (r)
213
- return v;
214
- // otherwise proceed
215
- } while (true); // eslint-disable-line no-constant-condition
216
- }
217
- async some(predicate) {
218
- do {
219
- const v = await this.next();
220
- if (v === END)
221
- return false;
222
- const r = await predicate(v, this.i);
223
- if (r === END)
224
- return false;
225
- if (r)
226
- return true;
227
- } while (true); // eslint-disable-line no-constant-condition
228
- }
229
- async every(predicate) {
230
- do {
231
- const v = await this.next();
232
- if (v === END)
233
- return true;
234
- const r = await predicate(v, this.i);
235
- if (r === END)
236
- return true;
237
- if (!r)
238
- return false;
239
- } while (true); // eslint-disable-line no-constant-condition
240
- }
241
- async toArray() {
242
- const a = [];
243
- // eslint-disable-next-line no-constant-condition
244
- while (true) {
245
- const v = await this.next();
246
- if (v === END)
247
- return a;
248
- a.push(v);
249
- }
250
- }
251
- }