@naturalcycles/js-lib 14.97.0 → 14.97.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.
@@ -62,7 +62,7 @@ export class LocalTime {
62
62
  } else if (typeof d === 'number') {
63
63
  date = new Date(d * 1000)
64
64
  } else {
65
- date = new Date(d)
65
+ date = new Date(d.slice(0, 19))
66
66
  }
67
67
 
68
68
  // validation
@@ -78,6 +78,32 @@ export class LocalTime {
78
78
  return new LocalTime(date, false)
79
79
  }
80
80
 
81
+ static parseToDate(d: LocalTimeConfig): Date {
82
+ if (d instanceof LocalTime) return d.$date
83
+ if (d instanceof Date) return d
84
+
85
+ const date = typeof d === 'number' ? new Date(d * 1000) : new Date(d)
86
+
87
+ if (isNaN(date.getDate())) {
88
+ throw new TypeError(`Cannot parse "${d}" to Date`)
89
+ }
90
+
91
+ return date
92
+ }
93
+
94
+ static parseToUnixTimestamp(d: LocalTimeConfig): UnixTimestampNumber {
95
+ if (typeof d === 'number') return d
96
+ if (d instanceof LocalTime) return d.unix()
97
+
98
+ const date = d instanceof Date ? d : new Date(d)
99
+
100
+ if (isNaN(date.getDate())) {
101
+ throw new TypeError(`Cannot parse "${d}" to UnixTimestamp`)
102
+ }
103
+
104
+ return date.valueOf() / 1000
105
+ }
106
+
81
107
  static isValid(d: LocalTimeConfig | undefined | null): boolean {
82
108
  return this.parseOrNull(d) !== null
83
109
  }
@@ -205,7 +231,7 @@ export class LocalTime {
205
231
  }
206
232
 
