@naturalcycles/js-lib 14.248.0 → 14.250.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.
Files changed (60) hide show
  1. package/dist/array/array.util.js +52 -42
  2. package/dist/datetime/localDate.js +30 -17
  3. package/dist/datetime/localTime.js +31 -18
  4. package/dist/error/assert.js +2 -1
  5. package/dist/http/fetcher.js +5 -5
  6. package/dist/json-schema/jsonSchemaBuilder.js +2 -2
  7. package/dist/math/math.util.js +14 -4
  8. package/dist/math/sma.js +4 -1
  9. package/dist/number/createDeterministicRandom.js +1 -1
  10. package/dist/number/number.util.js +2 -2
  11. package/dist/object/deepEquals.d.ts +2 -2
  12. package/dist/object/deepEquals.js +10 -3
  13. package/dist/object/object.util.d.ts +1 -2
  14. package/dist/object/object.util.js +59 -34
  15. package/dist/promise/pDelay.d.ts +3 -3
  16. package/dist/promise/pDelay.js +1 -1
  17. package/dist/promise/pRetry.js +3 -1
  18. package/dist/semver.js +22 -13
  19. package/dist/string/case.js +15 -6
  20. package/dist/string/lodash/words.js +1 -0
  21. package/dist/string/safeJsonStringify.js +1 -1
  22. package/dist-esm/array/array.util.js +52 -42
  23. package/dist-esm/datetime/localDate.js +30 -17
  24. package/dist-esm/datetime/localTime.js +31 -18
  25. package/dist-esm/error/assert.js +2 -1
  26. package/dist-esm/http/fetcher.js +5 -5
  27. package/dist-esm/json-schema/jsonSchemaBuilder.js +2 -2
  28. package/dist-esm/math/math.util.js +14 -4
  29. package/dist-esm/math/sma.js +4 -1
  30. package/dist-esm/number/createDeterministicRandom.js +1 -1
  31. package/dist-esm/number/number.util.js +2 -2
  32. package/dist-esm/object/deepEquals.js +10 -3
  33. package/dist-esm/object/object.util.js +60 -35
  34. package/dist-esm/promise/pDelay.js +1 -1
  35. package/dist-esm/promise/pRetry.js +3 -1
  36. package/dist-esm/semver.js +22 -13
  37. package/dist-esm/string/case.js +15 -6
  38. package/dist-esm/string/lodash/words.js +1 -0
  39. package/dist-esm/string/safeJsonStringify.js +1 -1
  40. package/package.json +1 -1
  41. package/src/array/array.util.ts +48 -40
  42. package/src/datetime/localDate.ts +34 -19
  43. package/src/datetime/localTime.ts +34 -21
  44. package/src/error/assert.ts +2 -1
  45. package/src/http/fetcher.ts +8 -6
  46. package/src/json-schema/jsonSchemaBuilder.ts +2 -2
  47. package/src/math/math.util.ts +13 -6
  48. package/src/math/sma.ts +3 -1
  49. package/src/number/createDeterministicRandom.ts +1 -1
  50. package/src/number/number.util.ts +4 -2
  51. package/src/object/deepEquals.ts +17 -15
  52. package/src/object/object.util.ts +77 -52
  53. package/src/promise/pDelay.ts +6 -3
  54. package/src/promise/pRetry.ts +3 -1
  55. package/src/semver.ts +22 -13
  56. package/src/string/case.ts +15 -12
  57. package/src/string/leven.ts +1 -1
  58. package/src/string/lodash/words.ts +1 -0
  59. package/src/string/safeJsonStringify.ts +1 -1
  60. package/src/string/string.util.ts +2 -2
@@ -39,7 +39,7 @@ exports._minBy = _minBy;
39
39
  exports._maxByOrUndefined = _maxByOrUndefined;
40
40
  exports._minByOrUndefined = _minByOrUndefined;
41
41
  exports._zip = _zip;
42
- const is_util_1 = require("../is.util");
42
+ const assert_1 = require("../error/assert");
43
43
  const types_1 = require("../types");
44
44
  /**
45
45
  * Creates an array of elements split into groups the length of size. If collection can’t be split evenly, the
@@ -54,9 +54,11 @@ const types_1 = require("../types");
54
54
  * Based on: https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore#_chunk
55
55
  */
