@naturalcycles/js-lib 14.195.0 → 14.197.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.
@@ -562,7 +562,7 @@ class Fetcher {
562
562
  },
563
563
  init: (0, object_util_1._merge)({
564
564
  ...this.cfg.init,
565
- headers: { ...this.cfg.init.headers },
565
+ headers: { ...this.cfg.init.headers }, // this avoids mutation
566
566
  method: opt.method || this.cfg.init.method,
567
567
  credentials: opt.credentials || this.cfg.init.credentials,
568
568
  redirect: opt.redirect || this.cfg.init.redirect || 'follow',
@@ -1,4 +1,4 @@
1
- import { AbortableMapper, AbortablePredicate, END, Predicate } from '../types';
1
+ import { AbortableMapper, AbortablePredicate, END } from '../types';
2
2
  /**
3
3
  * Iterable2 is a wrapper around Iterable that implements "Iterator Helpers proposal":
4
4
  * https://github.com/tc39/proposal-iterator-helpers
@@ -15,7 +15,9 @@ export declare class Iterable2<T> implements Iterable<T> {
15
15
  [Symbol.iterator](): Iterator<T>;
16
16
  toArray(): T[];
17
17
  forEach(cb: (v: T, i: number) => any | typeof END): void;
18
- find(cb: Predicate<T>): T | undefined;
18
+ some(cb: AbortablePredicate<T>): boolean;
19
+ every(cb: AbortablePredicate<T>): boolean;
20
+ find(cb: AbortablePredicate<T>): T | undefined;
19
21
  filter(cb: AbortablePredicate<T>): Iterable2<T>;
20
22
  map<OUT>(mapper: AbortableMapper<T, OUT>): Iterable2<OUT>;
21
23
  }
@@ -33,10 +33,26 @@ class Iterable2 {
33
33
  return;
34
34
  }
35
35
  }
36
+ some(cb) {
37
+ // eslint-disable-next-line unicorn/prefer-array-some
38
+ return !!this.find(cb);
39
+ }
40
+ every(cb) {
41
+ let i = 0;
42
+ for (const v of this.it) {
43
+ const r = cb(v, i++);
44
+ if (r === types_1.END || !r)
45
+ return false;
46
+ }
47
+ return true;
48
+ }
36
49
  find(cb) {
37
50
  let i = 0;
38
51
  for (const v of this.it) {
39
- if (cb(v, i++))
52
+ const r = cb(v, i++);
53
+ if (r === types_1.END)
54
+ return;
55
+ if (r)
40
56
  return v;
41
57
  }
42
58
  }
@@ -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();
@@ -43,7 +43,7 @@ function commonLoggerMinLevel(logger, minLevel, mutate = false) {
43
43
  return exports.commonLoggerNoop;
44
44
  }
45
45
  return {
46
- log: index_1._noop,
46
+ log: index_1._noop, // otherwise it is "log everything" logger (same logger as input)
47
47
  warn: level <= exports.commonLogLevelNumber['warn'] ? logger.warn.bind(logger) : index_1._noop,
48
48
  error: logger.error.bind(logger), // otherwise it's "log nothing" logger (same as noopLogger)
49
49
  };
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
  };
@@ -30,10 +30,26 @@ export class Iterable2 {
30
30
  return;
31
31
  }
32
32
  }
33
+ some(cb) {
34
+ // eslint-disable-next-line unicorn/prefer-array-some
35
+ return !!this.find(cb);
36
+ }
37
+ every(cb) {
38
+ let i = 0;
39
+ for (const v of this.it) {
40
+ const r = cb(v, i++);
41
+ if (r === END || !r)
42
+ return false;
43
+ }
44
+ return true;
45
+ }
33
46
  find(cb) {
34
47
  let i = 0;
35
48
  for (const v of this.it) {
36
- if (cb(v, i++))
49
+ const r = cb(v, i++);
50
+ if (r === END)
51
+ return;
52
+ if (r)
37
53
  return v;
38
54
  }
39
55
  }
@@ -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();
@@ -40,7 +40,7 @@ export function commonLoggerMinLevel(logger, minLevel, mutate = false) {
40
40
  return commonLoggerNoop;
41
41
  }
42
42
  return {
43
- log: _noop,
43
+ log: _noop, // otherwise it is "log everything" logger (same logger as input)
44
44
  warn: level <= commonLogLevelNumber['warn'] ? logger.warn.bind(logger) : _noop,
45
45
  error: logger.error.bind(logger), // otherwise it's "log nothing" logger (same as noopLogger)
46
46
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.195.0",
3
+ "version": "14.197.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -1,4 +1,4 @@
1
- import { AbortableMapper, AbortablePredicate, END, Predicate, SKIP } from '../types'
1
+ import { AbortableMapper, AbortablePredicate, END, SKIP } from '../types'
2
2
 
3
3
  /**
4
4
  * Iterable2 is a wrapper around Iterable that implements "Iterator Helpers proposal":
@@ -34,10 +34,26 @@ export class Iterable2<T> implements Iterable<T> {
34
34
  }
35
35
  }
36
36
 
37
- find(cb: Predicate<T>): T | undefined {
37
+ some(cb: AbortablePredicate<T>): boolean {
38
+ // eslint-disable-next-line unicorn/prefer-array-some
39
+ return !!this.find(cb)
40
+ }
41
+
42
+ every(cb: AbortablePredicate<T>): boolean {
43
+ let i = 0
44
+ for (const v of this.it) {
45
+ const r = cb(v, i++)
46
+ if (r === END || !r) return false
47
+ }
48
+ return true
49
+ }
50
+
51
+ find(cb: AbortablePredicate<T>): T | undefined {
38
52
  let i = 0
39
53
  for (const v of this.it) {
40
- if (cb(v, i++)) return v
54
+ const r = cb(v, i++)
55
+ if (r === END) return
56
+ if (r) return v
41
57
  }
42
58
  }
43
59
 
@@ -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> {
@@ -77,8 +77,8 @@ export function _leven(first: string, second: string): number {
77
77
  ? result + 1
78
78
  : temporary2
79
79
  : temporary2 > temporary
80
- ? temporary + 1
81
- : temporary2
80
+ ? temporary + 1
81
+ : temporary2
82
82
  }
83
83
  }
84
84
 
package/src/typeFest.ts CHANGED
@@ -30,8 +30,8 @@ export type IsEqual<T, U> = (<G>() => G extends T ? 1 : 2) extends <G>() => G ex
30
30
  type Filter<KeyType, ExcludeType> = IsEqual<KeyType, ExcludeType> extends true
31
31
  ? never
32
32
  : KeyType extends ExcludeType
33
- ? never
34
- : KeyType
33
+ ? never
34
+ : KeyType
35
35
 
36
36
  /**
37
37
  Create a type from an object type without certain keys.
@@ -94,12 +94,12 @@ export type Except<ObjectType, KeysType extends keyof ObjectType> = {
94
94
  export type ReadonlyDeep<T> = T extends Primitive | ((...args: any[]) => unknown)
95
95
  ? T
96
96
  : T extends ReadonlyMap<infer KeyType, infer ValueType>
97
- ? ReadonlyMapDeep<KeyType, ValueType>
98
- : T extends ReadonlySet<infer ItemType>
99
- ? ReadonlySetDeep<ItemType>
100
- : T extends object
101
- ? ReadonlyObjectDeep<T>
102
- : unknown
97
+ ? ReadonlyMapDeep<KeyType, ValueType>
98
+ : T extends ReadonlySet<infer ItemType>
99
+ ? ReadonlySetDeep<ItemType>
100
+ : T extends object
101
+ ? ReadonlyObjectDeep<T>
102
+ : unknown
103
103
 
104
104
  /**
105
105
  Same as `ReadonlyDeep`, but accepts only `ReadonlyMap`s as inputs. Internal helper for `ReadonlyDeep`.
@@ -171,8 +171,8 @@ export type Merge<Destination, Source> = {
171
171
  [Key in keyof OmitIndexSignature<Destination & Source>]: Key extends keyof Source
172
172
  ? Source[Key]
173
173
  : Key extends keyof Destination
174
- ? Destination[Key]
175
- : never
174
+ ? Destination[Key]
175
+ : never
176
176
  } & PickIndexSignature<Destination & Source>
177
177
 
178
178
  /**
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'> & {