@naturalcycles/js-lib 14.97.0 → 14.98.1

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,5 +1,5 @@
1
- import { LocalDate, LocalDateConfig } from './localDate';
2
- export declare type DateIntervalConfig = DateInterval | string;
1
+ import { Inclusiveness, LocalDate, LocalDateConfig, LocalDateUnit } from './localDate';
2
+ export declare type DateIntervalConfig = DateInterval | DateIntervalString;
3
3
  export declare type DateIntervalString = string;
4
4
  /**
5
5
  * Class that supports ISO8601 "Time interval" standard that looks like `2022-02-24/2022-03-30`.
@@ -16,24 +16,25 @@ export declare class DateInterval {
16
16
  */
17
17
  static parse(d: DateIntervalConfig): DateInterval;
18
18
  isSame(d: DateIntervalConfig): boolean;
19
- isBefore(d: DateIntervalConfig): boolean;
19
+ isBefore(d: DateIntervalConfig, inclusive?: boolean): boolean;
20
20
  isSameOrBefore(d: DateIntervalConfig): boolean;
21
- isAfter(d: DateIntervalConfig): boolean;
21
+ isAfter(d: DateIntervalConfig, inclusive?: boolean): boolean;
22
22
  isSameOrAfter(d: DateIntervalConfig): boolean;
23
23
  /**
24
24
  * Ranges of DateInterval (start, end) are INCLUSIVE.
25
25
  */
26
- includes(d: LocalDateConfig): boolean;
26
+ includes(d: LocalDateConfig, incl?: Inclusiveness): boolean;
27
+ intersects(int: DateIntervalConfig, inclusive?: boolean): boolean;
27
28
  /**
28
29
  * DateIntervals compare by start date.
29
30
  * If it's the same - then by end date.
30
31
  */
31
32
  cmp(d: DateIntervalConfig): -1 | 0 | 1;
33
+ getDays(incl?: Inclusiveness): LocalDate[];
32
34
  /**
33
35
  * Returns an array of LocalDates that are included in the interval.
34
- * Ranges are INCLUSIVE.
35
36
  */
36
- getDays(): LocalDate[];
37
+ range(incl?: Inclusiveness, step?: number, stepUnit?: LocalDateUnit): LocalDate[];
37
38
  toString(): DateIntervalString;
38
39
  toJSON(): DateIntervalString;
39
40
  }
