@naturalcycles/js-lib 14.91.1 → 14.93.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.
@@ -0,0 +1,29 @@
1
+ import { LocalDate, LocalDateConfig } from './localDate';
2
+ export declare type DateIntervalConfig = DateInterval | string;
3
+ /**
4
+ * Class that supports ISO8601 "Time interval" standard that looks like `2022-02-24/2022-03-30`.
5
+ *
6
+ * @experimental
7
+ */
8
+ export declare class DateInterval {
9
+ start: LocalDate;
10
+ end: LocalDate;
11
+ private constructor();
12
+ static of(start: LocalDateConfig, end: LocalDateConfig): DateInterval;
13
+ /**
14
+ * Parses string like `2022-02-24/2023-03-30` into a DateInterval.
15
+ */
16
+ static parse(d: DateIntervalConfig): DateInterval;
17
+ isSame(d: DateIntervalConfig): boolean;
18
+ isBefore(d: DateIntervalConfig): boolean;
19
+ isSameOrBefore(d: DateIntervalConfig): boolean;
20
+ isAfter(d: DateIntervalConfig): boolean;
21
+ isSameOrAfter(d: DateIntervalConfig): boolean;
22
+ /**
23
+ * DateIntervals compare by start date.
24
+ * If it's the same - then by end date.
25
+ */
26
+ cmp(d: DateIntervalConfig): -1 | 0 | 1;
27
+ toString(): string;
28
+ private toJSON;
29
+ }
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DateInterval = void 0;
4
+ const localDate_1 = require("./localDate");
5
+ /**
6
+ * Class that supports ISO8601 "Time interval" standard that looks like `2022-02-24/2022-03-30`.
7
+ *
8
+ * @experimental
9
+ */
10
+ class DateInterval {
11
+ constructor(start, end) {
12
+ this.start = start;
13
+ this.end = end;
14
+ }
15
+ static of(start, end) {
16
+ return new DateInterval(localDate_1.LocalDate.of(start), localDate_1.LocalDate.of(end));
17
+ }
18
+ /**
19
+ * Parses string like `2022-02-24/2023-03-30` into a DateInterval.
20
+ */
21
+ static parse(d) {
22
+ if (d instanceof DateInterval)
23
+ return d;
24
+ const [start, end] = d.split('/');
25
+ if (!end || !start) {
26
+ throw new Error(`Cannot parse "${d}" into DateInterval`);
27
+ }
28
+ return new DateInterval(localDate_1.LocalDate.of(start), localDate_1.LocalDate.of(end));
29
+ }
30
+ isSame(d) {
31
+ return this.cmp(d) === 0;
32
+ }
33
+ isBefore(d) {
34
+ return this.cmp(d) === -1;
35
+ }
36
+ isSameOrBefore(d) {
37
+ return this.cmp(d) <= 0;
38
+ }
39
+ isAfter(d) {
40
+ return this.cmp(d) === 1;
41
+ }
42
+ isSameOrAfter(d) {
43
+ return this.cmp(d) >= 0;
44
+ }
45
+ /**
46
+ * DateIntervals compare by start date.
47
+ * If it's the same - then by end date.
48
+ */
49
+ cmp(d) {
50
+ d = DateInterval.parse(d);
51
+ return this.start.cmp(d.start) || this.end.cmp(d.end);
52
+ }
53
+ toString() {
54
+ return [this.start, this.end].join('/');
55
+ }
56
+ toJSON() {
57
+ return this.toString();
58
+ }
59
+ }
60
+ exports.DateInterval = DateInterval;
@@ -1,5 +1,5 @@
1
1
  import { Sequence } from '../seq/seq';
2
- import { IsoDate } from '../types';
2
+ import { IsoDate, UnixTimestamp } from '../types';
3
3
  import { LocalTime } from './localTime';
4
4
  export declare type LocalDateUnit = 'year' | 'month' | 'day';
5
5
  export declare type LocalDateConfig = LocalDate | string;
