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