@naturalcycles/js-lib 14.97.1 → 14.98.2

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/index.d.ts CHANGED
@@ -51,7 +51,7 @@ export * from './string/string.util';
51
51
  import { JsonStringifyFunction, StringifyAnyOptions, _stringifyAny } from './string/stringifyAny';
52
52
  export * from './time/time.util';
53
53
  import { Class, ConditionalExcept, ConditionalPick, Merge, Promisable, ReadonlyDeep, Simplify } from './typeFest';
54
- import { AsyncMapper, AsyncPredicate, BaseDBEntity, CreatedUpdated, CreatedUpdatedId, ObjectWithId, AnyObjectWithId, Saved, Unsaved, BatchResult, InstanceId, IsoDate, IsoDateString, IsoDateTimeString, KeyValueTuple, Mapper, ObjectMapper, ObjectPredicate, Predicate, PromiseMap, AnyObject, AnyFunction, Reviver, SavedDBEntity, StringMap, UnixTimestampNumber, UnixTimestamp, Integer, ValueOf, ValuesOf, AbortableMapper, AbortableAsyncPredicate, AbortableAsyncMapper, AbortablePredicate, END, SKIP, _noop, _objectKeys, _passNothingPredicate, _passthroughMapper, _passthroughPredicate, _passUndefinedMapper, _stringMapEntries, _stringMapValues } from './types';
54
+ import { AsyncMapper, AsyncPredicate, BaseDBEntity, CreatedUpdated, CreatedUpdatedId, ObjectWithId, AnyObjectWithId, Saved, Unsaved, UnsavedId, BatchResult, InstanceId, IsoDate, IsoDateString, IsoDateTimeString, KeyValueTuple, Mapper, ObjectMapper, ObjectPredicate, Predicate, PromiseMap, AnyObject, AnyFunction, Reviver, SavedDBEntity, StringMap, UnixTimestampNumber, UnixTimestamp, Integer, ValueOf, ValuesOf, AbortableMapper, AbortableAsyncPredicate, AbortableAsyncMapper, AbortablePredicate, END, SKIP, _noop, _objectKeys, _passNothingPredicate, _passthroughMapper, _passthroughPredicate, _passUndefinedMapper, _stringMapEntries, _stringMapValues } from './types';
55
55
  export * from './unit/size.util';
56
56
  import { is } from './vendor/is';
57
57
  import { CommonLogLevel, CommonLogFunction, CommonLogger, commonLoggerMinLevel, commonLoggerNoop, commonLogLevelNumber, commonLoggerPipe, commonLoggerPrefix, CommonLogWithLevelFunction, commonLoggerCreate } from './log/commonLogger';
@@ -68,5 +68,5 @@ import { LocalDateConfig, LocalDateUnit, Inclusiveness } from './datetime/localD
68
68
  import { LocalTimeConfig, LocalTimeUnit, LocalTimeComponents } from './datetime/localTime';
69
69
  import { DateIntervalConfig, DateIntervalString } from './datetime/dateInterval';
70
70
  import { TimeIntervalConfig, TimeIntervalString } from './datetime/timeInterval';