56
56
  function _chunk(array, size = 1) {
57
- return array.reduce((arr, item, idx) => {
58
- return idx % size === 0 ? [...arr, [item]] : [...arr.slice(0, -1), [...arr.slice(-1)[0], item]];
59
- }, []);
57
+ const a = [];
58
+ for (let i = 0; i < array.length; i += size) {
59
+ a.push(array.slice(i, i + size));
60
+ }
61
+ return a;
60
62
  }
61
63
  /**
62
64
  * Removes duplicates from given array.
@@ -117,16 +119,13 @@ function _pushUniqBy(a, mapper, ...items) {
117
119
  * Based on: https://stackoverflow.com/a/40808569/4919972
118
120
  */
119
121
  function _uniqBy(arr, mapper) {
120
- return [
121
- ...arr
122
- .reduce((map, item, index) => {
123
- const key = item === null || item === undefined ? item : mapper(item, index);
124
- if (!map.has(key))
125
- map.set(key, item);
126
- return map;
127
- }, new Map())
128
- .values(),
129
- ];
122
+ const map = new Map();
123
+ for (const [i, item] of arr.entries()) {
124
+ const key = item === undefined || item === null ? item : mapper(item, i);
125
+ if (!map.has(key))
126
+ map.set(key, item);
127
+ }
128
+ return [...map.values()];
130
129
  }
131
130
  /**
132
131
  * const a = [
@@ -169,13 +168,14 @@ function _mapBy(items, mapper) {
169
168
  * Returning `undefined` from the Mapper will EXCLUDE the item.
170
169
  */
171
170
  function _groupBy(items, mapper) {
172
- return items.reduce((map, item, index) => {
173
- const res = mapper(item, index);
174
- if (res !== undefined) {
175
- map[res] = [...(map[res] || []), item];
176
- }
177
- return map;
178
- }, {});
171
+ const map = {};
172
+ for (const [i, item] of items.entries()) {
173
+ const key = mapper(item, i);
174
+ if (key === undefined)
175
+ continue;
176
+ (map[key] ||= []).push(item);
177
+ }
178
+ return map;
179
179
  }
180
180
  /**
181
181
  * _sortBy([{age: 20}, {age: 10}], 'age')
@@ -323,7 +323,11 @@ function _intersectsWith(a1, a2) {
323
323
  * // [1]
324
324
  */
325
325
  function _difference(source, ...diffs) {
326
- return diffs.reduce((a, b) => a.filter(c => !b.includes(c)), source);
326
+ let a = source;
327
+ for (const b of diffs) {
328
+ a = a.filter(c => !b.includes(c));
329
+ }
330
+ return a;
327
331
  }
328
332
  /**
329
333
  * Returns the sum of items, or 0 for empty array.
@@ -408,45 +412,51 @@ function _first(array) {
408
412
  return array[0];
409
413
  }
410
414
  function _minOrUndefined(array) {
411
- const a = array.filter(is_util_1._isNotNullish);
412
- if (!a.length)
413
- return;
414
- return a.reduce((min, item) => (min <= item ? min : item));
415
+ let min;
416
+ for (const item of array) {
417
+ if (item === undefined || item === null)
418
+ continue;
419
+ if (min === undefined || item < min) {
420
+ min = item;
421
+ }
422
+ }
423
+ return min;
415
424
  }
416
425
  /**
417
426
  * Filters out nullish values (undefined and null).
418
427
  */
419
428
  function _min(array) {
420
- const a = array.filter(is_util_1._isNotNullish);
421
- if (!a.length)
422
- throw new Error('_min called on empty array');
423
- return a.reduce((min, item) => (min <= item ? min : item));
429
+ const min = _minOrUndefined(array);
430
+ (0, assert_1._assert)(min !== undefined, '_min called on empty array');
431
+ return min;
424
432
  }
425
433
  function _maxOrUndefined(array) {
426
- const a = array.filter(is_util_1._isNotNullish);
427
- if (!a.length)
428
- return;
429
- return a.reduce((max, item) => (max >= item ? max : item));
434
+ let max;
435
+ for (const item of array) {
436
+ if (item === undefined || item === null)
437
+ continue;
438
+ if (max === undefined || item > max) {
439
+ max = item;
440
+ }
441
+ }
442
+ return max;
430
443
  }
431
444
  /**
432
445
  * Filters out nullish values (undefined and null).
433
446
  */
