@naturalcycles/js-lib 14.177.1 → 14.178.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.
@@ -77,7 +77,7 @@ function logFinished(logger, callSignature, started, sma, logResultFn, res, err)
77
77
  const millis = Date.now() - started;
78
78
  const t = ['<<', callSignature, 'took', (0, time_util_1._ms)(millis)];
79
79
  if (sma) {
80
- t.push(`(avg ${(0, time_util_1._ms)(sma.push(millis))})`);
80
+ t.push(`(avg ${(0, time_util_1._ms)(sma.pushGetAvg(millis))})`);
81
81
  }
82
82
  if (err !== undefined) {
83
83
  t.push('ERROR:', err);
@@ -10,14 +10,17 @@ export declare class SimpleMovingAverage {
10
10
  */
11
11
  private nextIndex;
12
12
  /**
13
- * Current average (calculated, cached).
13
+ * Current average (calculated on the fly).
14
14
  * Returns 0 (not undefined) for empty data.
15
15
  */
16
- avg: number;
16
+ get avg(): number;
17
17
  /**
18
18
  * Push new value.
19
19
  * Returns newly calculated average (using newly pushed value).
20
20
  */
21
- push(n: number): number;
22
- private calculateAvg;
21
+ pushGetAvg(n: number): number;
22
+ /**
23
+ * Push new value.
24
+ */
25
+ push(n: number): void;
23
26
  }
package/dist/math/sma.js CHANGED
@@ -12,28 +12,33 @@ class SimpleMovingAverage {
12
12
  * Next index of array to push to
13
13
  */
14
14
  this.nextIndex = 0;
15
- /**
16
- * Current average (calculated, cached).
17
- * Returns 0 (not undefined) for empty data.
18
- */
19
- this.avg = 0;
15
+ }
16
+ /**
17
+ * Current average (calculated on the fly).
18
+ * Returns 0 (not undefined) for empty data.
19
+ */
20
+ get avg() {
21
+ if (this.data.length === 0)
22
+ return 0;
23
+ return this.data.reduce((total, n) => total + n, 0) / this.data.length;
20
24
  }
21
25
  /**
22
26
  * Push new value.
23
27
  * Returns newly calculated average (using newly pushed value).
24
28
  */
29
+ pushGetAvg(n) {
30
+ this.push(n);
31
+ return this.avg;
32
+ }
33
+ /**
34
+ * Push new value.
35
+ */
25
36
  push(n) {
26
37
  this.data[this.nextIndex] = n;
27
38
  this.nextIndex =
28
39
  this.nextIndex === this.size - 1
29
40
  ? 0 // reset
30
41
  : this.nextIndex + 1;
31
- return this.calculateAvg();
32
- }
33
- calculateAvg() {
34
- return (this.avg = this.data.length
35
- ? this.data.reduce((total, n) => total + n, 0) / this.data.length
36
- : 0);
37
42
  }
38
43
  }
39
44
  exports.SimpleMovingAverage = SimpleMovingAverage;
@@ -18,7 +18,7 @@ exports._kb = _kb;
18
18
  */
