@naturalcycles/js-lib 14.201.0 → 14.202.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,4 +1,4 @@
1
- import type { AbortableAsyncMapper } from '..';
1
+ import type { AbortableAsyncMapper, CommonLogger } from '..';
2
2
  import { ErrorMode } from '..';
3
3
  export interface PMapOptions {
4
4
  /**
@@ -13,8 +13,14 @@ export interface PMapOptions {
13
13
  * When set to THROW_AGGREGATED, instead of stopping when a promise rejects, it will wait for all the promises to settle and then reject with an Aggregated error.
14
14
  *
15
15
  * When set to SUPPRESS - will ignore errors and return results from successful operations.
16
+ * When in SUPPRESS - errors are still logged via the `logger` (which defaults to console).
16
17
  */
17
18
  errorMode?: ErrorMode;
19
+ /**
20
+ * Default to `console`.
21
+ * Pass `null` to skip logging completely.
22
+ */
23
+ logger?: CommonLogger | null;
18
24
  }
19
25
  /**
20
26
  * Returns a `Promise` that is fulfilled when all promises in `input` and ones returned from `mapper` are fulfilled,
@@ -37,6 +37,7 @@ const __1 = require("..");
37
37
  * })();
38
38
  */
39
39
  async function pMap(iterable, mapper, opt = {}) {
40
+ const { logger = console } = opt;
40
41
  const ret = [];
41
42
  // const iterator = iterable[Symbol.iterator]()
42
43
  const items = [...iterable];
@@ -65,7 +66,10 @@ async function pMap(iterable, mapper, opt = {}) {
65
66
  if (errorMode === __1.ErrorMode.THROW_AGGREGATED) {
66
67
  errors.push(err);
67
68
  }
68
- // otherwise, suppress completely
69
+ else {
70
+ // otherwise, suppress (but still log via logger)
71
+ logger?.error(err);
72
+ }
69
73
  }
70
74
  }
71
75
  if (errors.length) {
@@ -87,6 +91,10 @@ async function pMap(iterable, mapper, opt = {}) {
87
91
  else if (errorMode === __1.ErrorMode.THROW_AGGREGATED) {
88
92
  errors.push(r.reason);
89
93
  }
94
+ else {
95
+ // otherwise, suppress (but still log via logger)
96
+ logger?.error(r.reason);
97
+ }
90
98
  });