@@ -19,12 +19,14 @@ export declare class LocalDate {
19
19
  static of(d: LocalDateConfig): LocalDate;
20
20
  static parseCompact(d: string): LocalDate;
21
21
  static fromDate(d: Date): LocalDate;
22
+ static fromDateUTC(d: Date): LocalDate;
22
23
  /**
23
24
  * Returns null if invalid.
24
25
  */
25
26
  static parseOrNull(d: LocalDateConfig): LocalDate | null;
26
27
  static isValid(iso: string): boolean;
27
28
  static today(): LocalDate;
29
+ static todayUTC(): LocalDate;
28
30
  static sort(items: LocalDate[], mutate?: boolean, descending?: boolean): LocalDate[];
29
31
  static earliestOrUndefined(items: LocalDate[]): LocalDate | undefined;
30
32
  static earliest(items: LocalDate[]): LocalDate;
@@ -72,8 +74,11 @@ export declare class LocalDate {
72
74
  */
73
75
  toDate(): Date;
74
76
  toLocalTime(): LocalTime;
77
+ toISODate(): IsoDate;
75
78
  toString(): IsoDate;
76
79
  toStringCompact(): string;
80
+ unix(): UnixTimestamp;
81
+ unixMillis(): number;
77
82
  toJSON(): IsoDate;
78
83
  }
79
84
  /**
@@ -39,6 +39,9 @@ class LocalDate {
39
39
  static fromDate(d) {
40
40
  return new LocalDate(d.getFullYear(), d.getMonth() + 1, d.getDate());
41
41
  }
42
+ static fromDateUTC(d) {
43
+ return new LocalDate(d.getUTCFullYear(), d.getUTCMonth() + 1, d.getUTCDate());
44
+ }
42
45
  /**
43
46
  * Returns null if invalid.
44
47
  */
@@ -64,6 +67,9 @@ class LocalDate {
64
67
  static today() {
65
68
  return this.fromDate(new Date());
66
69
  }
70
+ static todayUTC() {
71
+ return this.fromDateUTC(new Date());
72
+ }
67
73
  static sort(items, mutate = false, descending = false) {
68
74
  const mod = descending ? -1 : 1;
69
75
  return (mutate ? items : [...items]).sort((a, b) => a.cmp(b) * mod);
@@ -286,6 +292,9 @@ class LocalDate {
286
292
  toLocalTime() {
287
293
  return localTime_1.LocalTime.of(this.toDate());
288
294
  }
295
+ toISODate() {
296
+ return this.toString();
297
+ }
289
298
  toString() {
290
299
  return [
291
300
  String(this.year).padStart(4, '0'),
@@ -300,6 +309,13 @@ class LocalDate {
300
309
  String(this.day).padStart(2, '0'),
301
310
  ].join('');
302
311
  }
312
+ // May be not optimal, as LocalTime better suits it
313
+ unix() {
314
+ return Math.floor(this.toDate().valueOf() / 1000);
315
+ }
316
+ unixMillis() {
317
+ return this.toDate().valueOf();
318
+ }
303
319
  toJSON() {
304
320
  return this.toString();
305
321
  }
@@ -15,18 +15,20 @@ export interface LocalTimeComponents {
15
15
  */
16
16
  export declare class LocalTime {
17
17
  private $date;
18
+ utcMode: boolean;
18
19
  private constructor();
19
20
  /**
20
21
  * Parses input String into LocalDate.
21
22
  * Input can already be a LocalDate - it is returned as-is in that case.
22
23
  */
23
24
  static of(d: LocalTimeConfig): LocalTime;
25
+ utc(): this;
26
+ local(): this;
24
27
  /**
25
28
  * Returns null if invalid
26
29
  */
27
30
  static parseOrNull(d: LocalTimeConfig): LocalTime | null;
28
31
  static isValid(d: LocalTimeConfig): boolean;
29
- static unix(ts: UnixTimestamp): LocalTime;
30
32
  static now(): LocalTime;
31
33
  static fromComponents(c: {
32
34
  year: number;
@@ -69,9 +71,11 @@ export declare class LocalTime {
69
71
  */
70
72
  cmp(d: LocalTimeConfig): -1 | 0 | 1;
71
73
  components(): LocalTimeComponents;
74
+ fromNow(now?: LocalTimeConfig): string;
72
75
  getDate(): Date;
73
76
  clone(): LocalTime;
74
77
  unix(): UnixTimestamp;
78
+ unixMillis(): number;
75
79
  valueOf(): UnixTimestamp;
76
80
  toLocalDate(): LocalDate;
77
81
  toPretty(seconds?: boolean): IsoDateTime;
@@ -83,6 +87,10 @@ export declare class LocalTime {
83
87
  * Returns e.g: `1984-06-21`, only the date part of DateTime
84
88
  */
85
89
  toISODate(): IsoDate;
90
+ /**
91
+ * Returns e.g: `17:03:15` (or `17:03` with seconds=false)
92
+ */
93
+ toISOTime(seconds?: boolean): string;
86
94
  /**
87
95
  * Returns e.g: `19840621_1705`
88
96
  */
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.localTime = exports.LocalTime = void 0;
4
4
  const assert_1 = require("../error/assert");
5
+ const time_util_1 = require("../time/time.util");
5
6
  const localDate_1 = require("./localDate");
6
7
  /* eslint-disable no-dupe-class-members */
7
8
  // Design choices:
@@ -13,12 +14,16 @@ const localDate_1 = require("./localDate");
13
14
  // .valueOf returns unix timestamp (no millis)
14
15
  // Prevents dayjs(undefined) being dayjs.now()
15
16
  // Validates on parse, throws if invalid. Doesn't allow invalid objects
17
+ // No arbitrary .format('') (which is slow to parse) vs well-defined (opinionated) formats
18
+ // Separate LocalTime, powered by native js Date, and LocalDate - a smaller functionality class when
19
+ // you only need "whole dates", no time, no timezone worry
16
20
  /**
17
21
  * @experimental
18
22
  */
19
23
  class LocalTime {
20
- constructor($date) {
24
+ constructor($date, utcMode) {
21
25
  this.$date = $date;
26
+ this.utcMode = utcMode;
22
27
  }
23
28
  /**
24
29
  * Parses input String into LocalDate.
@@ -31,6 +36,14 @@ class LocalTime {
31
36
  }
32
37
  return t;
33
38
  }
39
+ utc() {
40
+ this.utcMode = true;
41
+ return this;
42
+ }
43
+ local() {
44
+ this.utcMode = false;
45
+ return this;
46
+ }
34
47
  /**
35
48
  * Returns null if invalid
36
49
  */
@@ -52,106 +65,110 @@ class LocalTime {
52
65
  // throw new TypeError(`Cannot parse "${d}" into LocalTime`)
53
66
  return null;
54
67
  }
55
- return new LocalTime(date);
68
+ // if (utc) {
69
+ // date.setMinutes(date.getMinutes() + date.getTimezoneOffset())
70
+ // }
71
+ return new LocalTime(date, false);
56
72
  }
57
73
  static isValid(d) {
58
74
  return this.parseOrNull(d) !== null;
59
75
  }
60
- static unix(ts) {
61
- return new LocalTime(new Date(ts * 1000));
62
- }
63
76
  static now() {
64
- return this.of(new Date());
77
+ return new LocalTime(new Date(), false);
65
78
  }
66
79
  static fromComponents(c) {
67
- return new LocalTime(new Date(c.year, c.month - 1, c.day, c.hour, c.minute, c.second));
80
+ return new LocalTime(new Date(c.year, c.month - 1, c.day, c.hour, c.minute, c.second), false);
68
81
  }
69
82
  get(unit) {
70
83
  if (unit === 'year') {
71
- return this.$date.getFullYear();
84
+ return this.utcMode ? this.$date.getUTCFullYear() : this.$date.getFullYear();
72
85
  }
73
86
  if (unit === 'month') {
74
- return this.$date.getMonth() + 1;
87
+ return (this.utcMode ? this.$date.getUTCMonth() : this.$date.getMonth()) + 1;
75
88
  }
76
89
  if (unit === 'day') {
77
- return this.$date.getDate();
90
+ return this.utcMode ? this.$date.getUTCDate() : this.$date.getDate();
78
91
  }
79
92
  if (unit === 'hour') {
80
- return this.$date.getHours();
93
+ return this.utcMode ? this.$date.getUTCHours() : this.$date.getHours();
81
94
  }
82
95
  if (unit === 'minute') {
83
- return this.$date.getMinutes();
96
+ return this.utcMode ? this.$date.getUTCMinutes() : this.$date.getMinutes();
84
97
  }
85
98
  // second
86
- return this.$date.getSeconds();
99
+ return this.utcMode ? this.$date.getUTCSeconds() : this.$date.getSeconds();
87
100
  }
88
101
  set(unit, v, mutate = false) {
89
102
  const t = mutate ? this : this.clone();
103
+ /* eslint-disable @typescript-eslint/no-unused-expressions */
90
104
  if (unit === 'year') {
91
- t.$date.setFullYear(v);
105
+ this.utcMode ? t.$date.setUTCFullYear(v) : t.$date.setFullYear(v);
92
106
  }
93
107
  else if (unit === 'month') {
94
- t.$date.setMonth(v - 1);
108
+ this.utcMode ? t.$date.setUTCMonth(v - 1) : t.$date.setMonth(v - 1);
95
109
  }
96
110
  else if (unit === 'day') {
97
- t.$date.setDate(v);
111
+ this.utcMode ? t.$date.setUTCDate(v) : t.$date.setDate(v);
98
112
  }
99
113
  else if (unit === 'hour') {
100
- t.$date.setHours(v);
114
+ this.utcMode ? t.$date.setUTCHours(v) : t.$date.setHours(v);
101
115
  }
102
116
  else if (unit === 'minute') {
103
- t.$date.setMinutes(v);
117
+ this.utcMode ? t.$date.setUTCMinutes(v) : t.$date.setMinutes(v);
104
118
  }
105
119
  else if (unit === 'second') {
106
- t.$date.setSeconds(v);
120
+ this.utcMode ? t.$date.setUTCSeconds(v) : t.$date.setSeconds(v);
107
121
  }
122
+ /* eslint-enable @typescript-eslint/no-unused-expressions */
108
123
  return t;
109
124
  }
110
125
  year(v) {
111
- return v === undefined ? this.$date.getFullYear() : this.set('year', v);
126
+ return v === undefined ? this.get('year') : this.set('year', v);
112
127
  }
113
128
  month(v) {
114
- return v === undefined ? this.$date.getMonth() + 1 : this.set('month', v);
129
+ return v === undefined ? this.get('month') : this.set('month', v);
115
130
  }
116
131
  date(v) {
117
- return v === undefined ? this.$date.getDate() : this.set('day', v);
132
+ return v === undefined ? this.get('day') : this.set('day', v);
118
133
  }
119
134
  hour(v) {
120
- return v === undefined ? this.$date.getHours() : this.set('hour', v);
135
+ return v === undefined ? this.get('hour') : this.set('hour', v);
121
136
  }
122
137
  minute(v) {
123
- return v === undefined ? this.$date.getMinutes() : this.set('minute', v);
138
+ return v === undefined ? this.get('minute') : this.set('minute', v);
124
139
  }
125
140
  second(v) {
126
- return v === undefined ? this.$date.getSeconds() : this.set('second', v);
141
+ return v === undefined ? this.get('second') : this.set('second', v);
127
142
  }
128
143
  setComponents(c, mutate = false) {
129
144
  const d = mutate ? this.$date : new Date(this.$date);
145
+ /* eslint-disable @typescript-eslint/no-unused-expressions */
130
146
  if (c.year) {
131
- d.setFullYear(c.year);
147
+ this.utcMode ? d.setUTCFullYear(c.year) : d.setFullYear(c.year);
132
148
  }
133
149
  if (c.month) {
134
- d.setMonth(c.month - 1);
150
+ this.utcMode ? d.setUTCMonth(c.month - 1) : d.setMonth(c.month - 1);
135
151
  }
136
152
  if (c.day) {
137
- d.setDate(c.day);
153
+ this.utcMode ? d.setUTCDate(c.day) : d.setDate(c.day);
138
154
  }
139
155
  if (c.hour !== undefined) {
140
- d.setHours(c.hour);
156
+ this.utcMode ? d.setUTCHours(c.hour) : d.setHours(c.hour);
141
157
  }
142
158
  if (c.minute !== undefined) {
143
- d.setMinutes(c.minute);
159
+ this.utcMode ? d.setUTCMinutes(c.minute) : d.setMinutes(c.minute);
144
160
  }
145
161
  if (c.second !== undefined) {
146
- d.setSeconds(c.second);
162
+ this.utcMode ? d.setUTCSeconds(c.second) : d.setSeconds(c.second);
147
163
  }
148
- return mutate ? this : new LocalTime(d);
164
+ /* eslint-enable @typescript-eslint/no-unused-expressions */
165
+ return mutate ? this : new LocalTime(d, this.utcMode);
149
166
  }
150
167
  add(num, unit, mutate = false) {
151
168
  return this.set(unit, this.get(unit) + num, mutate);
152
169
  }
153
170
  subtract(num, unit, mutate = false) {
154
- return this.add(-num, unit, mutate);
171
+ return this.add(num * -1, unit, mutate);
155
172
  }
156
173
  absDiff(other, unit) {
157
174
  return Math.abs(this.diff(other, unit));
@@ -189,40 +206,23 @@ class LocalTime {
189
206
  startOf(unit, mutate = false) {
190
207
  if (unit === 'second')
191
208
  return this;
192
- if (mutate) {
193
- const d = this.$date;
194
- d.setSeconds(0);
195
- if (unit === 'minute')
196
- return this;
197
- d.setMinutes(0);
198
- if (unit === 'hour')
199
- return this;
200
- d.setHours(0);
201
- if (unit === 'day')
202
- return this;
203
- d.setDate(0);
204
- if (unit === 'month')
205
- return this;
206
- d.setMonth(0);
207
- return this;
208
- }
209
- const c = this.components();
210
- c.second = 0;
211
- if (unit === 'year') {
212
- c.month = c.day = 1;
213
- c.hour = c.minute = 0;
214
- }
215
- else if (unit === 'month') {
216
- c.day = 1;
217
- c.hour = c.minute = 0;
218
- }
219
- else if (unit === 'day') {
220
- c.hour = c.minute = 0;
221
- }
222
- else if (unit === 'hour') {
223
- c.minute = 0;
224
- }
225
- return LocalTime.fromComponents(c);
209
+ const d = mutate ? this.$date : new Date(this.$date);
210
+ /* eslint-disable @typescript-eslint/no-unused-expressions */
211
+ this.utcMode ? d.setUTCSeconds(0) : d.setSeconds(0);
212
+ if (unit !== 'minute') {
213
+ this.utcMode ? d.setUTCMinutes(0) : d.setMinutes(0);
214
+ if (unit !== 'hour') {
215
+ this.utcMode ? d.setUTCHours(0) : d.setHours(0);
216
+ if (unit !== 'day') {
217
+ this.utcMode ? d.setUTCDate(0) : d.setDate(0);
218
+ if (unit !== 'month') {
219
+ this.utcMode ? d.setUTCMonth(0) : d.setMonth(0);
220
+ }
221
+ }
222
+ }
223
+ }
224
+ /* eslint-enable @typescript-eslint/no-unused-expressions */
225
+ return mutate ? this : new LocalTime(d, this.utcMode);
226
226
  }
227
227
  static sort(items, mutate = false, descending = false) {
228
228
  const mod = descending ? -1 : 1;
@@ -277,6 +277,16 @@ class LocalTime {
277
277
  }
278
278
  // todo: endOf
279
279
  components() {
280
+ if (this.utcMode) {
281
+ return {
282
+ year: this.$date.getUTCFullYear(),
283
+ month: this.$date.getUTCMonth() + 1,
284
+ day: this.$date.getUTCDate(),
285
+ hour: this.$date.getUTCHours(),
286
+ minute: this.$date.getUTCMinutes(),
287
+ second: this.$date.getSeconds(),
288
+ };
289
+ }
280
290
  return {
281
291
  year: this.$date.getFullYear(),
282
292
  month: this.$date.getMonth() + 1,
@@ -286,27 +296,56 @@ class LocalTime {
286
296
  second: this.$date.getSeconds(),
287
297
  };
288
298
  }
299
+ fromNow(now = LocalTime.now()) {
300
+ const msDiff = LocalTime.of(now).unixMillis() - this.unixMillis();
301
+ if (msDiff === 0)
302
+ return 'now';
303
+ if (msDiff >= 0) {
304
+ return `${(0, time_util_1._ms)(msDiff)} ago`;
305
+ }
306
+ return `in ${(0, time_util_1._ms)(msDiff * -1)}`;
307
+ }
289
308
  getDate() {
290
309
  return this.$date;
291
310
  }
292
311
  clone() {
293
- return new LocalTime(new Date(this.$date));
312
+ return new LocalTime(new Date(this.$date), this.utcMode);
294
313
  }
295
314
  unix() {
296
315
  return Math.floor(this.$date.valueOf() / 1000);
297
316
  }
317
+ unixMillis() {
318
+ return this.$date.valueOf();
319
+ }
298
320
  valueOf() {
299
321
  return Math.floor(this.$date.valueOf() / 1000);
300
322
  }
301
323
  toLocalDate() {
324
+ if (this.utcMode) {
325
+ return localDate_1.LocalDate.create(this.$date.getUTCFullYear(), this.$date.getUTCMonth() + 1, this.$date.getUTCDate());
326
+ }
302
327
  return localDate_1.LocalDate.create(this.$date.getFullYear(), this.$date.getMonth() + 1, this.$date.getDate());
303
328
  }
304
329
  toPretty(seconds = true) {
305
- return this.$date
306
- .toISOString()
307
- .slice(0, seconds ? 19 : 16)
308
- .split('T')
309
- .join(' ');
330
+ const { year, month, day, hour, minute, second } = this.components();
331
+ return ([
332
+ String(year).padStart(4, '0'),
333
+ String(month).padStart(2, '0'),
334
+ String(day).padStart(2, '0'),
335
+ ].join('-') +
336
+ ' ' +
337
+ [
338
+ String(hour).padStart(2, '0'),
339
+ String(minute).padStart(2, '0'),
340
+ seconds && String(second).padStart(2, '0'),
341
+ ]
342
+ .filter(Boolean)
343
+ .join(':'));
344
+ // return this.$date
345
+ // .toISOString()
346
+ // .slice(0, seconds ? 19 : 16)
347
+ // .split('T')
348
+ // .join(' ')
310
349
  }
311
350
  /**
312
351
  * Returns e.g: `1984-06-21T17:56:21`, only the date part of DateTime
@@ -318,20 +357,41 @@ class LocalTime {
318
357
  * Returns e.g: `1984-06-21`, only the date part of DateTime
319
358
  */
320
359
  toISODate() {
321
- return this.$date.toISOString().slice(0, 10);
360
+ const { year, month, day } = this.components();
361
+ return [
362
+ String(year).padStart(4, '0'),
363
+ String(month).padStart(2, '0'),
364
+ String(day).padStart(2, '0'),
365
+ ].join('-');
366
+ // return this.$date.toISOString().slice(0, 10)
367
+ }
368
+ /**
369
+ * Returns e.g: `17:03:15` (or `17:03` with seconds=false)
370
+ */
371
+ toISOTime(seconds = true) {
372
+ // return this.$date.toISOString().slice(11, seconds ? 19 : 16)
373
+ const { hour, minute, second } = this.components();
374
+ return [
375
+ String(hour).padStart(2, '0'),
376
+ String(minute).padStart(2, '0'),
377
+ seconds && String(second).padStart(2, '0'),
378
+ ]
379
+ .filter(Boolean)
380
+ .join(':');
322
381
  }
323
382
  /**
324
383
  * Returns e.g: `19840621_1705`
325
384
  */
326
385
  toStringCompact(seconds = false) {
386
+ const { year, month, day, hour, minute, second } = this.components();
327
387
  return [
328
- String(this.$date.getFullYear()).padStart(4, '0'),
329
- String(this.$date.getMonth() + 1).padStart(2, '0'),
330
- String(this.$date.getDate()).padStart(2, '0'),
388
+ String(year).padStart(4, '0'),
389
+ String(month).padStart(2, '0'),
390
+ String(day).padStart(2, '0'),
331
391
  '_',
332
- String(this.$date.getHours()).padStart(2, '0'),
333
- String(this.$date.getMinutes()).padStart(2, '0'),
334
- seconds ? String(this.$date.getSeconds()).padStart(2, '0') : '',
392
+ String(hour).padStart(2, '0'),
393
+ String(minute).padStart(2, '0'),
394
+ seconds ? String(second).padStart(2, '0') : '',
335
395
  ].join('');
336
396
  }
337
397
  toString() {
package/dist/index.d.ts CHANGED
@@ -63,7 +63,9 @@ export * from './math/stack.util';
63
63
  export * from './string/leven';
64
64
  export * from './datetime/localDate';
65
65
  export * from './datetime/localTime';
66
+ export * from './datetime/dateInterval';
66
67
  import { LocalDateConfig, LocalDateUnit } from './datetime/localDate';
67
68
  import { LocalTimeConfig, LocalTimeUnit, LocalTimeComponents } from './datetime/localTime';
68
- export type { LocalDateConfig, LocalDateUnit, 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, IsoDateTime, Reviver, PMapOptions, Mapper, AsyncMapper, Predicate, AsyncPredicate, BatchResult, DeferredPromise, PRetryOptions, PTimeoutOptions, TryCatchOptions, StringifyAnyOptions, JsonStringifyFunction, Merge, ReadonlyDeep, Promisable, Simplify, ConditionalPick, ConditionalExcept, Class, 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, };
69
+ import { DateIntervalConfig } from './datetime/dateInterval';
70
+ export type { DateIntervalConfig, LocalDateConfig, LocalDateUnit, 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, IsoDateTime, Reviver, PMapOptions, Mapper, AsyncMapper, Predicate, AsyncPredicate, BatchResult, DeferredPromise, PRetryOptions, PTimeoutOptions, TryCatchOptions, StringifyAnyOptions, JsonStringifyFunction, Merge, ReadonlyDeep, Promisable, Simplify, ConditionalPick, ConditionalExcept, Class, 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, };
69
71
  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("./math/stack.util"), exports);
95
95
  tslib_1.__exportStar(require("./string/leven"), exports);
96
96
  tslib_1.__exportStar(require("./datetime/localDate"), exports);
97
97
  tslib_1.__exportStar(require("./datetime/localTime"), exports);
98
+ tslib_1.__exportStar(require("./datetime/dateInterval"), exports);
@@ -0,0 +1,56 @@
1
+ import { LocalDate } from './localDate';
2
+ /**
3
+ * Class that supports ISO8601 "Time interval" standard that looks like `2022-02-24/2022-03-30`.
4
+ *
5
+ * @experimental
6
+ */
7
+ export class DateInterval {
8
+ constructor(start, end) {
9
+ this.start = start;
10
+ this.end = end;
11
+ }
12
+ static of(start, end) {
13
+ return new DateInterval(LocalDate.of(start), LocalDate.of(end));
14
+ }
15
+ /**
16
+ * Parses string like `2022-02-24/2023-03-30` into a DateInterval.
17
+ */
18
+ static parse(d) {
19
+ if (d instanceof DateInterval)
20
+ return d;
21
+ const [start, end] = d.split('/');
22
+ if (!end || !start) {
23
+ throw new Error(`Cannot parse "${d}" into DateInterval`);
24
+ }
25
+ return new DateInterval(LocalDate.of(start), LocalDate.of(end));
26
+ }
27
+ isSame(d) {
28
+ return this.cmp(d) === 0;
29
+ }
30
+ isBefore(d) {
31
+ return this.cmp(d) === -1;
32
+ }
33
+ isSameOrBefore(d) {
34
+ return this.cmp(d) <= 0;
35
+ }
36
+ isAfter(d) {
37
+ return this.cmp(d) === 1;
38
+ }
39
+ isSameOrAfter(d) {
40
+ return this.cmp(d) >= 0;
41
+ }
42
+ /**
43
+ * DateIntervals compare by start date.
44
+ * If it's the same - then by end date.
45
+ */
46
+ cmp(d) {
47
+ d = DateInterval.parse(d);
48
+ return this.start.cmp(d.start) || this.end.cmp(d.end);
49
+ }
50
+ toString() {
51
+ return [this.start, this.end].join('/');
52
+ }
53
+ toJSON() {
54
+ return this.toString();
55
+ }
56
+ }