434
447
  function _max(array) {
435
- const a = array.filter(is_util_1._isNotNullish);
436
- if (!a.length)
437
- throw new Error('_max called on empty array');
438
- return a.reduce((max, item) => (max >= item ? max : item));
448
+ const max = _maxOrUndefined(array);
449
+ (0, assert_1._assert)(max !== undefined, '_max called on empty array');
450
+ return max;
439
451
  }
440
452
  function _maxBy(array, mapper) {
441
453
  const max = _maxByOrUndefined(array, mapper);
442
- if (max === undefined)
443
- throw new Error(`_maxBy returned undefined`);
454
+ (0, assert_1._assert)(max !== undefined, '_maxBy returned undefined');
444
455
  return max;
445
456
  }
446
457
  function _minBy(array, mapper) {
447
458
  const min = _minByOrUndefined(array, mapper);
448
- if (min === undefined)
449
- throw new Error(`_minBy returned undefined`);
459
+ (0, assert_1._assert)(min !== undefined, '_minBy returned undefined');
450
460
  return min;
451
461
  }
452
462
  // todo: looks like it _maxByOrUndefined/_minByOrUndefined can be DRYer
@@ -2,7 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.localDate = exports.LocalDate = void 0;
4
4
  const assert_1 = require("../error/assert");
5
- const is_util_1 = require("../is.util");
6
5
  const iterable2_1 = require("../iter/iterable2");
7
6
  const localTime_1 = require("./localTime");
8
7
  const MDAYS = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
@@ -496,7 +495,7 @@ class LocalDateFactory {
496
495
  if (input instanceof LocalDate)
497
496
  return true;
498
497
  if (input instanceof Date)
499
- return !isNaN(input.getDate());
498
+ return !Number.isNaN(input.getDate());
500
499
  return this.isValidString(input);
501
500
  }
502
501
  /**
@@ -516,7 +515,7 @@ class LocalDateFactory {
516
515
  if (input instanceof LocalDate)
517
516
  return input;
518
517
  if (input instanceof Date) {
519
- if (isNaN(input.getDate()))
518
+ if (Number.isNaN(input.getDate()))
520
519
  return;
521
520
  return new LocalDate(input.getFullYear(), input.getMonth() + 1, input.getDate());
522
521
  }
@@ -574,7 +573,7 @@ class LocalDateFactory {
574
573
  * Takes Date as-is, in its timezone - local or UTC.
575
574
  */
576
575
  fromDate(d) {
577
- (0, assert_1._assert)(!isNaN(d.getDate()), `localDate.fromDate is called on Date object that is invalid`);
576
+ (0, assert_1._assert)(!Number.isNaN(d.getDate()), 'localDate.fromDate is called on Date object that is invalid');
578
577
  return new LocalDate(d.getFullYear(), d.getMonth() + 1, d.getDate());
579
578
  }
580
579
  /**
@@ -582,7 +581,7 @@ class LocalDateFactory {
582
581
  * Takes Date's year/month/day components in UTC, using getUTCFullYear, getUTCMonth, getUTCDate.
583
582
  */
584
583
  fromDateInUTC(d) {
585
- (0, assert_1._assert)(!isNaN(d.getDate()), `localDate.fromDateInUTC is called on Date object that is invalid`);
584
+ (0, assert_1._assert)(!Number.isNaN(d.getDate()), 'localDate.fromDateInUTC is called on Date object that is invalid');
586
585
  return new LocalDate(d.getUTCFullYear(), d.getUTCMonth() + 1, d.getUTCDate());
587
586
  }
