@naturalcycles/js-lib 14.196.0 → 14.198.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.
@@ -1,3 +1,4 @@
1
+ import { AsyncIterable2 } from '../iter/asyncIterable2';
1
2
  import { Iterable2 } from '../iter/iterable2';
2
3
  /**
3
4
  * Returns an array with ranges from `from` up to (but not including) `to`.
@@ -14,5 +15,10 @@ export declare function _range(fromIncl: number, toExcl: number, step?: number):
14
15
  /**
15
16
  * Like _range, but returns an Iterable2.
16
17
  */
17
- export declare function _rangeIt(toExcl: number): Iterable2<number>;
18
- export declare function _rangeIt(fromIncl: number, toExcl: number, step?: number): Iterable2<number>;
18
+ export declare function _rangeIterable(toExcl: number): Iterable2<number>;
19
+ export declare function _rangeIterable(fromIncl: number, toExcl: number, step?: number): Iterable2<number>;
20
+ /**
21
+ * Like _range, but returns an AsyncIterable2.
22
+ */
23
+ export declare function _rangeAsyncIterable(toExcl: number): AsyncIterable2<number>;
24
+ export declare function _rangeAsyncIterable(fromIncl: number, toExcl: number, step?: number): AsyncIterable2<number>;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports._rangeIt = exports._range = void 0;
3
+ exports._rangeAsyncIterable = exports._rangeIterable = exports._range = void 0;
4
+ const asyncIterable2_1 = require("../iter/asyncIterable2");
4
5
  const iterable2_1 = require("../iter/iterable2");
