@naturalcycles/js-lib 14.97.1 → 14.98.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.
@@ -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
@@ -5,7 +5,7 @@ import { Merge, Promisable } from './typeFest';
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,13 +24,13 @@ 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> extends CreatedUpdated {
28
28
  id: ID;
29
29
  }
30
- export interface ObjectWithId<ID = string> {
30
+ export interface ObjectWithId<ID extends string | number = string> {
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> extends AnyObject, ObjectWithId<ID> {
34
34
  }
35
35
  /**
36
36
  * Convenience type shorthand.
@@ -139,7 +139,7 @@ export declare type Integer = number;
139
139
  /**
140
140
  * Base interface for any Entity that was saved to DB.
141
141
  */
142
- export interface SavedDBEntity<ID = string> {
142
+ export interface SavedDBEntity<ID extends string | number = string> {
143
143
  id: ID;
144
144
  /**
145
145
  * unixTimestamp of when the entity was first created (in the DB).
@@ -156,9 +156,9 @@ export interface SavedDBEntity<ID = string> {
156
156
  * hence `id`, `created` and `updated` fields CAN BE undefined (yet).
157
157
  * When it's known to be saved - `SavedDBEntity` interface can be used instead.
158
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>>;
159
+ export declare type BaseDBEntity<ID extends string | number = string> = Partial<SavedDBEntity<ID>>;
160
+ export declare type Saved<E, ID extends string | number = string> = Merge<E, SavedDBEntity<ID>>;
161
+ export declare type Unsaved<E, ID extends string | number = string> = Merge<E, BaseDBEntity<ID>>;
162
162
  /**
163
163
  * Named type for JSON.parse / JSON.stringify second argument
164
164
  */
@@ -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.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -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
@@ -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,17 @@ 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> extends CreatedUpdated {
33
33
  id: ID
34
34
  }
35
35
 
36
- export interface ObjectWithId<ID = string> {
36
+ export interface ObjectWithId<ID extends string | number = string> {
37
37
  id: ID
38
38
  }
39
39
 
40
- export interface AnyObjectWithId<ID = string> extends AnyObject, ObjectWithId<ID> {}
40
+ export interface AnyObjectWithId<ID extends string | number = string>
41
+ extends AnyObject,
42
+ ObjectWithId<ID> {}
41
43
 
42
44
  /**
43
45
  * Convenience type shorthand.
@@ -184,7 +186,7 @@ export type Integer = number
184
186
  /**
185
187
  * Base interface for any Entity that was saved to DB.
186
188
  */
187
- export interface SavedDBEntity<ID = string> {
189
+ export interface SavedDBEntity<ID extends string | number = string> {
188
190
  id: ID
189
191
 
190
192
  /**
@@ -204,10 +206,10 @@ export interface SavedDBEntity<ID = string> {
204
206
  * hence `id`, `created` and `updated` fields CAN BE undefined (yet).
205
207
  * When it's known to be saved - `SavedDBEntity` interface can be used instead.
206
208
  */
207
- export type BaseDBEntity<ID = string> = Partial<SavedDBEntity<ID>>
209
+ export type BaseDBEntity<ID extends string | number = string> = Partial<SavedDBEntity<ID>>
208
210
 
209
- export type Saved<E, ID = string> = Merge<E, SavedDBEntity<ID>>
210
- export type Unsaved<E, ID = string> = Merge<E, BaseDBEntity<ID>>
211
+ export type Saved<E, ID extends string | number = string> = Merge<E, SavedDBEntity<ID>>
212
+ export type Unsaved<E, ID extends string | number = string> = Merge<E, BaseDBEntity<ID>>
211
213
 
212
214
  /**
213
215
  * Named type for JSON.parse / JSON.stringify second argument