19
19
  function _hb(b = 0) {
20
20
  if (b < 1024)
21
- return `${Math.round(b)} byte`;
21
+ return `${Math.round(b)} byte(s)`;
22
22
  if (b < 1024 ** 2)
23
23
  return `${(b / 1024).toPrecision(3)} Kb`;
24
24
  if (b < 1024 ** 3)
@@ -73,7 +73,7 @@ function logFinished(logger, callSignature, started, sma, logResultFn, res, err)
73
73
  const millis = Date.now() - started;
74
74
  const t = ['<<', callSignature, 'took', _ms(millis)];
75
75
  if (sma) {
76
- t.push(`(avg ${_ms(sma.push(millis))})`);
76
+ t.push(`(avg ${_ms(sma.pushGetAvg(millis))})`);
77
77
  }
78
78
  if (err !== undefined) {
79
79
  t.push('ERROR:', err);
@@ -79,7 +79,7 @@ export class Fetcher {
79
79
  return this;
80
80
  }
81
81
  static create(cfg = {}) {
82
- return new Fetcher(cfg);
82
+ return new _a(cfg);
83
83
  }
84
84
  // responseType=readableStream
85
85
  /**
@@ -183,7 +183,7 @@ export class Fetcher {
183
183
  }
184
184
  try {
185
185
  // Calls cfg.fetchFn if set, otherwise Fetcher.callNativeFetch
186
- res.fetchResponse = await (this.cfg.fetchFn || Fetcher.callNativeFetch)(req.fullUrl, req.init);
186
+ res.fetchResponse = await (this.cfg.fetchFn || _a.callNativeFetch)(req.fullUrl, req.init);
187
187
  res.ok = res.fetchResponse.ok;
188
188
  // important to set it to undefined, otherwise it can keep the previous value (from previous try)
189
189
  res.err = undefined;
@@ -506,7 +506,7 @@ export class Fetcher {
506
506
  retry: Object.assign({}, defRetryOptions),
507
507
  init: {
508
508
  method: cfg.method || 'GET',
509
- headers: _filterNullishValues(Object.assign({ 'user-agent': Fetcher.userAgent }, cfg.headers)),
509
+ headers: _filterNullishValues(Object.assign({ 'user-agent': _a.userAgent }, cfg.headers)),
510
510
  credentials: cfg.credentials,
511
511
  redirect: cfg.redirect,
512
512
  },
@@ -9,27 +9,32 @@ export class SimpleMovingAverage {
9
9
  * Next index of array to push to
10
10
  */
11
11
  this.nextIndex = 0;
12
- /**
13
- * Current average (calculated, cached).
14
- * Returns 0 (not undefined) for empty data.
15
- */
16
- this.avg = 0;
12
+ }
13
+ /**
14
+ * Current average (calculated on the fly).
15
+ * Returns 0 (not undefined) for empty data.
16
+ */
17
+ get avg() {
18
+ if (this.data.length === 0)
19
+ return 0;
20
+ return this.data.reduce((total, n) => total + n, 0) / this.data.length;
17
21
  }
18
22
  /**
19
23
  * Push new value.
20
24
  * Returns newly calculated average (using newly pushed value).
21
25
  */
26
+ pushGetAvg(n) {
27
+ this.push(n);
28
+ return this.avg;
29
+ }
30
+ /**
31
+ * Push new value.
32
+ */
22
33
  push(n) {
23
34
  this.data[this.nextIndex] = n;
24
35
  this.nextIndex =
25
36
  this.nextIndex === this.size - 1
26
37
  ? 0 // reset
27
38
  : this.nextIndex + 1;
28
- return this.calculateAvg();
29
- }
30
- calculateAvg() {
31
- return (this.avg = this.data.length
32
- ? this.data.reduce((total, n) => total + n, 0) / this.data.length
33
- : 0);
34
39
  }
35
40
  }
@@ -12,7 +12,7 @@ export function _kb(b) {
12
12
  */
13
13
  export function _hb(b = 0) {
14
14
  if (b < 1024)
15
- return `${Math.round(b)} byte`;
15
+ return `${Math.round(b)} byte(s)`;
16
16
  if (b < 1024 ** 2)
17
17
  return `${(b / 1024).toPrecision(3)} Kb`;
18
18
  if (b < 1024 ** 3)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.177.1",
3
+ "version": "14.178.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -151,7 +151,7 @@ function logFinished(
151
151
  const t = ['<<', callSignature, 'took', _ms(millis)]
152
152
 
153
153
  if (sma) {
154
- t.push(`(avg ${_ms(sma.push(millis))})`)
154
+ t.push(`(avg ${_ms(sma.pushGetAvg(millis))})`)
155
155
  }
156
156
 
157
157
  if (err !== undefined) {
package/src/math/sma.ts CHANGED
@@ -13,28 +13,31 @@ export class SimpleMovingAverage {
13
13
  private nextIndex = 0
14
14
 
15
15
  /**
16
- * Current average (calculated, cached).
16
+ * Current average (calculated on the fly).
17
17
  * Returns 0 (not undefined) for empty data.
18
18
  */
19
- avg = 0
19
+ get avg(): number {
20
+ if (this.data.length === 0) return 0
21
+ return this.data.reduce((total, n) => total + n, 0) / this.data.length
22
+ }
20
23
 
21
24
  /**
22
25
  * Push new value.
23
26
  * Returns newly calculated average (using newly pushed value).
24
27
  */
25
- push(n: number): number {
28
+ pushGetAvg(n: number): number {
29
+ this.push(n)
30
+ return this.avg
31
+ }
32
+
33
+ /**
34
+ * Push new value.
35
+ */
36
+ push(n: number): void {
26
37
  this.data[this.nextIndex] = n
27
38
  this.nextIndex =
28
39
  this.nextIndex === this.size - 1
29
40
  ? 0 // reset
30
41
  : this.nextIndex + 1
31
-
32
- return this.calculateAvg()
33
- }
34
-
35
- private calculateAvg(): number {
36
- return (this.avg = this.data.length
37
- ? this.data.reduce((total, n) => total + n, 0) / this.data.length
38
- : 0)
39
42
  }
40
43
  }
@@ -14,7 +14,7 @@ export function _kb(b: number): number {
14
14
  * Byte size to Human byte size string
15
15
  */
16
16
  export function _hb(b = 0): string {
17
- if (b < 1024) return `${Math.round(b)} byte`
17
+ if (b < 1024) return `${Math.round(b)} byte(s)`
18
18
  if (b < 1024 ** 2) return `${(b / 1024).toPrecision(3)} Kb`
19
19
  if (b < 1024 ** 3) return `${(b / 1024 ** 2).toPrecision(3)} Mb`
20
20
  if (b < 1024 ** 4) return `${(b / 1024 ** 3).toPrecision(3)} Gb`