5
6
  function _range(fromIncl, toExcl, step = 1) {
6
7
  if (toExcl === undefined) {
@@ -9,7 +10,7 @@ function _range(fromIncl, toExcl, step = 1) {
9
10
  return Array.from({ length: Math.ceil((toExcl - fromIncl) / step) }, (_, i) => i * step + fromIncl);
10
11
  }
11
12
  exports._range = _range;
12
- function _rangeIt(fromIncl, toExcl, step = 1) {
13
+ function _rangeIterable(fromIncl, toExcl, step = 1) {
13
14
  if (toExcl === undefined) {
14
15
  toExcl = fromIncl;
15
16
  fromIncl = 0;
@@ -22,4 +23,18 @@ function _rangeIt(fromIncl, toExcl, step = 1) {
22
23
  },
23
24
  });
24
25
  }
25
- exports._rangeIt = _rangeIt;
26
+ exports._rangeIterable = _rangeIterable;
27
+ function _rangeAsyncIterable(fromIncl, toExcl, step = 1) {
28
+ if (toExcl === undefined) {
29
+ toExcl = fromIncl;
30
+ fromIncl = 0;
31
+ }
32
+ return asyncIterable2_1.AsyncIterable2.of({
33
+ async *[Symbol.asyncIterator]() {
34
+ for (let i = fromIncl; i < toExcl; i += step) {
35
+ yield i;
36
+ }
37
+ },
38
+ });
39
+ }
40
+ exports._rangeAsyncIterable = _rangeAsyncIterable;
@@ -133,7 +133,7 @@ export declare function localDateRange(min: LocalDateInput, max: LocalDateInput,
133
133
  /**
134
134
  * Experimental, returns the range as Iterable2.
135
135
  */
136
- export declare function localDateRangeIt(min: LocalDateInput, max: LocalDateInput, incl?: Inclusiveness, step?: number, stepUnit?: LocalDateUnit): Iterable2<LocalDate>;
136
+ export declare function localDateRangeIterable(min: LocalDateInput, max: LocalDateInput, incl?: Inclusiveness, step?: number, stepUnit?: LocalDateUnit): Iterable2<LocalDate>;
137
137
  /**
138
138
  * Convenience wrapper around `LocalDate.of`
139
139
  */
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.localDateOrToday = exports.localDateOrUndefined = exports.localDateToday = exports.localDate = exports.localDateRangeIt = exports.localDateRange = exports.LocalDate = void 0;
3
+ exports.localDateOrToday = exports.localDateOrUndefined = exports.localDateToday = exports.localDate = exports.localDateRangeIterable = exports.localDateRange = exports.LocalDate = void 0;
4
4
  const assert_1 = require("../error/assert");
5
5
  const iterable2_1 = require("../iter/iterable2");
6
6
  const localTime_1 = require("./localTime");
@@ -441,13 +441,13 @@ class LocalDate {
441
441
  }
442
442
  exports.LocalDate = LocalDate;
443
443
  function localDateRange(min, max, incl = '[)', step = 1, stepUnit = 'day') {
444
- return localDateRangeIt(min, max, incl, step, stepUnit).toArray();
444
+ return localDateRangeIterable(min, max, incl, step, stepUnit).toArray();
445
445
  }
446
446
  exports.localDateRange = localDateRange;
447
447
  /**
448
448
  * Experimental, returns the range as Iterable2.
449
449
  */
450
- function localDateRangeIt(min, max, incl = '[)', step = 1, stepUnit = 'day') {
450
+ function localDateRangeIterable(min, max, incl = '[)', step = 1, stepUnit = 'day') {
451
451
  if (stepUnit === 'week') {
452
452
  step *= 7;
453
453
  stepUnit = 'day';
@@ -474,7 +474,7 @@ function localDateRangeIt(min, max, incl = '[)', step = 1, stepUnit = 'day') {
474
474
  },
475
475
  });
476
476
  }
477
- exports.localDateRangeIt = localDateRangeIt;
477
+ exports.localDateRangeIterable = localDateRangeIterable;
478
478
  /**
479
479
  * Convenience wrapper around `LocalDate.of`
480
480
  */
package/dist/index.d.ts CHANGED
@@ -61,6 +61,7 @@ export * from './string/safeJsonStringify';
61
61
  export * from './promise/pQueue';
62
62
  export * from './promise/abortable';
63
63
  export * from './iter/iterable2';
64
+ export * from './iter/asyncIterable2';
64
65
  export * from './math/stack.util';
65
66
  export * from './string/leven';
66
67
  export * from './datetime/localDate';
package/dist/index.js CHANGED
@@ -65,6 +65,7 @@ tslib_1.__exportStar(require("./string/safeJsonStringify"), exports);
65
65
  tslib_1.__exportStar(require("./promise/pQueue"), exports);
66
66
  tslib_1.__exportStar(require("./promise/abortable"), exports);
67
67
  tslib_1.__exportStar(require("./iter/iterable2"), exports);
68
+ tslib_1.__exportStar(require("./iter/asyncIterable2"), exports);
68
69
  tslib_1.__exportStar(require("./math/stack.util"), exports);
69
70
  tslib_1.__exportStar(require("./string/leven"), exports);
70
71
  tslib_1.__exportStar(require("./datetime/localDate"), exports);
@@ -0,0 +1,27 @@
1
+ import { Promisable } from '../typeFest';
2
+ import { AbortableAsyncMapper, AbortableAsyncPredicate, END } from '../types';
3
+ /**
4
+ * Similar to Iterable2, but for AsyncIterable.
5
+ *
6
+ * AsyncIterable2 is a wrapper around AsyncIterable that implements "Iterator Helpers proposal":
7
+ * https://github.com/tc39/proposal-iterator-helpers
8
+ *
9
+ * AsyncIterable2 can be removed after the proposal is widely implemented in Node & browsers.
10
+ *
11
+ * @experimental
12
+ */
13
+ export declare class AsyncIterable2<T> implements AsyncIterable<T> {
14
+ private it;
15
+ private constructor();
16
+ static of<T>(it: AsyncIterable<T>): AsyncIterable2<T>;
17
+ static ofIterable<T>(it: Iterable<T>): AsyncIterable2<T>;
18
+ static empty<T>(): AsyncIterable2<T>;
19
+ [Symbol.asyncIterator](): AsyncIterator<T>;
20
+ toArray(): Promise<T[]>;
21
+ forEach(cb: (v: T, i: number) => Promisable<any | typeof END>): Promise<void>;
22
+ some(cb: AbortableAsyncPredicate<T>): Promise<boolean>;
23
+ every(cb: AbortableAsyncPredicate<T>): Promise<boolean>;
24
+ find(cb: AbortableAsyncPredicate<T>): Promise<T | undefined>;
25
+ filter(cb: AbortableAsyncPredicate<T>): AsyncIterable2<T>;
26
+ map<OUT>(mapper: AbortableAsyncMapper<T, OUT>): AsyncIterable2<OUT>;
27
+ }
@@ -0,0 +1,107 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AsyncIterable2 = void 0;
4
+ const types_1 = require("../types");
5
+ /**
6
+ * Similar to Iterable2, but for AsyncIterable.
7
+ *
8
+ * AsyncIterable2 is a wrapper around AsyncIterable that implements "Iterator Helpers proposal":
9
+ * https://github.com/tc39/proposal-iterator-helpers
10
+ *
11
+ * AsyncIterable2 can be removed after the proposal is widely implemented in Node & browsers.
12
+ *
13
+ * @experimental
14
+ */
15
+ class AsyncIterable2 {
16
+ constructor(it) {
17
+ this.it = it;
18
+ }
19
+ static of(it) {
20
+ return new AsyncIterable2(it);
21
+ }
22
+ static ofIterable(it) {
23
+ return new AsyncIterable2({
24
+ async *[Symbol.asyncIterator]() {
25
+ yield* it;
26
+ },
27
+ });
28
+ }
29
+ static empty() {
30
+ return new AsyncIterable2({
31
+ async *[Symbol.asyncIterator]() { },
32
+ });
33
+ }
34
+ [Symbol.asyncIterator]() {
35
+ return this.it[Symbol.asyncIterator]();
36
+ }
37
+ async toArray() {
38
+ // todo: Array.fromAsync is not yet available, use that when it's ready
39
+ // return await Array.fromAsync(this.it)
40
+ const res = [];
41
+ for await (const item of this.it) {
42
+ res.push(item);
43
+ }
44
+ return res;
45
+ }
46
+ async forEach(cb) {
47
+ let i = 0;
48
+ for await (const v of this.it) {
49
+ if ((await cb(v, i++)) === types_1.END)
50
+ return;
51
+ }
52
+ }
53
+ async some(cb) {
54
+ return !!(await this.find(cb));
55
+ }
56
+ async every(cb) {
57
+ let i = 0;
58
+ for await (const v of this.it) {
59
+ const r = await cb(v, i++);
60
+ if (r === types_1.END || !r)
61
+ return false;
62
+ }
63
+ return true;
64
+ }
65
+ async find(cb) {
66
+ let i = 0;
67
+ for await (const v of this.it) {
68
+ const r = await cb(v, i++);
69
+ if (r === types_1.END)
70
+ return;
71
+ if (r)
72
+ return v;
73
+ }
74
+ }
75
+ filter(cb) {
76
+ const { it } = this;
77
+ return new AsyncIterable2({
78
+ async *[Symbol.asyncIterator]() {
79
+ let i = 0;
80
+ for await (const v of it) {
81
+ const r = await cb(v, i++);
82
+ if (r === types_1.END)
83
+ return;
84
+ if (r)
85
+ yield v;
86
+ }
87
+ },
88
+ });
89
+ }
90
+ map(mapper) {
91
+ const { it } = this;
92
+ return new AsyncIterable2({
93
+ async *[Symbol.asyncIterator]() {
94
+ let i = 0;
95
+ for await (const v of it) {
96
+ const r = await mapper(v, i++);
97
+ if (r === types_1.END)
98
+ return;
99
+ if (r === types_1.SKIP)
100
+ continue;
101
+ yield r;
102
+ }
103
+ },
104
+ });
105
+ }
106
+ }
107
+ exports.AsyncIterable2 = AsyncIterable2;
@@ -116,8 +116,8 @@ export declare class JsonSchemaObjectBuilder<T extends AnyObject> extends JsonSc
116
116
  minProps(minProperties: number): this;
117
117
  maxProps(maxProperties: number): this;
118
118
  additionalProps(additionalProperties: boolean): this;
119
- baseDBEntity<ID extends string | number = string>(idType?: string): JsonSchemaObjectBuilder<T & BaseDBEntity<ID>>;
120
- savedDBEntity<ID extends string | number = string>(idType?: string): JsonSchemaObjectBuilder<T & SavedDBEntity<ID>>;
119
+ baseDBEntity(): JsonSchemaObjectBuilder<T & BaseDBEntity>;
120
+ savedDBEntity(): JsonSchemaObjectBuilder<T & SavedDBEntity>;
121
121
  extend<T2 extends AnyObject>(s2: JsonSchemaObjectBuilder<T2>): JsonSchemaObjectBuilder<T & T2>;
122
122
  }
123
123
  export declare class JsonSchemaArrayBuilder<ITEM> extends JsonSchemaAnyBuilder<ITEM[], JsonSchemaArray<ITEM>> {
@@ -311,16 +311,16 @@ class JsonSchemaObjectBuilder extends JsonSchemaAnyBuilder {
311
311
  Object.assign(this.schema, { additionalProperties });
312
312
  return this;
313
313
  }
314
- baseDBEntity(idType = 'string') {
314
+ baseDBEntity() {
315
315
  Object.assign(this.schema.properties, {
316
- id: { type: idType },
316
+ id: { type: 'string' },
317
317
  created: { type: 'number', format: 'unixTimestamp2000' },
318
318
  updated: { type: 'number', format: 'unixTimestamp2000' },
319
319
  });
320
320
  return this;
321
321
  }
322
- savedDBEntity(idType = 'string') {
323
- return this.baseDBEntity(idType).addRequired(['id', 'created', 'updated']);
322
+ savedDBEntity() {
323
+ return this.baseDBEntity().addRequired(['id', 'created', 'updated']);
324
324
  }
325
325
  extend(s2) {
326
326
  const builder = new JsonSchemaObjectBuilder();
package/dist/types.d.ts CHANGED
@@ -19,19 +19,19 @@ export type CreatedUpdated = {
19
19
  created: number;
20
20
  updated: number;
21
21
  };
22
- export interface CreatedUpdatedId<ID extends string | number = string | number> extends CreatedUpdated {
23
- id: ID;
22
+ export interface CreatedUpdatedId extends CreatedUpdated {
23
+ id: string;
24
24
  }
25
- export type ObjectWithId<ID extends string | number = string | number> = {
26
- id: ID;
25
+ export type ObjectWithId = {
26
+ id: string;
27
27
  };
28
- export interface AnyObjectWithId<ID extends string | number = string | number> extends AnyObject, ObjectWithId<ID> {
28
+ export interface AnyObjectWithId extends AnyObject, ObjectWithId {
29
29
  }
30
30
  /**
31
31
  * Base interface for any Entity that was saved to DB.
32
32
  */
33
- export type SavedDBEntity<ID extends string | number = string> = {
34
- id: ID;
33
+ export type SavedDBEntity = {
34
+ id: string;
35
35
  /**
36
36
  * unixTimestamp of when the entity was first created (in the DB).
37
37
  */
@@ -47,8 +47,8 @@ export type SavedDBEntity<ID extends string | number = string> = {
47
47
  * hence `id`, `created` and `updated` fields CAN BE undefined (yet).
48
48
  * When it's known to be saved - `SavedDBEntity` interface can be used instead.
49
49
  */
50
- export type BaseDBEntity<ID extends string | number = string> = {
51
- id?: ID;
50
+ export type BaseDBEntity = {
51
+ id?: string;
52
52
  /**
53
53
  * unixTimestamp of when the entity was first created (in the DB).
54
54
  */
@@ -58,8 +58,8 @@ export type BaseDBEntity<ID extends string | number = string> = {
58
58
  */
59
59
  updated?: UnixTimestampNumber;
60
60
  };
61
- export type Saved<T extends Partial<ObjectWithId>> = T extends AnyObject ? Omit<T, 'id' | 'created' | 'updated'> & SavedDBEntity<NonNullable<T['id']>> : T;
62
- export type Unsaved<T extends Partial<ObjectWithId>> = T extends AnyObject ? Omit<T, 'id' | 'created' | 'updated'> & BaseDBEntity<NonNullable<T['id']>> : T;
61
+ export type Saved<T extends Partial<ObjectWithId>> = T extends AnyObject ? Omit<T, 'id' | 'created' | 'updated'> & SavedDBEntity : T;
62
+ export type Unsaved<T extends Partial<ObjectWithId>> = T extends AnyObject ? Omit<T, 'id' | 'created' | 'updated'> & BaseDBEntity : T;
63
63
  export type UnsavedId<T extends Partial<ObjectWithId>> = Omit<T, 'id'> & {
64
64
  id?: T['id'];
65
65
  };
@@ -1,3 +1,5 @@
1
+ import { __asyncGenerator, __await } from "tslib";
2
+ import { AsyncIterable2 } from '../iter/asyncIterable2';
1
3
  import { Iterable2 } from '../iter/iterable2';
2
4
  export function _range(fromIncl, toExcl, step = 1) {
3
5
  if (toExcl === undefined) {
@@ -5,7 +7,7 @@ export function _range(fromIncl, toExcl, step = 1) {
5
7
  }
6
8
  return Array.from({ length: Math.ceil((toExcl - fromIncl) / step) }, (_, i) => i * step + fromIncl);
7
9
  }
8
- export function _rangeIt(fromIncl, toExcl, step = 1) {
10
+ export function _rangeIterable(fromIncl, toExcl, step = 1) {
9
11
  if (toExcl === undefined) {
10
12
  toExcl = fromIncl;
11
13
  fromIncl = 0;
@@ -18,3 +20,18 @@ export function _rangeIt(fromIncl, toExcl, step = 1) {
18
20
  },
19
21
  });
20
22
  }
23
+ export function _rangeAsyncIterable(fromIncl, toExcl, step = 1) {
24
+ if (toExcl === undefined) {
25
+ toExcl = fromIncl;
26
+ fromIncl = 0;
27
+ }
28
+ return AsyncIterable2.of({
29
+ [Symbol.asyncIterator]() {
30
+ return __asyncGenerator(this, arguments, function* _a() {
31
+ for (let i = fromIncl; i < toExcl; i += step) {
32
+ yield yield __await(i);
33
+ }
34
+ });
35
+ },
36
+ });
37
+ }
@@ -437,12 +437,12 @@ export class LocalDate {
437
437
  }
438
438
  }
439
439
  export function localDateRange(min, max, incl = '[)', step = 1, stepUnit = 'day') {
440
- return localDateRangeIt(min, max, incl, step, stepUnit).toArray();
440
+ return localDateRangeIterable(min, max, incl, step, stepUnit).toArray();
441
441
  }
442
442
  /**
443
443
  * Experimental, returns the range as Iterable2.
444
444
  */
445
- export function localDateRangeIt(min, max, incl = '[)', step = 1, stepUnit = 'day') {
445
+ export function localDateRangeIterable(min, max, incl = '[)', step = 1, stepUnit = 'day') {
446
446
  if (stepUnit === 'week') {
447
447
  step *= 7;
448
448
  stepUnit = 'day';
package/dist-esm/index.js CHANGED
@@ -61,6 +61,7 @@ export * from './string/safeJsonStringify';
61
61
  export * from './promise/pQueue';
62
62
  export * from './promise/abortable';
63
63
  export * from './iter/iterable2';
64
+ export * from './iter/asyncIterable2';
64
65
  export * from './math/stack.util';
65
66
  export * from './string/leven';
66
67
  export * from './datetime/localDate';
@@ -0,0 +1,188 @@
1
+ import { __asyncDelegator, __asyncGenerator, __asyncValues, __await } from "tslib";
2
+ import { END, SKIP } from '../types';
3
+ /**
4
+ * Similar to Iterable2, but for AsyncIterable.
5
+ *
6
+ * AsyncIterable2 is a wrapper around AsyncIterable that implements "Iterator Helpers proposal":
7
+ * https://github.com/tc39/proposal-iterator-helpers
8
+ *
9
+ * AsyncIterable2 can be removed after the proposal is widely implemented in Node & browsers.
10
+ *
11
+ * @experimental
12
+ */
13
+ export class AsyncIterable2 {
14
+ constructor(it) {
15
+ this.it = it;
16
+ }
17
+ static of(it) {
18
+ return new AsyncIterable2(it);
19
+ }
20
+ static ofIterable(it) {
21
+ return new AsyncIterable2({
22
+ [Symbol.asyncIterator]() {
23
+ return __asyncGenerator(this, arguments, function* _a() {
24
+ yield __await(yield* __asyncDelegator(__asyncValues(it)));
25
+ });
26
+ },
27
+ });
28
+ }
29
+ static empty() {
30
+ return new AsyncIterable2({
31
+ [Symbol.asyncIterator]() { return __asyncGenerator(this, arguments, function* _a() { }); },
32
+ });
33
+ }
34
+ [Symbol.asyncIterator]() {
35
+ return this.it[Symbol.asyncIterator]();
36
+ }
37
+ async toArray() {
38
+ // todo: Array.fromAsync is not yet available, use that when it's ready
39
+ // return await Array.fromAsync(this.it)
40
+ var _a, e_1, _b, _c;
41
+ const res = [];
42
+ try {
43
+ for (var _d = true, _e = __asyncValues(this.it), _f; _f = await _e.next(), _a = _f.done, !_a; _d = true) {
44
+ _c = _f.value;
45
+ _d = false;
46
+ const item = _c;
47
+ res.push(item);
48
+ }
49
+ }
50
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
51
+ finally {
52
+ try {
53
+ if (!_d && !_a && (_b = _e.return)) await _b.call(_e);
54
+ }
55
+ finally { if (e_1) throw e_1.error; }
56
+ }
57
+ return res;
58
+ }
59
+ async forEach(cb) {
60
+ var _a, e_2, _b, _c;
61
+ let i = 0;
62
+ try {
63
+ for (var _d = true, _e = __asyncValues(this.it), _f; _f = await _e.next(), _a = _f.done, !_a; _d = true) {
64
+ _c = _f.value;
65
+ _d = false;
66
+ const v = _c;
67
+ if ((await cb(v, i++)) === END)
68
+ return;
69
+ }
70
+ }
71
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
72
+ finally {
73
+ try {
74
+ if (!_d && !_a && (_b = _e.return)) await _b.call(_e);
75
+ }
76
+ finally { if (e_2) throw e_2.error; }
77
+ }
78
+ }
79
+ async some(cb) {
80
+ return !!(await this.find(cb));
81
+ }
82
+ async every(cb) {
83
+ var _a, e_3, _b, _c;
84
+ let i = 0;
85
+ try {
86
+ for (var _d = true, _e = __asyncValues(this.it), _f; _f = await _e.next(), _a = _f.done, !_a; _d = true) {
87
+ _c = _f.value;
88
+ _d = false;
89
+ const v = _c;
90
+ const r = await cb(v, i++);
91
+ if (r === END || !r)
92
+ return false;
93
+ }
94
+ }
95
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
96
+ finally {
97
+ try {
98
+ if (!_d && !_a && (_b = _e.return)) await _b.call(_e);
99
+ }
100
+ finally { if (e_3) throw e_3.error; }
101
+ }
102
+ return true;
103
+ }
104
+ async find(cb) {
105
+ var _a, e_4, _b, _c;
106
+ let i = 0;
107
+ try {
108
+ for (var _d = true, _e = __asyncValues(this.it), _f; _f = await _e.next(), _a = _f.done, !_a; _d = true) {
109
+ _c = _f.value;
110
+ _d = false;
111
+ const v = _c;
112
+ const r = await cb(v, i++);
113
+ if (r === END)
114
+ return;
115
+ if (r)
116
+ return v;
117
+ }
118
+ }
119
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
120
+ finally {
121
+ try {
122
+ if (!_d && !_a && (_b = _e.return)) await _b.call(_e);
123
+ }
124
+ finally { if (e_4) throw e_4.error; }
125
+ }
126
+ }
127
+ filter(cb) {
128
+ const { it } = this;
129
+ return new AsyncIterable2({
130
+ [Symbol.asyncIterator]() {
131
+ return __asyncGenerator(this, arguments, function* _a() {
132
+ var _b, e_5, _c, _d;
133
+ let i = 0;
134
+ try {
135
+ for (var _e = true, it_1 = __asyncValues(it), it_1_1; it_1_1 = yield __await(it_1.next()), _b = it_1_1.done, !_b; _e = true) {
136
+ _d = it_1_1.value;
137
+ _e = false;
138
+ const v = _d;
139
+ const r = yield __await(cb(v, i++));
140
+ if (r === END)
141
+ return yield __await(void 0);
142
+ if (r)
143
+ yield yield __await(v);
144
+ }
145
+ }
146
+ catch (e_5_1) { e_5 = { error: e_5_1 }; }
147
+ finally {
148
+ try {
149
+ if (!_e && !_b && (_c = it_1.return)) yield __await(_c.call(it_1));
150
+ }
151
+ finally { if (e_5) throw e_5.error; }
152
+ }
153
+ });
154
+ },
155
+ });
156
+ }
157
+ map(mapper) {
158
+ const { it } = this;
159
+ return new AsyncIterable2({
160
+ [Symbol.asyncIterator]() {
161
+ return __asyncGenerator(this, arguments, function* _a() {
162
+ var _b, e_6, _c, _d;
163
+ let i = 0;
164
+ try {
165
+ for (var _e = true, it_2 = __asyncValues(it), it_2_1; it_2_1 = yield __await(it_2.next()), _b = it_2_1.done, !_b; _e = true) {
166
+ _d = it_2_1.value;
167
+ _e = false;
168
+ const v = _d;
169
+ const r = yield __await(mapper(v, i++));
170
+ if (r === END)
171
+ return yield __await(void 0);
172
+ if (r === SKIP)
173
+ continue;
174
+ yield yield __await(r);
175
+ }
176
+ }
177
+ catch (e_6_1) { e_6 = { error: e_6_1 }; }
178
+ finally {
179
+ try {
180
+ if (!_e && !_b && (_c = it_2.return)) yield __await(_c.call(it_2));
181
+ }
182
+ finally { if (e_6) throw e_6.error; }
183
+ }
184
+ });
185
+ },
186
+ });
187
+ }
188
+ }
@@ -306,16 +306,16 @@ export class JsonSchemaObjectBuilder extends JsonSchemaAnyBuilder {
306
306
  Object.assign(this.schema, { additionalProperties });
307
307
  return this;
308
308
  }
309
- baseDBEntity(idType = 'string') {
309
+ baseDBEntity() {
310
310
  Object.assign(this.schema.properties, {
311
- id: { type: idType },
311
+ id: { type: 'string' },
312
312
  created: { type: 'number', format: 'unixTimestamp2000' },
313
313
  updated: { type: 'number', format: 'unixTimestamp2000' },
314
314
  });
315
315
  return this;
316
316
  }
317
- savedDBEntity(idType = 'string') {
318
- return this.baseDBEntity(idType).addRequired(['id', 'created', 'updated']);
317
+ savedDBEntity() {
318
+ return this.baseDBEntity().addRequired(['id', 'created', 'updated']);
319
319
  }
320
320
  extend(s2) {
321
321
  const builder = new JsonSchemaObjectBuilder();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.196.0",
3
+ "version": "14.198.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -1,3 +1,4 @@
1
+ import { AsyncIterable2 } from '../iter/asyncIterable2'
1
2
  import { Iterable2 } from '../iter/iterable2'
2
3
 
3
4
  /* eslint-disable no-redeclare, unicorn/no-new-array */
@@ -28,9 +29,9 @@ export function _range(fromIncl: number, toExcl?: number, step = 1): number[] {
28
29
  /**
29
30
  * Like _range, but returns an Iterable2.
30
31
  */
31
- export function _rangeIt(toExcl: number): Iterable2<number>
32
- export function _rangeIt(fromIncl: number, toExcl: number, step?: number): Iterable2<number>
33
- export function _rangeIt(fromIncl: number, toExcl?: number, step = 1): Iterable2<number> {
32
+ export function _rangeIterable(toExcl: number): Iterable2<number>
33
+ export function _rangeIterable(fromIncl: number, toExcl: number, step?: number): Iterable2<number>
34
+ export function _rangeIterable(fromIncl: number, toExcl?: number, step = 1): Iterable2<number> {
34
35
  if (toExcl === undefined) {
35
36
  toExcl = fromIncl
36
37
  fromIncl = 0
@@ -44,3 +45,31 @@ export function _rangeIt(fromIncl: number, toExcl?: number, step = 1): Iterable2
44
45
  },
45
46
  })
46
47
  }
48
+
49
+ /**
50
+ * Like _range, but returns an AsyncIterable2.
51
+ */
52
+ export function _rangeAsyncIterable(toExcl: number): AsyncIterable2<number>
53
+ export function _rangeAsyncIterable(
54
+ fromIncl: number,
55
+ toExcl: number,
56
+ step?: number,
57
+ ): AsyncIterable2<number>
58
+ export function _rangeAsyncIterable(
59
+ fromIncl: number,
60
+ toExcl?: number,
61
+ step = 1,
62
+ ): AsyncIterable2<number> {
63
+ if (toExcl === undefined) {
64
+ toExcl = fromIncl
65
+ fromIncl = 0
66
+ }
67
+
68
+ return AsyncIterable2.of({
69
+ async *[Symbol.asyncIterator]() {
70
+ for (let i = fromIncl; i < toExcl!; i += step) {
71
+ yield i
72
+ }
73
+ },
74
+ })
75
+ }
@@ -539,13 +539,13 @@ export function localDateRange(
539
539
  step = 1,
540
540
  stepUnit: LocalDateUnit = 'day',
541
541
  ): LocalDate[] {
542
- return localDateRangeIt(min, max, incl, step, stepUnit).toArray()
542
+ return localDateRangeIterable(min, max, incl, step, stepUnit).toArray()
543
543
  }
544
544
 
545
545
  /**
546
546
  * Experimental, returns the range as Iterable2.
547
547
  */
548
- export function localDateRangeIt(
548
+ export function localDateRangeIterable(
549
549
  min: LocalDateInput,
550
550
  max: LocalDateInput,
551
551
  incl: Inclusiveness = '[)',
package/src/index.ts CHANGED
@@ -61,6 +61,7 @@ export * from './string/safeJsonStringify'
61
61
  export * from './promise/pQueue'
62
62
  export * from './promise/abortable'
63
63
  export * from './iter/iterable2'
64
+ export * from './iter/asyncIterable2'
64
65
  export * from './math/stack.util'
65
66
  export * from './string/leven'
66
67
  export * from './datetime/localDate'
@@ -0,0 +1,109 @@
1
+ import { Promisable } from '../typeFest'
2
+ import { AbortableAsyncMapper, AbortableAsyncPredicate, END, SKIP } from '../types'
3
+
4
+ /**
5
+ * Similar to Iterable2, but for AsyncIterable.
6
+ *
7
+ * AsyncIterable2 is a wrapper around AsyncIterable that implements "Iterator Helpers proposal":
8
+ * https://github.com/tc39/proposal-iterator-helpers
9
+ *
10
+ * AsyncIterable2 can be removed after the proposal is widely implemented in Node & browsers.
11
+ *
12
+ * @experimental
13
+ */
14
+ export class AsyncIterable2<T> implements AsyncIterable<T> {
15
+ private constructor(private it: AsyncIterable<T>) {}
16
+
17
+ static of<T>(it: AsyncIterable<T>): AsyncIterable2<T> {
18
+ return new AsyncIterable2(it)
19
+ }
20
+
21
+ static ofIterable<T>(it: Iterable<T>): AsyncIterable2<T> {
22
+ return new AsyncIterable2<T>({
23
+ async *[Symbol.asyncIterator]() {
24
+ yield* it
25
+ },
26
+ })
27
+ }
28
+
29
+ static empty<T>(): AsyncIterable2<T> {
30
+ return new AsyncIterable2<T>({
31
+ async *[Symbol.asyncIterator]() {},
32
+ })
33
+ }
34
+
35
+ [Symbol.asyncIterator](): AsyncIterator<T> {
36
+ return this.it[Symbol.asyncIterator]()
37
+ }
38
+
39
+ async toArray(): Promise<T[]> {
40
+ // todo: Array.fromAsync is not yet available, use that when it's ready
41
+ // return await Array.fromAsync(this.it)
42
+
43
+ const res: T[] = []
44
+ for await (const item of this.it) {
45
+ res.push(item)
46
+ }
47
+ return res
48
+ }
49
+
50
+ async forEach(cb: (v: T, i: number) => Promisable<any | typeof END>): Promise<void> {
51
+ let i = 0
52
+ for await (const v of this.it) {
53
+ if ((await cb(v, i++)) === END) return
54
+ }
55
+ }
56
+
57
+ async some(cb: AbortableAsyncPredicate<T>): Promise<boolean> {
58
+ return !!(await this.find(cb))
59
+ }
60
+
61
+ async every(cb: AbortableAsyncPredicate<T>): Promise<boolean> {
62
+ let i = 0
63
+ for await (const v of this.it) {
64
+ const r = await cb(v, i++)
65
+ if (r === END || !r) return false
66
+ }
67
+ return true
68
+ }
69
+
70
+ async find(cb: AbortableAsyncPredicate<T>): Promise<T | undefined> {
71
+ let i = 0
72
+ for await (const v of this.it) {
73
+ const r = await cb(v, i++)
74
+ if (r === END) return
75
+ if (r) return v
76
+ }
77
+ }
78
+
79
+ filter(cb: AbortableAsyncPredicate<T>): AsyncIterable2<T> {
80
+ const { it } = this
81
+
82
+ return new AsyncIterable2<T>({
83
+ async *[Symbol.asyncIterator]() {
84
+ let i = 0
85
+ for await (const v of it) {
86
+ const r = await cb(v, i++)
87
+ if (r === END) return
88
+ if (r) yield v
89
+ }
90
+ },
91
+ })
92
+ }
93
+
94
+ map<OUT>(mapper: AbortableAsyncMapper<T, OUT>): AsyncIterable2<OUT> {
95
+ const { it } = this
96
+
97
+ return new AsyncIterable2<OUT>({
98
+ async *[Symbol.asyncIterator]() {
99
+ let i = 0
100
+ for await (const v of it) {
101
+ const r = await mapper(v, i++)
102
+ if (r === END) return
103
+ if (r === SKIP) continue
104
+ yield r
105
+ }
106
+ },
107
+ })
108
+ }
109
+ }
@@ -368,11 +368,9 @@ export class JsonSchemaObjectBuilder<T extends AnyObject> extends JsonSchemaAnyB
368
368
  return this
369
369
  }
370
370
 
371
- baseDBEntity<ID extends string | number = string>(
372
- idType = 'string',
373
- ): JsonSchemaObjectBuilder<T & BaseDBEntity<ID>> {
371
+ baseDBEntity(): JsonSchemaObjectBuilder<T & BaseDBEntity> {
374
372
  Object.assign(this.schema.properties, {
375
- id: { type: idType },
373
+ id: { type: 'string' },
376
374
  created: { type: 'number', format: 'unixTimestamp2000' },
377
375
  updated: { type: 'number', format: 'unixTimestamp2000' },
378
376
  })
@@ -380,10 +378,8 @@ export class JsonSchemaObjectBuilder<T extends AnyObject> extends JsonSchemaAnyB
380
378
  return this
381
379
  }
382
380
 
383
- savedDBEntity<ID extends string | number = string>(
384
- idType = 'string',
385
- ): JsonSchemaObjectBuilder<T & SavedDBEntity<ID>> {
386
- return this.baseDBEntity(idType).addRequired(['id', 'created', 'updated']) as any
381
+ savedDBEntity(): JsonSchemaObjectBuilder<T & SavedDBEntity> {
382
+ return this.baseDBEntity().addRequired(['id', 'created', 'updated']) as any
387
383
  }
388
384
 
389
385
  extend<T2 extends AnyObject>(s2: JsonSchemaObjectBuilder<T2>): JsonSchemaObjectBuilder<T & T2> {
package/src/types.ts CHANGED
@@ -25,26 +25,23 @@ export type CreatedUpdated = {
25
25
  updated: number
26
26
  }
27
27
 
28
- export interface CreatedUpdatedId<ID extends string | number = string | number>
29
- extends CreatedUpdated {
30
- id: ID
28
+ export interface CreatedUpdatedId extends CreatedUpdated {
29
+ id: string
31
30
  }
32
31
 
33
32
  // eslint-disable-next-line @typescript-eslint/consistent-type-definitions
34
- export type ObjectWithId<ID extends string | number = string | number> = {
35
- id: ID
33
+ export type ObjectWithId = {
34
+ id: string
36
35
  }
37
36
 
38
- export interface AnyObjectWithId<ID extends string | number = string | number>
39
- extends AnyObject,
40
- ObjectWithId<ID> {}
37
+ export interface AnyObjectWithId extends AnyObject, ObjectWithId {}
41
38
 
42
39
  /**
43
40
  * Base interface for any Entity that was saved to DB.
44
41
  */
45
42
  // eslint-disable-next-line @typescript-eslint/consistent-type-definitions
46
- export type SavedDBEntity<ID extends string | number = string> = {
47
- id: ID
43
+ export type SavedDBEntity = {
44
+ id: string
48
45
 
49
46
  /**
50
47
  * unixTimestamp of when the entity was first created (in the DB).
@@ -64,8 +61,8 @@ export type SavedDBEntity<ID extends string | number = string> = {
64
61
  * When it's known to be saved - `SavedDBEntity` interface can be used instead.
65
62
  */
66
63
  // eslint-disable-next-line @typescript-eslint/consistent-type-definitions
67
- export type BaseDBEntity<ID extends string | number = string> = {
68
- id?: ID
64
+ export type BaseDBEntity = {
65
+ id?: string
69
66
 
70
67
  /**
71
68
  * unixTimestamp of when the entity was first created (in the DB).
@@ -79,11 +76,11 @@ export type BaseDBEntity<ID extends string | number = string> = {
79
76
  }
80
77
 
81
78
  export type Saved<T extends Partial<ObjectWithId>> = T extends AnyObject
82
- ? Omit<T, 'id' | 'created' | 'updated'> & SavedDBEntity<NonNullable<T['id']>>
79
+ ? Omit<T, 'id' | 'created' | 'updated'> & SavedDBEntity
83
80
  : T
84
81
 
85
82
  export type Unsaved<T extends Partial<ObjectWithId>> = T extends AnyObject
86
- ? Omit<T, 'id' | 'created' | 'updated'> & BaseDBEntity<NonNullable<T['id']>>
83
+ ? Omit<T, 'id' | 'created' | 'updated'> & BaseDBEntity
87
84
  : T
88
85
 
89
86
  export type UnsavedId<T extends Partial<ObjectWithId>> = Omit<T, 'id'> & {