91
99
  if (errors.length) {
92
100
  throw new AggregateError(errors, `pMap resulted in ${errors.length} error(s)`);
@@ -133,6 +141,10 @@ async function pMap(iterable, mapper, opt = {}) {
133
141
  if (errorMode === __1.ErrorMode.THROW_AGGREGATED) {
134
142
  errors.push(err);
135
143
  }
144
+ else {
145
+ // otherwise, suppress (but still log via logger)
146
+ logger?.error(err);
147
+ }
136
148
  resolvingCount--;
137
149
  next();
138
150
  }
package/dist/types.d.ts CHANGED
@@ -25,6 +25,9 @@ export interface CreatedUpdatedId extends CreatedUpdated {
25
25
  export type ObjectWithId = {
26
26
  id: string;
27
27
  };
28
+ export type PartialObjectWithId = {
29
+ id?: string;
30
+ };
28
31
  export interface AnyObjectWithId extends AnyObject, ObjectWithId {
29
32
  }
30
33
  /**
@@ -58,9 +61,9 @@ export type BaseDBEntity = {
58
61
  */
59
62
  updated?: UnixTimestampNumber;
60
63
  };
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
- export type UnsavedId<T extends Partial<ObjectWithId>> = Omit<T, 'id'> & {
64
+ export type Saved<T extends PartialObjectWithId> = T extends AnyObject ? Omit<T, 'id' | 'created' | 'updated'> & SavedDBEntity : T;
65
+ export type Unsaved<T extends PartialObjectWithId> = T extends AnyObject ? Omit<T, 'id' | 'created' | 'updated'> & BaseDBEntity : T;
66
+ export type UnsavedId<T extends PartialObjectWithId> = Omit<T, 'id'> & {
64
67
  id?: T['id'];
65
68
  };
66
69
  /**
@@ -34,6 +34,7 @@ import { END, ErrorMode, SKIP } from '..';
34
34
  * })();
35
35
  */
36
36
  export async function pMap(iterable, mapper, opt = {}) {
37
+ const { logger = console } = opt;
37
38
  const ret = [];
38
39
  // const iterator = iterable[Symbol.iterator]()
39
40
  const items = [...iterable];
@@ -62,7 +63,10 @@ export async function pMap(iterable, mapper, opt = {}) {
62
63
  if (errorMode === ErrorMode.THROW_AGGREGATED) {
63
64
  errors.push(err);
64
65
  }
65
- // otherwise, suppress completely
66
+ else {
67
+ // otherwise, suppress (but still log via logger)
68
+ logger === null || logger === void 0 ? void 0 : logger.error(err);
69
+ }
66
70
  }
67
71
  }
68
72
  if (errors.length) {
@@ -84,6 +88,10 @@ export async function pMap(iterable, mapper, opt = {}) {
84
88
  else if (errorMode === ErrorMode.THROW_AGGREGATED) {
85
89
  errors.push(r.reason);
86
90
  }
91
+ else {
92
+ // otherwise, suppress (but still log via logger)
93
+ logger === null || logger === void 0 ? void 0 : logger.error(r.reason);
94
+ }
87
95
  });
88
96
  if (errors.length) {
89
97
  throw new AggregateError(errors, `pMap resulted in ${errors.length} error(s)`);
@@ -130,6 +138,10 @@ export async function pMap(iterable, mapper, opt = {}) {
130
138
  if (errorMode === ErrorMode.THROW_AGGREGATED) {
131
139
  errors.push(err);
132
140
  }
141
+ else {
142
+ // otherwise, suppress (but still log via logger)
143
+ logger === null || logger === void 0 ? void 0 : logger.error(err);
144
+ }
133
145
  resolvingCount--;
134
146
  next();
135
147
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.201.0",
3
+ "version": "14.202.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -7,7 +7,7 @@ Improvements:
7
7
  - Compatible with pProps (that had typings issues)
8
8
  */
9
9
 
10
- import type { AbortableAsyncMapper } from '..'
10
+ import type { AbortableAsyncMapper, CommonLogger } from '..'
11
11
  import { END, ErrorMode, SKIP } from '..'
12
12
 
13
13
  export interface PMapOptions {
@@ -24,8 +24,15 @@ export interface PMapOptions {
24
24
  * When set to THROW_AGGREGATED, instead of stopping when a promise rejects, it will wait for all the promises to settle and then reject with an Aggregated error.
25
25
  *
26
26
  * When set to SUPPRESS - will ignore errors and return results from successful operations.
27
+ * When in SUPPRESS - errors are still logged via the `logger` (which defaults to console).
27
28
  */
28
29
  errorMode?: ErrorMode
30
+
31
+ /**
32
+ * Default to `console`.
33
+ * Pass `null` to skip logging completely.
34
+ */
35
+ logger?: CommonLogger | null
29
36
  }
30
37
 
31
38
  /**
@@ -59,6 +66,7 @@ export async function pMap<IN, OUT>(
59
66
  mapper: AbortableAsyncMapper<IN, OUT>,
60
67
  opt: PMapOptions = {},
61
68
  ): Promise<OUT[]> {
69
+ const { logger = console } = opt
62
70
  const ret: (OUT | typeof SKIP)[] = []
63
71
  // const iterator = iterable[Symbol.iterator]()
64
72
  const items = [...iterable]
@@ -86,8 +94,10 @@ export async function pMap<IN, OUT>(
86
94
  if (errorMode === ErrorMode.THROW_IMMEDIATELY) throw err
87
95
  if (errorMode === ErrorMode.THROW_AGGREGATED) {
88
96
  errors.push(err as Error)
97
+ } else {
98
+ // otherwise, suppress (but still log via logger)
99
+ logger?.error(err)
89
100
  }
90
- // otherwise, suppress completely
91
101
  }
92
102
  }
93
103
 
@@ -110,6 +120,9 @@ export async function pMap<IN, OUT>(
110
120
  if (r.value !== SKIP && r.value !== END) ret.push(r.value)
111
121
  } else if (errorMode === ErrorMode.THROW_AGGREGATED) {
112
122
  errors.push(r.reason)
123
+ } else {
124
+ // otherwise, suppress (but still log via logger)
125
+ logger?.error(r.reason)
113
126
  }
114
127
  })
115
128
 
@@ -165,6 +178,9 @@ export async function pMap<IN, OUT>(
165
178
  } else {
166
179
  if (errorMode === ErrorMode.THROW_AGGREGATED) {
167
180
  errors.push(err)
181
+ } else {
182
+ // otherwise, suppress (but still log via logger)
183
+ logger?.error(err)
168
184
  }
169
185
  resolvingCount--
170
186
  next()
package/src/typeFest.ts CHANGED
@@ -17,9 +17,8 @@ export type Simplify<T> = { [KeyType in keyof T]: T[KeyType] }
17
17
  @link https://github.com/microsoft/TypeScript/issues/27024#issuecomment-421529650
18
18
  @link https://stackoverflow.com/questions/68961864/how-does-the-equals-work-in-typescript/68963796#68963796
19
19
  */
20
- export type IsEqual<T, U> = (<G>() => G extends T ? 1 : 2) extends <G>() => G extends U ? 1 : 2
21
- ? true
22
- : false
20
+ export type IsEqual<T, U> =
21
+ (<G>() => G extends T ? 1 : 2) extends <G>() => G extends U ? 1 : 2 ? true : false
23
22
 
24
23
  /**
25
24
  * Filter out keys from an object.
@@ -27,11 +26,8 @@ export type IsEqual<T, U> = (<G>() => G extends T ? 1 : 2) extends <G>() => G ex
27
26
  * Returns `never` if `Key` extends `Exclude`.
28
27
  * Returns `Key` otherwise.
29
28
  */
30
- type Filter<KeyType, ExcludeType> = IsEqual<KeyType, ExcludeType> extends true
31
- ? never
32
- : KeyType extends ExcludeType
33
- ? never
34
- : KeyType
29
+ type Filter<KeyType, ExcludeType> =
30
+ IsEqual<KeyType, ExcludeType> extends true ? never : KeyType extends ExcludeType ? never : KeyType
35
31
 
36
32
  /**
37
33
  Create a type from an object type without certain keys.
package/src/types.ts CHANGED
@@ -34,6 +34,11 @@ export type ObjectWithId = {
34
34
  id: string
35
35
  }
36
36
 
37
+ // eslint-disable-next-line @typescript-eslint/consistent-type-definitions
38
+ export type PartialObjectWithId = {
39
+ id?: string
40
+ }
41
+
37
42
  export interface AnyObjectWithId extends AnyObject, ObjectWithId {}
38
43
 
39
44
  /**
@@ -75,15 +80,15 @@ export type BaseDBEntity = {
75
80
  updated?: UnixTimestampNumber
76
81
  }
77
82
 
78
- export type Saved<T extends Partial<ObjectWithId>> = T extends AnyObject
83
+ export type Saved<T extends PartialObjectWithId> = T extends AnyObject
79
84
  ? Omit<T, 'id' | 'created' | 'updated'> & SavedDBEntity
80
85
  : T
81
86
 
82
- export type Unsaved<T extends Partial<ObjectWithId>> = T extends AnyObject
87
+ export type Unsaved<T extends PartialObjectWithId> = T extends AnyObject
83
88
  ? Omit<T, 'id' | 'created' | 'updated'> & BaseDBEntity
84
89
  : T
85
90
 
86
- export type UnsavedId<T extends Partial<ObjectWithId>> = Omit<T, 'id'> & {
91
+ export type UnsavedId<T extends PartialObjectWithId> = Omit<T, 'id'> & {
87
92
  id?: T['id']
88
93
  }
89
94