@@ -30,14 +30,16 @@ class DateInterval {
30
30
  isSame(d) {
31
31
  return this.cmp(d) === 0;
32
32
  }
33
- isBefore(d) {
34
- return this.cmp(d) === -1;
33
+ isBefore(d, inclusive = false) {
34
+ const r = this.cmp(d);
35
+ return r === -1 || (r === 0 && inclusive);
35
36
  }
36
37
  isSameOrBefore(d) {
37
38
  return this.cmp(d) <= 0;
38
39
  }
39
- isAfter(d) {
40
- return this.cmp(d) === 1;
40
+ isAfter(d, inclusive = false) {
41
+ const r = this.cmp(d);
42
+ return r === 1 || (r === 0 && inclusive);
41
43
  }
42
44
  isSameOrAfter(d) {
43
45
  return this.cmp(d) >= 0;
@@ -45,9 +47,14 @@ class DateInterval {
45
47
  /**
46
48
  * Ranges of DateInterval (start, end) are INCLUSIVE.
47
49
  */
48
- includes(d) {
50
+ includes(d, incl = '[]') {
49
51
  d = localDate_1.LocalDate.of(d);
50
- return d.isSameOrAfter(this.start) && d.isSameOrBefore(this.end);
52
+ return d.isAfter(this.start, incl[0] === '[') && d.isBefore(this.end, incl[1] === ']');
53
+ }
54
+ intersects(int, inclusive = true) {
55
+ const $int = DateInterval.parse(int);
56
+ const incl = inclusive ? '[]' : '()';
57
+ return this.includes($int.start, incl) || this.includes($int.end, incl);
51
58
  }
52
59
  /**
53
60
  * DateIntervals compare by start date.
@@ -57,18 +64,14 @@ class DateInterval {
57
64
  d = DateInterval.parse(d);
58
65
  return this.start.cmp(d.start) || this.end.cmp(d.end);
59
66
  }
67
+ getDays(incl = '[]') {
68
+ return localDate_1.LocalDate.range(this.start, this.end, incl, 1, 'day');
69
+ }
60
70
  /**
61
71
  * Returns an array of LocalDates that are included in the interval.
62
- * Ranges are INCLUSIVE.
63
72
  */
64
- getDays() {
65
- const days = [];
66
- let current = this.start;
67
- do {
68
- days.push(current);
69
- current = current.add(1, 'day');
70
- } while (current.isSameOrBefore(this.end));
71
- return days;
73
+ range(incl = '[]', step = 1, stepUnit = 'day') {
74
+ return localDate_1.LocalDate.range(this.start, this.end, incl, step, stepUnit);
72
75
  }
73
76
  toString() {
74
77
  return [this.start, this.end].join('/');
@@ -1,9 +1,8 @@
1
- import { Sequence } from '../seq/seq';
2
1
  import { IsoDateString, UnixTimestampNumber } from '../types';
3
2
  import { LocalTime } from './localTime';
4
3
  export declare type LocalDateUnit = 'year' | 'month' | 'day';
5
4
  export declare type Inclusiveness = '()' | '[]' | '[)' | '(]';
6
- export declare type LocalDateConfig = LocalDate | string;
5
+ export declare type LocalDateConfig = LocalDate | IsoDateString;
7
6
  /**
8
7
  * @experimental
9
8
  */
@@ -33,11 +32,7 @@ export declare class LocalDate {
33
32
  static earliest(items: LocalDate[]): LocalDate;
34
33
  static latestOrUndefined(items: LocalDate[]): LocalDate | undefined;
35
34
  static latest(items: LocalDate[]): LocalDate;
36
- static range(minIncl: LocalDateConfig, maxExcl: LocalDateConfig, step?: number, stepUnit?: LocalDateUnit): LocalDate[];
37
- static rangeSeq(minIncl: LocalDateConfig, maxExcl: LocalDateConfig, step?: number, stepUnit?: LocalDateUnit): Sequence<LocalDate>;
38
- static rangeString(minIncl: LocalDateConfig, maxExcl: LocalDateConfig, step?: number, stepUnit?: LocalDateUnit): IsoDateString[];
39
- static rangeIncl(minIncl: LocalDateConfig, maxIncl: LocalDateConfig, step?: number, stepUnit?: LocalDateUnit): LocalDate[];
40
- static rangeInclString(minIncl: LocalDateConfig, maxIncl: LocalDateConfig, step?: number, stepUnit?: LocalDateUnit): IsoDateString[];
35
+ static range(min: LocalDateConfig, max: LocalDateConfig, incl?: Inclusiveness, step?: number, stepUnit?: LocalDateUnit): LocalDate[];
41
36
  get(unit: LocalDateUnit): number;
42
37
  set(unit: LocalDateUnit, v: number, mutate?: boolean): LocalDate;
43
38
  year(): number;
@@ -47,9 +42,9 @@ export declare class LocalDate {
47
42
  day(): number;
48
43
  day(v: number): LocalDate;
49
44
  isSame(d: LocalDateConfig): boolean;
50
- isBefore(d: LocalDateConfig): boolean;
45
+ isBefore(d: LocalDateConfig, inclusive?: boolean): boolean;
51
46
  isSameOrBefore(d: LocalDateConfig): boolean;
52
- isAfter(d: LocalDateConfig): boolean;
47
+ isAfter(d: LocalDateConfig, inclusive?: boolean): boolean;
53
48
  isSameOrAfter(d: LocalDateConfig): boolean;
54
49
  isBetween(min: LocalDateConfig, max: LocalDateConfig, incl?: Inclusiveness): boolean;
55
50
  /**
@@ -2,10 +2,9 @@
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 seq_1 = require("../seq/seq");
6
- const types_1 = require("../types");
7
5
  const localTime_1 = require("./localTime");
8
- const m31 = new Set([1, 3, 5, 7, 8, 10, 12]);
6
+ const MDAYS = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
7
+ const DATE_REGEX = /^(\d\d\d\d)-(\d\d)-(\d\d)$/;
9
8
  /* eslint-disable no-dupe-class-members */
10
9
  /**
11
10
  * @experimental
@@ -51,8 +50,13 @@ class LocalDate {
51
50
  return null;
52
51
  if (d instanceof LocalDate)
53
52
  return d;
54
- // todo: explore more performant options
55
- const [year, month, day] = d.slice(0, 10).split('-').map(Number);
53
+ // const [year, month, day] = d.slice(0, 10).split('-').map(Number)
54
+ const matches = DATE_REGEX.exec(d.slice(0, 10));
55
+ if (!matches)
56
+ return null;
57
+ const year = Number(matches[1]);
58
+ const month = Number(matches[2]);
59
+ const day = Number(matches[3]);
56
60
  if (!year ||
57
61
  !month ||
58
62
  month < 1 ||
@@ -64,6 +68,10 @@ class LocalDate {
64
68
  }
65
69
  return new LocalDate(year, month, day);
66
70
  }
71
+ // Can use just .toString()
72
+ // static parseToString(d: LocalDateConfig): IsoDateString {
73
+ // return typeof d === 'string' ? d : d.toString()
74
+ // }
67
75
  static isValid(iso) {
68
76
  return this.parseOrNull(iso) !== null;
69
77
  }
@@ -91,32 +99,23 @@ class LocalDate {
91
99
  (0, assert_1._assert)(items.length, 'LocalDate.latest called on empty array');
92
100
  return items.reduce((max, item) => (max.isSameOrAfter(item) ? max : item));
93
101
  }
94
- static range(minIncl, maxExcl, step = 1, stepUnit = 'day') {
95
- const days = [];
96
- let current = LocalDate.of(minIncl).startOf(stepUnit);
97
- const max = LocalDate.of(maxExcl).startOf(stepUnit);
98
- do {
99
- days.push(current);
102
+ static range(min, max, incl = '[)', step = 1, stepUnit = 'day') {
103
+ const dates = [];
104
+ const $min = LocalDate.of(min);
105
+ const $max = LocalDate.of(max).startOf(stepUnit);
106
+ let current = $min.startOf(stepUnit);
107
+ if (current.isAfter($min, incl[0] === '[')) {
108
+ // ok
109
+ }
110
+ else {
111
+ current.add(1, stepUnit, true);
112
+ }
113
+ const incl2 = incl[1] === ']';
114
+ while (current.isBefore($max, incl2)) {
115
+ dates.push(current);
100
116
  current = current.add(step, stepUnit);
101
- } while (current.isBefore(max));
102
- return days;
103
- }
104
- static rangeSeq(minIncl, maxExcl, step = 1, stepUnit = 'day') {
105
- const min = LocalDate.of(minIncl).startOf(stepUnit);
106
- const max = LocalDate.of(maxExcl).startOf(stepUnit);
107
- return seq_1.Sequence.create(min, d => {
108
- const next = d.add(step, stepUnit);
109
- return next.isAfter(max) ? types_1.END : next;
110
- });
111
- }
112
- static rangeString(minIncl, maxExcl, step = 1, stepUnit = 'day') {
113
- return LocalDate.range(minIncl, maxExcl, step, stepUnit).map(ld => ld.toString());
114
- }
115
- static rangeIncl(minIncl, maxIncl, step = 1, stepUnit = 'day') {
116
- return LocalDate.range(minIncl, LocalDate.of(maxIncl).add(1, stepUnit), step, stepUnit);
117
- }
118
- static rangeInclString(minIncl, maxIncl, step = 1, stepUnit = 'day') {
119
- return LocalDate.range(minIncl, LocalDate.of(maxIncl).add(1, stepUnit), step, stepUnit).map(ld => ld.toString());
117
+ }
118
+ return dates;
120
119
  }
121
120
  get(unit) {
122
121
  return unit === 'year' ? this.$year : unit === 'month' ? this.$month : this.$day;
@@ -147,14 +146,16 @@ class LocalDate {
147
146
  d = LocalDate.of(d);
148
147
  return this.$day === d.$day && this.$month === d.$month && this.$year === d.$year;
149
148
  }
150
- isBefore(d) {
151
- return this.cmp(d) === -1;
149
+ isBefore(d, inclusive = false) {
150
+ const r = this.cmp(d);
151
+ return r === -1 || (r === 0 && inclusive);
152
152
  }
153
153
  isSameOrBefore(d) {
154
154
  return this.cmp(d) <= 0;
155
155
  }
156
- isAfter(d) {
157
- return this.cmp(d) === 1;
156
+ isAfter(d, inclusive = false) {
157
+ const r = this.cmp(d);
158
+ return r === 1 || (r === 0 && inclusive);
158
159
  }
159
160
  isSameOrAfter(d) {
160
161
  return this.cmp(d) >= 0;
@@ -244,24 +245,29 @@ class LocalDate {
244
245
  $year += num;
245
246
  }
246
247
  // check day overflow
247
- let monLen = LocalDate.getMonthLength($year, $month);
248
- while ($day > monLen) {
249
- $day -= monLen;
250
- $month += 1;
251
- if ($month > 12) {
252
- $year += 1;
253
- $month -= 12;
248
+ if (unit === 'day') {
249
+ if ($day < 1) {
250
+ while ($day < 1) {
251
+ $month -= 1;
252
+ if ($month < 1) {
253
+ $year -= 1;
254
+ $month += 12;
255
+ }
256
+ $day += LocalDate.getMonthLength($year, $month);
257
+ }
254
258
  }
255
- monLen = LocalDate.getMonthLength($year, $month);
256
- }
257
- while ($day < 1) {
258
- $day += monLen;
259
- $month -= 1;
260
- if ($month < 1) {
261
- $year -= 1;
262
- $month += 12;
259
+ else {
260
+ let monLen = LocalDate.getMonthLength($year, $month);
261
+ while ($day > monLen) {
262
+ $day -= monLen;
263
+ $month += 1;
264
+ if ($month > 12) {
265
+ $year += 1;
266
+ $month -= 12;
267
+ }
268
+ monLen = LocalDate.getMonthLength($year, $month);
269
+ }
263
270
  }
264
- monLen = LocalDate.getMonthLength($year, $month);
265
271
  }
266
272
  // check month overflow
267
273
  while ($month > 12) {
@@ -305,14 +311,10 @@ class LocalDate {
305
311
  static getMonthLength(year, month) {
306
312
  if (month === 2)
307
313
  return this.isLeapYear(year) ? 29 : 28;
308
- return m31.has(month) ? 31 : 30;
314
+ return MDAYS[month];
309
315
  }
310
316
  static isLeapYear(year) {
311
- if (year % 4 !== 0)
312
- return false;
313
- if (year % 100 !== 0)
314
- return true;
315
- return year % 400 === 0;
317
+ return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
316
318
  }
317
319
  clone() {
318
320
  return new LocalDate(this.$year, this.$month, this.$day);
@@ -28,6 +28,8 @@ export declare class LocalTime {
28
28
  * Returns null if invalid
29
29
  */
30
30
  static parseOrNull(d: LocalTimeConfig | undefined | null): LocalTime | null;
31
+ static parseToDate(d: LocalTimeConfig): Date;
32
+ static parseToUnixTimestamp(d: LocalTimeConfig): UnixTimestampNumber;
31
33
  static isValid(d: LocalTimeConfig | undefined | null): boolean;
32
34
  static now(): LocalTime;
33
35
  static fromComponents(c: {
@@ -60,9 +62,9 @@ export declare class LocalTime {
60
62
  static latestOrUndefined(items: LocalTime[]): LocalTime | undefined;
61
63
  static latest(items: LocalTime[]): LocalTime;
62
64
  isSame(d: LocalTimeConfig): boolean;
63
- isBefore(d: LocalTimeConfig): boolean;
65
+ isBefore(d: LocalTimeConfig, inclusive?: boolean): boolean;
64
66
  isSameOrBefore(d: LocalTimeConfig): boolean;
65
- isAfter(d: LocalTimeConfig): boolean;
67
+ isAfter(d: LocalTimeConfig, inclusive?: boolean): boolean;
66
68
  isSameOrAfter(d: LocalTimeConfig): boolean;
67
69
  isBetween(min: LocalTimeConfig, max: LocalTimeConfig, incl?: Inclusiveness): boolean;
68
70
  /**
@@ -48,7 +48,7 @@ class LocalTime {
48
48
  date = new Date(d * 1000);
49
49
  }
50
50
  else {
51
- date = new Date(d);
51
+ date = new Date(d.slice(0, 19));
52
52
  }
53
53
  // validation
54
54
  if (isNaN(date.getDate())) {
@@ -60,6 +60,28 @@ class LocalTime {
60
60
  // }
61
61
  return new LocalTime(date, false);
62
62
  }
63
+ static parseToDate(d) {
64
+ if (d instanceof LocalTime)
65
+ return d.$date;
66
+ if (d instanceof Date)
67
+ return d;
68
+ const date = typeof d === 'number' ? new Date(d * 1000) : new Date(d);
69
+ if (isNaN(date.getDate())) {
70
+ throw new TypeError(`Cannot parse "${d}" to Date`);
71
+ }
72
+ return date;
73
+ }
74
+ static parseToUnixTimestamp(d) {
75
+ if (typeof d === 'number')
76
+ return d;
77
+ if (d instanceof LocalTime)
78
+ return d.unix();
79
+ const date = d instanceof Date ? d : new Date(d);
80
+ if (isNaN(date.getDate())) {
81
+ throw new TypeError(`Cannot parse "${d}" to UnixTimestamp`);
82
+ }
83
+ return date.valueOf() / 1000;
84
+ }
63
85
  static isValid(d) {
64
86
  return this.parseOrNull(d) !== null;
65
87
  }
@@ -164,7 +186,7 @@ class LocalTime {
164
186
  return Math.abs(this.diff(other, unit));
165
187
  }
166
188
  diff(other, unit) {
167
- const date2 = LocalTime.of(other).$date;
189
+ const date2 = LocalTime.parseToDate(other);
168
190
  if (unit === 'year') {
169
191
  return this.$date.getFullYear() - date2.getFullYear();
170
192
  }
@@ -241,14 +263,16 @@ class LocalTime {
241
263
  isSame(d) {
242
264
  return this.cmp(d) === 0;
243
265
  }
244
- isBefore(d) {
245
- return this.cmp(d) === -1;
266
+ isBefore(d, inclusive = false) {
267
+ const r = this.cmp(d);
268
+ return r === -1 || (r === 0 && inclusive);
246
269
  }
247
270
  isSameOrBefore(d) {
248
271
  return this.cmp(d) <= 0;
249
272
  }
250
- isAfter(d) {
251
- return this.cmp(d) === 1;
273
+ isAfter(d, inclusive = false) {
274
+ const r = this.cmp(d);
275
+ return r === 1 || (r === 0 && inclusive);
252
276
  }
253
277
  isSameOrAfter(d) {
254
278
  return this.cmp(d) >= 0;
@@ -269,7 +293,7 @@ class LocalTime {
269
293
  */
270
294
  cmp(d) {
271
295
  const t1 = this.$date.valueOf();
272
- const t2 = LocalTime.of(d).$date.valueOf();
296
+ const t2 = LocalTime.parseToDate(d).valueOf();
273
297
  if (t1 === t2)
274
298
  return 0;
275
299
  return t1 < t2 ? -1 : 1;
@@ -295,8 +319,8 @@ class LocalTime {
295
319
  second: this.$date.getSeconds(),
296
320
  };
297
321
  }
298
- fromNow(now = LocalTime.now()) {
299
- const msDiff = LocalTime.of(now).unixMillis() - this.unixMillis();
322
+ fromNow(now = new Date()) {
323
+ const msDiff = LocalTime.parseToDate(now).valueOf() - this.$date.valueOf();
300
324
  if (msDiff === 0)
301
325
  return 'now';
302
326
  if (msDiff >= 0) {
@@ -408,4 +432,3 @@ function localTime(d) {
408
432
  return d ? LocalTime.of(d) : LocalTime.now();
409
433
  }
410
434
  exports.localTime = localTime;
411
- // todo: range
@@ -0,0 +1,38 @@
1
+ import { UnixTimestampNumber } from '../types';
2
+ import { Inclusiveness } from './localDate';
3
+ import { LocalTime, LocalTimeConfig } from './localTime';
4
+ export declare type TimeIntervalConfig = TimeInterval | TimeIntervalString;
5
+ export declare type TimeIntervalString = string;
6
+ /**
7
+ * Class that supports an "interval of time" between 2 timestamps - start and end.
8
+ * Example: `1649267185/1649267187`.
9
+ *
10
+ * @experimental
11
+ */
12
+ export declare class TimeInterval {
13
+ private $start;
14
+ private $end;
15
+ private constructor();
16
+ static of(start: LocalTimeConfig, end: LocalTimeConfig): TimeInterval;
17
+ get start(): UnixTimestampNumber;
18
+ get end(): UnixTimestampNumber;
19
+ get startTime(): LocalTime;
20
+ get endTime(): LocalTime;
21
+ /**
22
+ * Parses string like `1649267185/1649267187` into a TimeInterval.
23
+ */
24
+ static parse(d: TimeIntervalConfig): TimeInterval;
25
+ isSame(d: TimeIntervalConfig): boolean;
26
+ isBefore(d: TimeIntervalConfig, inclusive?: boolean): boolean;
27
+ isSameOrBefore(d: TimeIntervalConfig): boolean;
28
+ isAfter(d: TimeIntervalConfig, inclusive?: boolean): boolean;
29
+ isSameOrAfter(d: TimeIntervalConfig): boolean;
30
+ includes(d: LocalTimeConfig, incl?: Inclusiveness): boolean;
31
+ /**
32
+ * TimeIntervals compare by start date.
33
+ * If it's the same - then by end date.
34
+ */
35
+ cmp(d: TimeIntervalConfig): -1 | 0 | 1;
36
+ toString(): TimeIntervalString;
37
+ toJSON(): TimeIntervalString;
38
+ }
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TimeInterval = void 0;
4
+ const localTime_1 = require("./localTime");
5
+ /**
6
+ * Class that supports an "interval of time" between 2 timestamps - start and end.
7
+ * Example: `1649267185/1649267187`.
8
+ *
9
+ * @experimental
10
+ */
11
+ class TimeInterval {
12
+ constructor($start, $end) {
13
+ this.$start = $start;
14
+ this.$end = $end;
15
+ }
16
+ static of(start, end) {
17
+ return new TimeInterval(localTime_1.LocalTime.parseToUnixTimestamp(start), localTime_1.LocalTime.parseToUnixTimestamp(end));
18
+ }
19
+ get start() {
20
+ return this.$start;
21
+ }
22
+ get end() {
23
+ return this.$end;
24
+ }
25
+ get startTime() {
26
+ return localTime_1.LocalTime.of(this.$start);
27
+ }
28
+ get endTime() {
29
+ return localTime_1.LocalTime.of(this.$end);
30
+ }
31
+ /**
32
+ * Parses string like `1649267185/1649267187` into a TimeInterval.
33
+ */
34
+ static parse(d) {
35
+ if (d instanceof TimeInterval)
36
+ return d;
37
+ const [start, end] = d.split('/').map(Number);
38
+ if (!end || !start) {
39
+ throw new Error(`Cannot parse "${d}" into TimeInterval`);
40
+ }
41
+ return new TimeInterval(start, end);
42
+ }
43
+ isSame(d) {
44
+ return this.cmp(d) === 0;
45
+ }
46
+ isBefore(d, inclusive = false) {
47
+ const r = this.cmp(d);
48
+ return r === -1 || (r === 0 && inclusive);
49
+ }
50
+ isSameOrBefore(d) {
51
+ return this.cmp(d) <= 0;
52
+ }
53
+ isAfter(d, inclusive = false) {
54
+ const r = this.cmp(d);
55
+ return r === 1 || (r === 0 && inclusive);
56
+ }
57
+ isSameOrAfter(d) {
58
+ return this.cmp(d) >= 0;
59
+ }
60
+ includes(d, incl = '[)') {
61
+ d = localTime_1.LocalTime.parseToUnixTimestamp(d);
62
+ if (d < this.$start || (d === this.$start && incl[0] === '('))
63
+ return false;
64
+ if (d > this.$end || (d === this.$end && incl[1] === ')'))
65
+ return false;
66
+ return true;
67
+ }
68
+ /**
69
+ * TimeIntervals compare by start date.
70
+ * If it's the same - then by end date.
71
+ */
72
+ cmp(d) {
73
+ d = TimeInterval.parse(d);
74
+ if (this.$start > d.$start)
75
+ return 1;
76
+ if (this.$start < d.$start)
77
+ return -1;
78
+ if (this.$end > d.$end)
79
+ return 1;
80
+ if (this.$end < d.$end)
81
+ return -1;
82
+ return 0;
83
+ }
84
+ toString() {
85
+ return [this.$start, this.$end].join('/');
86
+ }
87
+ toJSON() {
88
+ return this.toString();
89
+ }
90
+ }
91
+ exports.TimeInterval = TimeInterval;
package/dist/index.d.ts CHANGED
@@ -63,8 +63,10 @@ export * from './string/leven';
63
63
  export * from './datetime/localDate';
64
64
  export * from './datetime/localTime';
65
65
  export * from './datetime/dateInterval';
66
+ export * from './datetime/timeInterval';
66
67
  import { LocalDateConfig, LocalDateUnit, Inclusiveness } from './datetime/localDate';
67
68
  import { LocalTimeConfig, LocalTimeUnit, LocalTimeComponents } from './datetime/localTime';
68
69
  import { DateIntervalConfig, DateIntervalString } from './datetime/dateInterval';
69
- export type { DateIntervalConfig, DateIntervalString, 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, };
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, };
70
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, };
package/dist/index.js CHANGED
@@ -95,3 +95,4 @@ tslib_1.__exportStar(require("./string/leven"), exports);
95
95
  tslib_1.__exportStar(require("./datetime/localDate"), exports);
96
96
  tslib_1.__exportStar(require("./datetime/localTime"), exports);
97
97
  tslib_1.__exportStar(require("./datetime/dateInterval"), exports);
98
+ tslib_1.__exportStar(require("./datetime/timeInterval"), exports);
@@ -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 =