207
233
  diff(other: LocalTimeConfig, unit: LocalTimeUnit): number {
208
- const date2 = LocalTime.of(other).$date
234
+ const date2 = LocalTime.parseToDate(other)
209
235
 
210
236
  if (unit === 'year') {
211
237
  return this.$date.getFullYear() - date2.getFullYear()
@@ -295,16 +321,18 @@ export class LocalTime {
295
321
  return this.cmp(d) === 0
296
322
  }
297
323
 
298
- isBefore(d: LocalTimeConfig): boolean {
299
- return this.cmp(d) === -1
324
+ isBefore(d: LocalTimeConfig, inclusive = false): boolean {
325
+ const r = this.cmp(d)
326
+ return r === -1 || (r === 0 && inclusive)
300
327
  }
301
328
 
302
329
  isSameOrBefore(d: LocalTimeConfig): boolean {
303
330
  return this.cmp(d) <= 0
304
331
  }
305
332
 
306
- isAfter(d: LocalTimeConfig): boolean {
307
- return this.cmp(d) === 1
333
+ isAfter(d: LocalTimeConfig, inclusive = false): boolean {
334
+ const r = this.cmp(d)
335
+ return r === 1 || (r === 0 && inclusive)
308
336
  }
309
337
 
310
338
  isSameOrAfter(d: LocalTimeConfig): boolean {
@@ -326,7 +354,7 @@ export class LocalTime {
326
354
  */
327
355
  cmp(d: LocalTimeConfig): -1 | 0 | 1 {
328
356
  const t1 = this.$date.valueOf()
329
- const t2 = LocalTime.of(d).$date.valueOf()
357
+ const t2 = LocalTime.parseToDate(d).valueOf()
330
358
  if (t1 === t2) return 0
331
359
  return t1 < t2 ? -1 : 1
332
360
  }
@@ -355,8 +383,8 @@ export class LocalTime {
355
383
  }
356
384
  }
357
385
 
358
- fromNow(now: LocalTimeConfig = LocalTime.now()): string {
359
- const msDiff = LocalTime.of(now).unixMillis() - this.unixMillis()
386
+ fromNow(now: LocalTimeConfig = new Date()): string {
387
+ const msDiff = LocalTime.parseToDate(now).valueOf() - this.$date.valueOf()
360
388
 
361
389
  if (msDiff === 0) return 'now'
362
390
 
@@ -499,5 +527,3 @@ export class LocalTime {
499
527
  export function localTime(d?: LocalTimeConfig): LocalTime {
500
528
  return d ? LocalTime.of(d) : LocalTime.now()
501
529
  }
502
-
503
- // todo: range
@@ -0,0 +1,104 @@
1
+ import { UnixTimestampNumber } from '../types'
2
+ import { Inclusiveness } from './localDate'
3
+ import { LocalTime, LocalTimeConfig } from './localTime'
4
+
5
+ export type TimeIntervalConfig = TimeInterval | TimeIntervalString
6
+ export type TimeIntervalString = string
7
+
8
+ /**
9
+ * Class that supports an "interval of time" between 2 timestamps - start and end.
10
+ * Example: `1649267185/1649267187`.
11
+ *
12
+ * @experimental
13
+ */
14
+ export class TimeInterval {
15
+ private constructor(private $start: UnixTimestampNumber, private $end: UnixTimestampNumber) {}
16
+
17
+ static of(start: LocalTimeConfig, end: LocalTimeConfig): TimeInterval {
18
+ return new TimeInterval(
19
+ LocalTime.parseToUnixTimestamp(start),
20
+ LocalTime.parseToUnixTimestamp(end),
21
+ )
22
+ }
23
+
24
+ get start(): UnixTimestampNumber {
25
+ return this.$start
26
+ }
27
+
28
+ get end(): UnixTimestampNumber {
29
+ return this.$end
30
+ }
31
+
32
+ get startTime(): LocalTime {
33
+ return LocalTime.of(this.$start)
34
+ }
35
+
36
+ get endTime(): LocalTime {
37
+ return LocalTime.of(this.$end)
38
+ }
39
+
40
+ /**
41
+ * Parses string like `1649267185/1649267187` into a TimeInterval.
42
+ */
43
+ static parse(d: TimeIntervalConfig): TimeInterval {
44
+ if (d instanceof TimeInterval) return d
45
+
46
+ const [start, end] = d.split('/').map(Number)
47
+
48
+ if (!end || !start) {
49
+ throw new Error(`Cannot parse "${d}" into TimeInterval`)
50
+ }
51
+
52
+ return new TimeInterval(start, end)
53
+ }
54
+
55
+ isSame(d: TimeIntervalConfig): boolean {
56
+ return this.cmp(d) === 0
57
+ }
58
+
59
+ isBefore(d: TimeIntervalConfig, inclusive = false): boolean {
60
+ const r = this.cmp(d)
61
+ return r === -1 || (r === 0 && inclusive)
62
+ }
63
+
64
+ isSameOrBefore(d: TimeIntervalConfig): boolean {
65
+ return this.cmp(d) <= 0
66
+ }
67
+
68
+ isAfter(d: TimeIntervalConfig, inclusive = false): boolean {
69
+ const r = this.cmp(d)
70
+ return r === 1 || (r === 0 && inclusive)
71
+ }
72
+
73
+ isSameOrAfter(d: TimeIntervalConfig): boolean {
74
+ return this.cmp(d) >= 0
75
+ }
76
+
77
+ includes(d: LocalTimeConfig, incl: Inclusiveness = '[)'): boolean {
78
+ d = LocalTime.parseToUnixTimestamp(d)
79
+ if (d < this.$start || (d === this.$start && incl[0] === '(')) return false
80
+ if (d > this.$end || (d === this.$end && incl[1] === ')')) return false
81
+ return true
82
+ }
83
+
84
+ /**
85
+ * TimeIntervals compare by start date.
86
+ * If it's the same - then by end date.
87
+ */
88
+ cmp(d: TimeIntervalConfig): -1 | 0 | 1 {
89
+ d = TimeInterval.parse(d)
90
+ if (this.$start > d.$start) return 1
91
+ if (this.$start < d.$start) return -1
92
+ if (this.$end > d.$end) return 1
93
+ if (this.$end < d.$end) return -1
94
+ return 0
95
+ }
96
+
97
+ toString(): TimeIntervalString {
98
+ return [this.$start, this.$end].join('/')
99
+ }
100
+
101
+ toJSON(): TimeIntervalString {
102
+ return this.toString()
103
+ }
104
+ }
package/src/index.ts CHANGED
@@ -160,13 +160,17 @@ export * from './string/leven'
160
160
  export * from './datetime/localDate'
161
161
  export * from './datetime/localTime'
162
162
  export * from './datetime/dateInterval'
163
+ export * from './datetime/timeInterval'
163
164
  import { LocalDateConfig, LocalDateUnit, Inclusiveness } from './datetime/localDate'
164
165
  import { LocalTimeConfig, LocalTimeUnit, LocalTimeComponents } from './datetime/localTime'
165
166
  import { DateIntervalConfig, DateIntervalString } from './datetime/dateInterval'
167
+ import { TimeIntervalConfig, TimeIntervalString } from './datetime/timeInterval'
166
168
 
167
169
  export type {
168
170
  DateIntervalConfig,
169
171
  DateIntervalString,
172
+ TimeIntervalConfig,
173
+ TimeIntervalString,
170
174
  LocalDateConfig,
171
175
  LocalDateUnit,
172
176
  Inclusiveness,