71
- export type { DateIntervalConfig, DateIntervalString, TimeIntervalConfig, TimeIntervalString, LocalDateConfig, LocalDateUnit, Inclusiveness, LocalTimeConfig, LocalTimeUnit, LocalTimeComponents, AbortableMapper, AbortablePredicate, AbortableAsyncPredicate, AbortableAsyncMapper, PQueueCfg, MemoCache, AsyncMemoCache, PromiseDecoratorCfg, PromiseDecoratorResp, ErrorData, ErrorObject, HttpErrorData, HttpErrorResponse, Admin401ErrorData, Admin403ErrorData, StringMap, PromiseMap, AnyObject, AnyFunction, ValuesOf, ValueOf, KeyValueTuple, ObjectMapper, ObjectPredicate, InstanceId, IsoDate, IsoDateString, IsoDateTimeString, Reviver, PMapOptions, Mapper, AsyncMapper, Predicate, AsyncPredicate, BatchResult, DeferredPromise, PRetryOptions, PTimeoutOptions, TryCatchOptions, StringifyAnyOptions, JsonStringifyFunction, Merge, ReadonlyDeep, Promisable, Simplify, ConditionalPick, ConditionalExcept, Class, UnixTimestampNumber, UnixTimestamp, Integer, BaseDBEntity, SavedDBEntity, Saved, Unsaved, CreatedUpdated, CreatedUpdatedId, ObjectWithId, AnyObjectWithId, JsonSchema, JsonSchemaAny, JsonSchemaOneOf, JsonSchemaAllOf, JsonSchemaAnyOf, JsonSchemaNot, JsonSchemaRef, JsonSchemaConst, JsonSchemaEnum, JsonSchemaString, JsonSchemaNumber, JsonSchemaBoolean, JsonSchemaNull, JsonSchemaRootObject, JsonSchemaObject, JsonSchemaArray, JsonSchemaTuple, JsonSchemaBuilder, CommonLogLevel, CommonLogWithLevelFunction, CommonLogFunction, CommonLogger, };
71
+ export type { DateIntervalConfig, DateIntervalString, TimeIntervalConfig, TimeIntervalString, LocalDateConfig, LocalDateUnit, Inclusiveness, LocalTimeConfig, LocalTimeUnit, LocalTimeComponents, AbortableMapper, AbortablePredicate, AbortableAsyncPredicate, AbortableAsyncMapper, PQueueCfg, MemoCache, AsyncMemoCache, PromiseDecoratorCfg, PromiseDecoratorResp, ErrorData, ErrorObject, HttpErrorData, HttpErrorResponse, Admin401ErrorData, Admin403ErrorData, StringMap, PromiseMap, AnyObject, AnyFunction, ValuesOf, ValueOf, KeyValueTuple, ObjectMapper, ObjectPredicate, InstanceId, IsoDate, IsoDateString, IsoDateTimeString, Reviver, PMapOptions, Mapper, AsyncMapper, Predicate, AsyncPredicate, BatchResult, DeferredPromise, PRetryOptions, PTimeoutOptions, TryCatchOptions, StringifyAnyOptions, JsonStringifyFunction, Merge, ReadonlyDeep, Promisable, Simplify, ConditionalPick, ConditionalExcept, Class, UnixTimestampNumber, UnixTimestamp, Integer, BaseDBEntity, SavedDBEntity, Saved, Unsaved, UnsavedId, CreatedUpdated, CreatedUpdatedId, ObjectWithId, AnyObjectWithId, JsonSchema, JsonSchemaAny, JsonSchemaOneOf, JsonSchemaAllOf, JsonSchemaAnyOf, JsonSchemaNot, JsonSchemaRef, JsonSchemaConst, JsonSchemaEnum, JsonSchemaString, JsonSchemaNumber, JsonSchemaBoolean, JsonSchemaNull, JsonSchemaRootObject, JsonSchemaObject, JsonSchemaArray, JsonSchemaTuple, JsonSchemaBuilder, CommonLogLevel, CommonLogWithLevelFunction, CommonLogFunction, CommonLogger, };
72
72
  export { is, _createPromiseDecorator, _stringMapValues, _stringMapEntries, _objectKeys, pMap, _passthroughMapper, _passUndefinedMapper, _passthroughPredicate, _passNothingPredicate, _noop, ErrorMode, pDefer, AggregatedError, pRetry, pRetryFn, pTimeout, pTimeoutFn, _tryCatch, _TryCatch, _stringifyAny, jsonSchema, JsonSchemaAnyBuilder, commonLoggerMinLevel, commonLoggerNoop, commonLogLevelNumber, commonLoggerPipe, commonLoggerPrefix, commonLoggerCreate, PQueue, END, SKIP, };
@@ -113,8 +113,8 @@ export declare class JsonSchemaObjectBuilder<T extends AnyObject> extends JsonSc
113
113
  minProps(minProperties: number): this;