588
587
  fromDateObject(o) {
@@ -600,35 +599,49 @@ class LocalDateFactory {
600
599
  * Returns the earliest (min) LocalDate from the array, or undefined if the array is empty.
601
600
  */
602
601
  minOrUndefined(items) {
603
- return items.length ? this.min(items) : undefined;
602
+ let min;
603
+ for (const item of items) {
604
+ if (!item)
605
+ continue;
606
+ const ld = this.fromInput(item);
607
+ if (!min || ld.isBefore(min)) {
608
+ min = ld;
609
+ }
610
+ }
611
+ return min;
604
612
  }
605
613
  /**
606
614
  * Returns the earliest LocalDate from the array.
607
615
  * Throws if the array is empty.
608
616
  */
609
617
  min(items) {
610
- const items2 = items.filter(is_util_1._isTruthy);
611
- (0, assert_1._assert)(items2.length, 'localDate.min called on empty array');
612
- return items2
613
- .map(i => this.fromInput(i))
614
- .reduce((min, item) => (min.isSameOrBefore(item) ? min : item));
618
+ const min = this.minOrUndefined(items);
619
+ (0, assert_1._assert)(min, 'localDate.min called on empty array');
620
+ return min;
615
621
  }
616
622
  /**
617
623
  * Returns the latest (max) LocalDate from the array, or undefined if the array is empty.
618
624
  */
619
625
  maxOrUndefined(items) {
620
- return items.length ? this.max(items) : undefined;
626
+ let max;
627
+ for (const item of items) {
628
+ if (!item)
629
+ continue;
630
+ const ld = this.fromInput(item);
631
+ if (!max || ld.isAfter(max)) {
632
+ max = ld;
633
+ }
634
+ }
635
+ return max;
621
636
  }
622
637
  /**
623
638
  * Returns the latest LocalDate from the array.
624
639
  * Throws if the array is empty.
625
640
  */
626
641
  max(items) {
627
- const items2 = items.filter(is_util_1._isTruthy);
628
- (0, assert_1._assert)(items2.length, 'localDate.max called on empty array');
629
- return items2
630
- .map(i => this.fromInput(i))
631
- .reduce((max, item) => (max.isSameOrAfter(item) ? max : item));
642
+ const max = this.maxOrUndefined(items);
643
+ (0, assert_1._assert)(max, 'localDate.max called on empty array');
644
+ return max;
632
645
  }
633
646
  /**
634
647
  * Returns the range (array) of LocalDates between min and max.
@@ -2,7 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.localTime = exports.LocalTime = exports.ISODayOfWeek = void 0;
4
4
  const assert_1 = require("../error/assert");
5
- const is_util_1 = require("../is.util");
6
5
  const time_util_1 = require("../time/time.util");
7
6
  const localDate_1 = require("./localDate");
8
7
  const wallTime_1 = require("./wallTime");
@@ -677,7 +676,7 @@ class LocalTimeFactory {
677
676
  if (input instanceof LocalTime)
678
677
  return true;
679
678
  if (input instanceof Date)
680
- return !isNaN(input.getDate());
679
+ return !Number.isNaN(input.getDate());
681
680
  // We currently don't validate Unixtimestamp input, treat it as always valid
682
681
  if (typeof input === 'number')
683
682
  return true;
@@ -698,7 +697,7 @@ class LocalTimeFactory {
698
697
  if (input instanceof LocalTime)
699
698
  return input;
700
699
  if (input instanceof Date) {
701
- if (isNaN(input.getDate()))
700
+ if (Number.isNaN(input.getDate()))
702
701
  return;
703
702
  return new LocalTime(input);
704
703
  }
@@ -762,7 +761,7 @@ class LocalTimeFactory {
762
761
  return;
763
762
  // Attempt to parse with Date constructor
764
763
  const d = new Date(s);
765
- return isNaN(d.getDate()) ? undefined : d;
764
+ return Number.isNaN(d.getDate()) ? undefined : d;
766
765
  }
767
766
  const o = {
768
767
  year: Number(m[1]),
@@ -790,7 +789,7 @@ class LocalTimeFactory {
790
789
  return hour >= 0 && hour <= 23 && minute >= 0 && minute <= 59 && second >= 0 && second <= 59;
791
790
  }
792
791
  fromDate(date) {
793
- (0, assert_1._assert)(!isNaN(date.getDate()), `localTime.fromDate is called on Date object that is invalid`);
792
+ (0, assert_1._assert)(!Number.isNaN(date.getDate()), 'localTime.fromDate is called on Date object that is invalid');
794
793
  return new LocalTime(date);
795
794
  }
796
795
  fromUnix(ts) {
@@ -845,24 +844,38 @@ class LocalTimeFactory {
845
844
  });
846
845
  }
847
846
  minOrUndefined(items) {
848
- return items.length ? this.min(items) : undefined;
847
+ let min;
848
+ for (const item of items) {
849
+ if (!item)
850
+ continue;
851
+ const lt = this.fromInput(item);
852
+ if (!min || lt.$date.valueOf() < min.$date.valueOf()) {
853
+ min = lt;
854
+ }
855
+ }
856
+ return min;
849
857
  }
850
858
  min(items) {
851
- const items2 = items.filter(is_util_1._isTruthy);
852
- (0, assert_1._assert)(items2.length, 'localTime.min called on empty array');
853
- return items2
854
- .map(i => this.fromInput(i))
855
- .reduce((min, item) => (min.$date.valueOf() <= item.$date.valueOf() ? min : item));
859
+ const min = this.minOrUndefined(items);
860
+ (0, assert_1._assert)(min, 'localTime.min called on empty array');
861
+ return min;
856
862
  }
857
863
  maxOrUndefined(items) {
858
- return items.length ? this.max(items) : undefined;
864
+ let max;
865
+ for (const item of items) {
866
+ if (!item)
867
+ continue;
868
+ const lt = this.fromInput(item);
869
+ if (!max || lt.$date.valueOf() > max.$date.valueOf()) {
870
+ max = lt;
871
+ }
872
+ }
873
+ return max;
859
874
  }
860
875
  max(items) {
861
- const items2 = items.filter(is_util_1._isTruthy);
862
- (0, assert_1._assert)(items2.length, 'localTime.max called on empty array');
863
- return items2
864
- .map(i => this.fromInput(i))
865
- .reduce((max, item) => (max.$date.valueOf() >= item.$date.valueOf() ? max : item));
876
+ const max = this.maxOrUndefined(items);
877
+ (0, assert_1._assert)(max, 'localTime.max called on empty array');
878
+ return max;
866
879
  }
867
880
  }
868
881
  // based on: https://github.com/date-fns/date-fns/blob/master/src/getISOWeek/index.ts
@@ -898,7 +911,7 @@ function getWeekYear(date) {
898
911
  if (date.getTime() >= startOfNextYear.getTime()) {
899
912
  return year + 1;
900
913
  }
901
- else if (date.getTime() >= startOfThisYear.getTime()) {
914
+ if (date.getTime() >= startOfThisYear.getTime()) {
902
915
  return year;
903
916
  }
904
917
  return year - 1;
@@ -60,7 +60,7 @@ function _assertEquals(actual, expected, message, errorData) {
60
60
  function _assertDeepEquals(actual, expected, message, errorData) {
61
61
  if (!(0, __1._deepEquals)(actual, expected)) {
62
62
  const msg = message ||
63
- [`not deeply equal`, `expected: ${(0, __1._stringify)(expected)}`, `got : ${(0, __1._stringify)(actual)}`]
63
+ ['not deeply equal', `expected: ${(0, __1._stringify)(expected)}`, `got : ${(0, __1._stringify)(actual)}`]
64
64
  .filter(Boolean)
65
65
  .join('\n');
66
66
  throw new __1.AssertionError(msg, {
@@ -96,6 +96,7 @@ function _assertIsNumber(v, message) {
96
96
  _assertTypeOf(v, 'number', message);
97
97
  }
98
98
  function _assertTypeOf(v, expectedType, message) {
99
+ // biome-ignore lint/suspicious/useValidTypeof: ok
99
100
  if (typeof v !== expectedType) {
100
101
  const msg = message || `Expected typeof ${expectedType}, actual typeof: ${typeof v}`;
101
102
  throw new __1.AssertionError(msg);
@@ -45,7 +45,7 @@ class Fetcher {
45
45
  static { this.userAgent = (0, env_1.isServerSide)() ? `fetcher${this.VERSION}` : undefined; }
46
46
  constructor(cfg = {}) {
47
47
  if (typeof globalThis.fetch !== 'function') {
48
- throw new TypeError(`globalThis.fetch is not available`);
48
+ throw new TypeError('globalThis.fetch is not available');
49
49
  }
50
50
  this.cfg = this.normalizeCfg(cfg);
51
51
  // Dynamically create all helper methods
@@ -289,7 +289,7 @@ class Fetcher {
289
289
  res.body = res.fetchResponse.body;
290
290
  if (res.body === null) {
291
291
  // Error is to be handled upstream
292
- throw new Error(`fetchResponse.body is null`);
292
+ throw new Error('fetchResponse.body is null');
293
293
  }
294
294
  }
295
295
  res.retryStatus.retryStopped = true;
@@ -418,13 +418,13 @@ class Fetcher {
418
418
  }
419
419
  else {
420
420
  const date = new Date(retryAfterStr);
421
- if (!isNaN(date)) {
421
+ if (!Number.isNaN(date)) {
422
422
  timeout = Number(date) - Date.now();
423
423
  }
424
424
  }
425
425
  this.cfg.logger.log(`retry-after: ${retryAfterStr}`);
426
426
  if (!timeout) {
427
- this.cfg.logger.warn(`retry-after could not be parsed`);
427
+ this.cfg.logger.warn('retry-after could not be parsed');
428
428
  }
429
429
  }
430
430
  }
@@ -577,7 +577,7 @@ class Fetcher {
577
577
  const baseUrl = opt.baseUrl || this.cfg.baseUrl;
578
578
  if (baseUrl) {
579
579
  if (req.fullUrl.startsWith('/')) {
580
- console.warn(`Fetcher: url should not start with / when baseUrl is specified`);
580
+ console.warn('Fetcher: url should not start with / when baseUrl is specified');
581
581
  req.fullUrl = req.fullUrl.slice(1);
582
582
  }
583
583
  req.fullUrl = `${baseUrl}/${req.inputUrl}`;
@@ -144,7 +144,7 @@ class JsonSchemaAnyBuilder {
144
144
  this.schema.optionalField = true;
145
145
  }
146
146
  else {
147
- delete this.schema.optionalField;
147
+ this.schema.optionalField = undefined;
148
148
  }
149
149
  return this;
150
150
  }
@@ -281,7 +281,7 @@ class JsonSchemaObjectBuilder extends JsonSchemaAnyBuilder {
281
281
  this.schema.required.push(k);
282
282
  }
283
283
  else {
284
- delete schema.optionalField;
284
+ schema.optionalField = undefined;
285
285
  }
286
286
  this.schema.properties[k] = schema;
287
287
  });
@@ -6,6 +6,7 @@ exports._averageWeighted = _averageWeighted;
6
6
  exports._percentile = _percentile;
7
7
  exports._percentiles = _percentiles;
8
8
  exports._median = _median;
9
+ const assert_1 = require("../error/assert");
9
10
  const number_util_1 = require("../number/number.util");
10
11
  /**
11
12
  * @returns Average of the array of numbers
@@ -16,20 +17,29 @@ const number_util_1 = require("../number/number.util");
16
17
  * // 2.5
17
18
  */
18
19
  function _average(values) {
19
- return values.reduce((a, b) => a + b) / values.length;
20
+ (0, assert_1._assert)(values.length, '_average is called on empty array');
21
+ let total = 0;
22
+ for (const n of values)
23
+ total += n;
24
+ return total / values.length;
20
25
  }
21
26
  /**
22
27
  * Same as _average, but safely returns null if input array is empty or nullish.
23
28
  */
24
29
  function _averageOrNull(values) {
25
- return values?.length ? values.reduce((a, b) => a + b) / values.length : null;
30
+ return values?.length ? _average(values) : null;
26
31
  }
27
32
  /**
28
33
  * valuesArray and weightsArray length is expected to be the same.
29
34
  */
30
35
  function _averageWeighted(values, weights) {
31
- const numerator = values.map((value, i) => value * weights[i]).reduce((a, b) => a + b);
32
- const denominator = weights.reduce((a, b) => a + b);
36
+ let numerator = 0;
37
+ let denominator = 0;
38
+ // eslint-disable-next-line unicorn/no-for-loop
39
+ for (let i = 0; i < values.length; i++) {
40
+ numerator += values[i] * weights[i];
41
+ denominator += weights[i];
42
+ }
33
43
  return numerator / denominator;
34
44
  }
35
45
  /**
package/dist/math/sma.js CHANGED
@@ -20,7 +20,10 @@ class SimpleMovingAverage {
20
20
  get avg() {
21
21
  if (this.data.length === 0)
22
22
  return 0;
23
- return this.data.reduce((total, n) => total + n, 0) / this.data.length;
23
+ let total = 0;
24
+ for (const n of this.data)
25
+ total += n;
26
+ return total / this.data.length;
24
27
  }
25
28
  /**
26
29
  * Push new value.
@@ -9,7 +9,7 @@ exports._createDeterministicRandom = _createDeterministicRandom;
9
9
  */
10
10
  function _createDeterministicRandom() {
11
11
  let seed = 0x2f6e2b1;
12
- return function () {
12
+ return () => {
13
13
  // Robert Jenkins’ 32 bit integer hash function
14
14
  seed = (seed + 0x7ed55d16 + (seed << 12)) & 0xffffffff;
15
15
  seed = (seed ^ 0xc761c23c ^ (seed >>> 19)) & 0xffffffff;
@@ -47,10 +47,10 @@ function _isBetween(x, min, max, incl = '[)') {
47
47
  if (incl === '[)') {
48
48
  return x >= min && x < max;
49
49
  }
50
- else if (incl === '[]') {
50
+ if (incl === '[]') {
51
51
  return x >= min && x <= max;
52
52
  }
53
- else if (incl === '(]') {
53
+ if (incl === '(]') {
54
54
  return x > min && x <= max;
55
55
  }
56
56
  return x > min && x < max;
@@ -38,7 +38,7 @@
38
38
 
39
39
  TLDR: _deepEquals should be useful in most of the cases, start there.
40
40
  */
41
- export declare function _deepEquals(a: any, b: any): boolean;
41
+ export declare function _deepEquals<T>(a: T, b: T): boolean;
42
42
  /**
43
43
  Returns true if a and b are deeply equal.
44
44
 
@@ -53,7 +53,7 @@ export declare function _deepEquals(a: any, b: any): boolean;
53
53
 
54
54
  See _deepEquals docs for more details and comparison.
55
55
  */
56
- export declare function _deepJsonEquals(a: any, b: any): boolean;
56
+ export declare function _deepJsonEquals<T>(a: T, b: T): boolean;
57
57
  /**
58
58
  * Shortcut for JSON.stringify(a) === JSON.stringify(b)
59
59
  *
@@ -55,7 +55,7 @@ function _deepEquals(a, b) {
55
55
  return false;
56
56
  if (Array.isArray(a)) {
57
57
  const length = a.length;
58
- if (length !== b.length)
58
+ if (!Array.isArray(b) || length !== b.length)
59
59
  return false;
60
60
  for (let i = length; i-- !== 0;) {
61
61
  if (!_deepEquals(a[i], b[i]))
@@ -79,8 +79,9 @@ function _deepEquals(a, b) {
79
79
  }
80
80
  return true;
81
81
  }
82
- if (a.constructor === RegExp)
82
+ if (a.constructor === RegExp) {
83
83
  return a.source === b.source && a.flags === b.flags;
84
+ }
84
85
  if (a.valueOf !== Object.prototype.valueOf)
85
86
  return a.valueOf() === b.valueOf();
86
87
  if (a.toString !== Object.prototype.toString)
@@ -111,13 +112,16 @@ function _deepJsonEquals(a, b) {
111
112
  if (a === b)
112
113
  return true;
113
114
  if (Number.isNaN(a)) {
115
+ ;
114
116
  a = null;
115
117
  }
116
118
  else if (typeof a === 'function') {
119
+ ;
117
120
  a = undefined;
118
121
  }
119
122
  else if (a && typeof a === 'object') {
120
123
  if (a instanceof Date) {
124
+ ;
121
125
  a = a.valueOf();
122
126
  }
123
127
  else if ('toJSON' in a) {
@@ -125,13 +129,16 @@ function _deepJsonEquals(a, b) {
125
129
  }
126
130
  }
127
131
  if (Number.isNaN(b)) {
132
+ ;
128
133
  b = null;
129
134
  }
130
135
  else if (typeof b === 'function') {
136
+ ;
131
137
  b = undefined;
132
138
  }
133
139
  else if (b && typeof b === 'object') {
134
140
  if (b instanceof Date) {
141
+ ;
135
142
  b = b.valueOf();
136
143
  }
137
144
  else if ('toJSON' in b) {
@@ -141,7 +148,7 @@ function _deepJsonEquals(a, b) {
141
148
  if (a && b && typeof a === 'object' && typeof b === 'object') {
142
149
  if (Array.isArray(a)) {
143
150
  const length = a.length;
144
- if (length !== b.length)
151
+ if (!Array.isArray(b) || length !== b.length)
145
152
  return false;
146
153
  for (let i = length; i-- !== 0;) {
147
154
  if (!_deepJsonEquals(a[i], b[i]))
@@ -1,5 +1,4 @@
1
- import type { AnyObject, ObjectMapper, ObjectPredicate, ValueOf } from '../types';
2
- import { KeyValueTuple, Reviver, SKIP } from '../types';
1
+ import { AnyObject, KeyValueTuple, ObjectMapper, ObjectPredicate, Reviver, SKIP, ValueOf } from '../types';
3
2
  /**
4
3
  * Returns clone of `obj` with only `props` preserved.
5
4
  * Opposite of Omit.