@naturalcycles/js-lib 14.139.2 → 14.140.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.
@@ -3,7 +3,7 @@ import { LocalTime } from './localTime';
3
3
  export type LocalDateUnit = LocalDateUnitStrict | 'week';
4
4
  export type LocalDateUnitStrict = 'year' | 'month' | 'day';
5
5
  export type Inclusiveness = '()' | '[]' | '[)' | '(]';
6
- export type LocalDateConfig = LocalDate | IsoDateString;
6
+ export type LocalDateConfig = LocalDate | Date | IsoDateString;
7
7
  export type LocalDateFormatter = (ld: LocalDate) => string;
8
8
  /**
9
9
  * @experimental
@@ -50,8 +50,11 @@ class LocalDate {
50
50
  return null;
51
51
  if (d instanceof LocalDate)
52
52
  return d;
53
+ if (d instanceof Date) {
54
+ return this.fromDate(d);
55
+ }
53
56
  // const [year, month, day] = d.slice(0, 10).split('-').map(Number)
54
- const matches = DATE_REGEX.exec(d.slice(0, 10));
57
+ const matches = typeof d === 'string' && DATE_REGEX.exec(d.slice(0, 10));
55
58
  if (!matches)
56
59
  return null;
57
60
  const year = Number(matches[1]);
@@ -60,6 +60,10 @@ class LocalTime {
60
60
  else if (typeof d === 'number') {
61
61
  date = new Date(d * 1000);
62
62
  }
63
+ else if (typeof d !== 'string') {
64
+ // unexpected type, e.g Function or something
65
+ return null;
66
+ }
63
67
  else {
64
68
  date = new Date(d.slice(0, 19));
65
69
  }
@@ -402,7 +406,7 @@ class LocalTime {
402
406
  return Math.floor(this.$date.valueOf() / 1000);
403
407
  }
404
408
  toLocalDate() {
405
- return localDate_1.LocalDate.create(this.$date.getFullYear(), this.$date.getMonth() + 1, this.$date.getDate());
409
+ return localDate_1.LocalDate.fromDate(this.$date);
406
410
  }
407
411
  toPretty(seconds = true) {
408
412
  const { year, month, day, hour, minute, second } = this.components();
@@ -47,8 +47,11 @@ export class LocalDate {
47
47
  return null;
48
48
  if (d instanceof LocalDate)
49
49
  return d;
50
+ if (d instanceof Date) {
51
+ return this.fromDate(d);
52
+ }
50
53
  // const [year, month, day] = d.slice(0, 10).split('-').map(Number)
51
- const matches = DATE_REGEX.exec(d.slice(0, 10));
54
+ const matches = typeof d === 'string' && DATE_REGEX.exec(d.slice(0, 10));
52
55
  if (!matches)
53
56
  return null;
54
57
  const year = Number(matches[1]);
@@ -57,6 +57,10 @@ export class LocalTime {
57
57
  else if (typeof d === 'number') {
58
58
  date = new Date(d * 1000);
59
59
  }
60
+ else if (typeof d !== 'string') {
61
+ // unexpected type, e.g Function or something
62
+ return null;
63
+ }
60
64
  else {
61
65
  date = new Date(d.slice(0, 19));
62
66
  }
@@ -400,7 +404,7 @@ export class LocalTime {
400
404
  return Math.floor(this.$date.valueOf() / 1000);
401
405
  }
402
406
  toLocalDate() {
403
- return LocalDate.create(this.$date.getFullYear(), this.$date.getMonth() + 1, this.$date.getDate());
407
+ return LocalDate.fromDate(this.$date);
404
408
  }
405
409
  toPretty(seconds = true) {
406
410
  const { year, month, day, hour, minute, second } = this.components();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.139.2",
3
+ "version": "14.140.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -14,7 +14,7 @@ export type Inclusiveness = '()' | '[]' | '[)' | '(]'
14
14
  const MDAYS = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
15
15
  const DATE_REGEX = /^(\d\d\d\d)-(\d\d)-(\d\d)$/
16
16
 
17
- export type LocalDateConfig = LocalDate | IsoDateString
17
+ export type LocalDateConfig = LocalDate | Date | IsoDateString
18
18
  export type LocalDateFormatter = (ld: LocalDate) => string
19
19
 
20
20
  /* eslint-disable no-dupe-class-members */
@@ -67,9 +67,12 @@ export class LocalDate {
67
67
  static parseOrNull(d: LocalDateConfig | undefined | null): LocalDate | null {
68
68
  if (!d) return null
69
69
  if (d instanceof LocalDate) return d
70
+ if (d instanceof Date) {
71
+ return this.fromDate(d)
72
+ }
70
73
 
71
74
  // const [year, month, day] = d.slice(0, 10).split('-').map(Number)
72
- const matches = DATE_REGEX.exec(d.slice(0, 10))
75
+ const matches = typeof (d as any) === 'string' && DATE_REGEX.exec(d.slice(0, 10))
73
76
  if (!matches) return null
74
77
 
75
78
  const year = Number(matches[1])
@@ -82,6 +82,9 @@ export class LocalTime {
82
82
  date = d
83
83
  } else if (typeof d === 'number') {
84
84
  date = new Date(d * 1000)
85
+ } else if (typeof (d as any) !== 'string') {
86
+ // unexpected type, e.g Function or something
87
+ return null
85
88
  } else {
86
89
  date = new Date(d.slice(0, 19))
87
90
  }
@@ -494,11 +497,7 @@ export class LocalTime {
494
497
  }
495
498
 
496
499
  toLocalDate(): LocalDate {
497
- return LocalDate.create(
498
- this.$date.getFullYear(),
499
- this.$date.getMonth() + 1,
500
- this.$date.getDate(),
501
- )
500
+ return LocalDate.fromDate(this.$date)
502
501
  }
503
502
 
504
503
  toPretty(seconds = true): IsoDateTimeString {