114
114
  maxProps(maxProperties: number): this;
115
115
  additionalProps(additionalProperties: boolean): this;
116
- baseDBEntity<ID = string>(idType?: string): JsonSchemaObjectBuilder<T & BaseDBEntity<ID>>;
117
- savedDBEntity<ID = string>(idType?: string): JsonSchemaObjectBuilder<T & SavedDBEntity<ID>>;
116
+ baseDBEntity<ID extends string | number = string>(idType?: string): JsonSchemaObjectBuilder<T & BaseDBEntity<ID>>;
117
+ savedDBEntity<ID extends string | number = string>(idType?: string): JsonSchemaObjectBuilder<T & SavedDBEntity<ID>>;
118
118
  extend<T2 extends AnyObject>(s2: JsonSchemaObjectBuilder<T2>): JsonSchemaObjectBuilder<T & T2>;
119
119
  }
120
120
  export declare class JsonSchemaArrayBuilder<ITEM> extends JsonSchemaAnyBuilder<ITEM[], JsonSchemaArray<ITEM>> {
@@ -7,6 +7,10 @@
7
7
  * // 2.5
8
8
  */
9
9
  export declare function _average(values: number[]): number;
10
+ /**
11
+ * Same as _average, but safely returns null if input array is empty or nullish.
12
+ */
13
+ export declare function _averageOrNull(values: number[] | undefined | null): number | null;
10
14
  /**
11
15
  * valuesArray and weightsArray length is expected to be the same.
12
16
  */
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports._median = exports._percentiles = exports._percentile = exports._averageWeighted = exports._average = void 0;
3
+ exports._median = exports._percentiles = exports._percentile = exports._averageWeighted = exports._averageOrNull = exports._average = void 0;
4
4
  const number_util_1 = require("../number/number.util");
5
5
  /**
6
6
  * @returns Average of the array of numbers
@@ -14,6 +14,13 @@ function _average(values) {
14
14
  return values.reduce((a, b) => a + b) / values.length;
15
15
  }
16
16
  exports._average = _average;
17
+ /**
18
+ * Same as _average, but safely returns null if input array is empty or nullish.
19
+ */
20
+ function _averageOrNull(values) {
21
+ return values?.length ? values.reduce((a, b) => a + b) / values.length : null;
22
+ }
23
+ exports._averageOrNull = _averageOrNull;
17
24
  /**
18
25
  * valuesArray and weightsArray length is expected to be the same.
19
26
  */
@@ -14,6 +14,10 @@ export declare class SimpleMovingAverage {
14
14
  * Returns 0 (not undefined) for empty data.
15
15
  */
16
16
  avg: number;
17
+ /**
18
+ * Push new value.
19
+ * Returns newly calculated average (using newly pushed value).
20
+ */
17
21
  push(n: number): number;
18
22
  private calculateAvg;
19
23
  }
package/dist/math/sma.js CHANGED
@@ -18,6 +18,10 @@ class SimpleMovingAverage {
18
18
  */
19
19
  this.avg = 0;
20
20
  }
21
+ /**
22
+ * Push new value.
23
+ * Returns newly calculated average (using newly pushed value).
24
+ */
21
25
  push(n) {
22
26
  this.data[this.nextIndex] = n;
23
27
  this.nextIndex =
package/dist/types.d.ts CHANGED
@@ -1,11 +1,11 @@
1
- import { Merge, Promisable } from './typeFest';
1
+ import { Except, Merge, Promisable } from './typeFest';
2
2
  /**
3
3
  * Map from String to String (or <T>).
4
4
  *
5
5
  * Alternative: Record<string, T | undefined>
6
6
  */
7
7
  export interface StringMap<T = string> {
8
- [k: string]: T | undefined;
8
+ [k: string | number]: T | undefined;
9
9
  }
10
10
  /**
11
11
  * Object to be passed to pProps to resolve all promises into properties.
@@ -24,14 +24,40 @@ export interface CreatedUpdated {
24
24
  created: number;
25
25
  updated: number;
26
26
  }
27
- export interface CreatedUpdatedId<ID = string> extends CreatedUpdated {
27
+ export interface CreatedUpdatedId<ID extends string | number = string | number> extends CreatedUpdated {
28
28
  id: ID;
29
29
  }
30
- export interface ObjectWithId<ID = string> {
30
+ export interface ObjectWithId<ID extends string | number = string | number> {
31
31
  id: ID;
32
32
  }
33
- export interface AnyObjectWithId<ID = string> extends AnyObject, ObjectWithId<ID> {
33
+ export interface AnyObjectWithId<ID extends string | number = string | number> extends AnyObject, ObjectWithId<ID> {
34
34
  }
35
+ /**
36
+ * Base interface for any Entity that was saved to DB.
37
+ */
38
+ export interface SavedDBEntity<ID extends string | number = string> {
39
+ id: ID;
40
+ /**
41
+ * unixTimestamp of when the entity was first created (in the DB).
42
+ */
43
+ created: UnixTimestampNumber;
44
+ /**
45
+ * unixTimestamp of when the entity was last updated (in the DB).
46
+ */
47
+ updated: UnixTimestampNumber;
48
+ }
49
+ /**
50
+ * Base interface for any Entity that can be saved to DB.
51
+ * This interface fits when entity was NOT YET saved to DB,
52
+ * hence `id`, `created` and `updated` fields CAN BE undefined (yet).
53
+ * When it's known to be saved - `SavedDBEntity` interface can be used instead.
54
+ */
55
+ export declare type BaseDBEntity<ID extends string | number = string> = Partial<SavedDBEntity<ID>>;
56
+ export declare type Saved<T extends Partial<ObjectWithId>> = Merge<T, SavedDBEntity<Exclude<T['id'], undefined>>>;
57
+ export declare type Unsaved<T extends Partial<ObjectWithId>> = Merge<T, BaseDBEntity<Exclude<T['id'], undefined>>>;
58
+ export declare type UnsavedId<T extends Partial<ObjectWithId>> = Except<T, 'id'> & {
59
+ id: Exclude<T['id'], undefined>;
60
+ };
35
61
  /**
36
62
  * Convenience type shorthand.
37
63
  * Because `Function` type is discouraged by eslint.
@@ -136,29 +162,6 @@ export declare type UnixTimestamp = number;
136
162
  * Same as `number`, but with semantic meaning that it's an Integer.
137
163
  */
138
164
  export declare type Integer = number;
139
- /**
140
- * Base interface for any Entity that was saved to DB.
141
- */
142
- export interface SavedDBEntity<ID = string> {
143
- id: ID;
144
- /**
145
- * unixTimestamp of when the entity was first created (in the DB).
146
- */
147
- created: UnixTimestampNumber;
148
- /**
149
- * unixTimestamp of when the entity was last updated (in the DB).
150
- */
151
- updated: UnixTimestampNumber;
152
- }
153
- /**
154
- * Base interface for any Entity that can be saved to DB.
155
- * This interface fits when entity was NOT YET saved to DB,
156
- * hence `id`, `created` and `updated` fields CAN BE undefined (yet).
157
- * When it's known to be saved - `SavedDBEntity` interface can be used instead.
158
- */
159
- export declare type BaseDBEntity<ID = string> = Partial<SavedDBEntity<ID>>;
160
- export declare type Saved<E, ID = string> = Merge<E, SavedDBEntity<ID>>;
161
- export declare type Unsaved<E, ID = string> = Merge<E, BaseDBEntity<ID>>;
162
165
  /**
163
166
  * Named type for JSON.parse / JSON.stringify second argument
164
167
  */
@@ -10,6 +10,12 @@ import { _sortNumbers } from '../number/number.util';
10
10
  export function _average(values) {
11
11
  return values.reduce((a, b) => a + b) / values.length;
12
12
  }
13
+ /**
14
+ * Same as _average, but safely returns null if input array is empty or nullish.
15
+ */
16
+ export function _averageOrNull(values) {
17
+ return (values === null || values === void 0 ? void 0 : values.length) ? values.reduce((a, b) => a + b) / values.length : null;
18
+ }
13
19
  /**
14
20
  * valuesArray and weightsArray length is expected to be the same.
15
21
  */
@@ -15,6 +15,10 @@ export class SimpleMovingAverage {
15
15
  */
16
16
  this.avg = 0;
17
17
  }
18
+ /**
19
+ * Push new value.
20
+ * Returns newly calculated average (using newly pushed value).
21
+ */
18
22
  push(n) {
19
23
  this.data[this.nextIndex] = n;
20
24
  this.nextIndex =
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.97.1",
3
+ "version": "14.98.2",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
package/src/index.ts CHANGED
@@ -102,6 +102,7 @@ import {
102
102
  AnyObjectWithId,
103
103
  Saved,
104
104
  Unsaved,
105
+ UnsavedId,
105
106
  BatchResult,
106
107
  InstanceId,
107
108
  IsoDate,
@@ -232,6 +233,7 @@ export type {
232
233
  SavedDBEntity,
233
234
  Saved,
234
235
  Unsaved,
236
+ UnsavedId,
235
237
  CreatedUpdated,
236
238
  CreatedUpdatedId,
237
239
  ObjectWithId,
@@ -365,7 +365,9 @@ export class JsonSchemaObjectBuilder<T extends AnyObject> extends JsonSchemaAnyB
365
365
  return this
366
366
  }
367
367
 
368
- baseDBEntity<ID = string>(idType = 'string'): JsonSchemaObjectBuilder<T & BaseDBEntity<ID>> {
368
+ baseDBEntity<ID extends string | number = string>(
369
+ idType = 'string',
370
+ ): JsonSchemaObjectBuilder<T & BaseDBEntity<ID>> {
369
371
  Object.assign(this.schema.properties, {
370
372
  id: { type: idType },
371
373
  created: { type: 'number', format: 'unixTimestamp' },
@@ -375,7 +377,9 @@ export class JsonSchemaObjectBuilder<T extends AnyObject> extends JsonSchemaAnyB
375
377
  return this
376
378
  }
377
379
 
378
- savedDBEntity<ID = string>(idType = 'string'): JsonSchemaObjectBuilder<T & SavedDBEntity<ID>> {
380
+ savedDBEntity<ID extends string | number = string>(
381
+ idType = 'string',
382
+ ): JsonSchemaObjectBuilder<T & SavedDBEntity<ID>> {
379
383
  return this.baseDBEntity(idType).addRequired(['id', 'created', 'updated']) as any
380
384
  }
381
385
 
@@ -12,6 +12,13 @@ export function _average(values: number[]): number {
12
12
  return values.reduce((a, b) => a + b) / values.length
13
13
  }
14
14
 
15
+ /**
16
+ * Same as _average, but safely returns null if input array is empty or nullish.
17
+ */
18
+ export function _averageOrNull(values: number[] | undefined | null): number | null {
19
+ return values?.length ? values.reduce((a, b) => a + b) / values.length : null
20
+ }
21
+
15
22
  /**
16
23
  * valuesArray and weightsArray length is expected to be the same.
17
24
  */
package/src/math/sma.ts CHANGED
@@ -15,6 +15,10 @@ export class SimpleMovingAverage {
15
15
  */
16
16
  avg = 0
17
17
 
18
+ /**
19
+ * Push new value.
20
+ * Returns newly calculated average (using newly pushed value).
21
+ */
18
22
  push(n: number): number {
19
23
  this.data[this.nextIndex] = n
20
24
  this.nextIndex =
package/src/types.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Merge, Promisable } from './typeFest'
1
+ import { Except, Merge, Promisable } from './typeFest'
2
2
 
3
3
  /**
4
4
  * Map from String to String (or <T>).
@@ -6,7 +6,7 @@ import { Merge, Promisable } from './typeFest'
6
6
  * Alternative: Record<string, T | undefined>
7
7
  */
8
8
  export interface StringMap<T = string> {
9
- [k: string]: T | undefined
9
+ [k: string | number]: T | undefined
10
10
  }
11
11
 
12
12
  /**
@@ -29,15 +29,57 @@ export interface CreatedUpdated {
29
29
  updated: number
30
30
  }
31
31
 
32
- export interface CreatedUpdatedId<ID = string> extends CreatedUpdated {
32
+ export interface CreatedUpdatedId<ID extends string | number = string | number>
33
+ extends CreatedUpdated {
33
34
  id: ID
34
35
  }
35
36
 
36
- export interface ObjectWithId<ID = string> {
37
+ export interface ObjectWithId<ID extends string | number = string | number> {
37
38
  id: ID
38
39
  }
39
40
 
40
- export interface AnyObjectWithId<ID = string> extends AnyObject, ObjectWithId<ID> {}
41
+ export interface AnyObjectWithId<ID extends string | number = string | number>
42
+ extends AnyObject,
43
+ ObjectWithId<ID> {}
44
+
45
+ /**
46
+ * Base interface for any Entity that was saved to DB.
47
+ */
48
+ export interface SavedDBEntity<ID extends string | number = string> {
49
+ id: ID
50
+
51
+ /**
52
+ * unixTimestamp of when the entity was first created (in the DB).
53
+ */
54
+ created: UnixTimestampNumber
55
+
56
+ /**
57
+ * unixTimestamp of when the entity was last updated (in the DB).
58
+ */
59
+ updated: UnixTimestampNumber
60
+ }
61
+
62
+ /**
63
+ * Base interface for any Entity that can be saved to DB.
64
+ * This interface fits when entity was NOT YET saved to DB,
65
+ * hence `id`, `created` and `updated` fields CAN BE undefined (yet).
66
+ * When it's known to be saved - `SavedDBEntity` interface can be used instead.
67
+ */
68
+ export type BaseDBEntity<ID extends string | number = string> = Partial<SavedDBEntity<ID>>
69
+
70
+ export type Saved<T extends Partial<ObjectWithId>> = Merge<
71
+ T,
72
+ SavedDBEntity<Exclude<T['id'], undefined>>
73
+ >
74
+
75
+ export type Unsaved<T extends Partial<ObjectWithId>> = Merge<
76
+ T,
77
+ BaseDBEntity<Exclude<T['id'], undefined>>
78
+ >
79
+
80
+ export type UnsavedId<T extends Partial<ObjectWithId>> = Except<T, 'id'> & {
81
+ id: Exclude<T['id'], undefined>
82
+ }
41
83
 
42
84
  /**
43
85
  * Convenience type shorthand.
@@ -181,34 +223,6 @@ export type UnixTimestamp = number
181
223
  */
182
224
  export type Integer = number
183
225
 
184
- /**
185
- * Base interface for any Entity that was saved to DB.
186
- */
187
- export interface SavedDBEntity<ID = string> {
188
- id: ID
189
-
190
- /**
191
- * unixTimestamp of when the entity was first created (in the DB).
192
- */
193
- created: UnixTimestampNumber
194
-
195
- /**
196
- * unixTimestamp of when the entity was last updated (in the DB).
197
- */
198
- updated: UnixTimestampNumber
199
- }
200
-
201
- /**
202
- * Base interface for any Entity that can be saved to DB.
203
- * This interface fits when entity was NOT YET saved to DB,
204
- * hence `id`, `created` and `updated` fields CAN BE undefined (yet).
205
- * When it's known to be saved - `SavedDBEntity` interface can be used instead.
206
- */
207
- export type BaseDBEntity<ID = string> = Partial<SavedDBEntity<ID>>
208
-
209
- export type Saved<E, ID = string> = Merge<E, SavedDBEntity<ID>>
210
- export type Unsaved<E, ID = string> = Merge<E, BaseDBEntity<ID>>
211
-
212
226
  /**
213
227
  * Named type for JSON.parse / JSON.stringify second argument
214
228
  */