-
Notifications
You must be signed in to change notification settings - Fork 5
/
HSpan.ts
513 lines (460 loc) · 12.4 KB
/
HSpan.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/*
* Copyright (c) 2021, J2 Innovations. All Rights Reserved
*/
/* eslint @typescript-eslint/no-explicit-any: "off" */
import { HDateTime } from './HDateTime'
import { HDate } from './HDate'
import { HStr } from './HStr'
import { ZincReader } from './ZincReader'
import { valueIsKind } from './HVal'
import { Kind } from './Kind'
import { HaysonDate, HaysonDateTime } from './hayson'
/**
* Relative span mode.
*/
export enum SpanMode {
today = 'today',
yesterday = 'yesterday',
thisWeek = 'thisWeek',
lastWeek = 'lastWeek',
thisMonth = 'thisMonth',
lastMonth = 'lastMonth',
thisQuarter = 'thisQuarter',
lastQuarter = 'lastQuarter',
thisYear = 'thisYear',
lastYear = 'lastYear',
}
/**
* The arguments for a relative span.
*/
export interface RelativeSpan {
mode: SpanMode
timezone?: string
}
function isRelativeSpan(args: unknown): args is RelativeSpan {
return !!(args as RelativeSpan)?.mode
}
/**
* The arguments for a single date span.
*/
export interface SingleDateSpan {
start: HDate
timezone?: string
}
/**
* The arguments for a double date span.
*/
export interface DoubleDateSpan {
start: HDate
end: HDate
timezone?: string
}
/**
* The arguments for a double date time span.
*/
export interface DoubleDateTimeSpan {
start: HDateTime
end: HDateTime
timezone?: string
}
/**
* A JSON encoded Span.
*/
export interface SpanJsonData {
mode?: SpanMode
start?: HaysonDate | HaysonDateTime
end?: HaysonDate | HaysonDateTime
timezone?: string
}
/**
* Models a range of time using an inclusive starting timestamp and exclusive ending timestamp.
*
* Please note, due to the lack of date time support in vanilla JavaScript, this implementation relies
* on using a third party library with proper timezone and locale support. See {@link HSpan.toLuxonInterval}
* and {@link HSpan.toMomentRange}.
*/
export class HSpan {
/**
* The relative mode. If undefined the span is absolute.
*/
public readonly mode?: SpanMode
/**
* Inclusive starting time.
*/
public readonly start?: HDateTime | HDate
/**
* Exclusive ending time.
*/
public readonly end?: HDateTime | HDate
/**
* The timezone to use.
*/
public readonly timezone: string
/**
* Construct a new span.
*
* @param options.mode The span mode.
* @param options.start The starting date or date time.
* @param options.end The ending date or date time.
* @param options.timezone Optional timezone identifier.
*/
public constructor(
args:
| RelativeSpan
| SingleDateSpan
| DoubleDateSpan
| DoubleDateTimeSpan
) {
this.timezone = args.timezone ?? ''
if (isRelativeSpan(args)) {
this.mode = args.mode
} else {
this.start = (
args as SingleDateSpan | DoubleDateSpan | DoubleDateTimeSpan
).start
this.end = (args as DoubleDateSpan | DoubleDateTimeSpan).end
}
}
/**
* Create a span from a encoded string or undefined if it can't be decoded.
*
* Decode from string format:
* - relative `SpanMode` mode name
* - absolute single date: 'YYYY-MM-DD'
* - absolute date span: 'YYYY-MM-DD,YYYY-MM-DD'
* - absolute date time span: 'YYYY-MM-DDThh:mm:ss.FFF zzzz,YYYY-MM-DDThh:mm:ss.FFF zzzz'
*
* @param str The string to decode.
* @returns The decoded span or undefined.
*/
public static fromStr(str: string): HSpan | undefined {
const mode = SpanMode[str as SpanMode]
if (mode) {
return new HSpan({ mode })
}
// Parse single or double dates.
const [, start, end] = /^([^,]+)(?:,([^,]+))?$/.exec(str) ?? []
// Absolute single date: 'YYYY-MM-DD'.
if (start && !end) {
const startVal = ZincReader.readValue(start)
if (valueIsKind<HDate>(startVal, Kind.Date)) {
return new HSpan({
start: startVal,
})
}
}
// Absolute date span: 'YYYY-MM-DD,YYYY-MM-DD' or
// absolute date time span: 'YYYY-MM-DDThh:mm:ss.FFF zzzz,YYYY-MM-DDThh:mm:ss.FFF zzzz'
if (start && end) {
const startVal = ZincReader.readValue(start)
const endVal = ZincReader.readValue(end)
if (
valueIsKind<HDate>(startVal, Kind.Date) &&
valueIsKind<HDate>(endVal, Kind.Date)
) {
return new HSpan({
start: startVal,
end: endVal,
})
}
if (
valueIsKind<HDateTime>(startVal, Kind.DateTime) &&
valueIsKind<HDateTime>(endVal, Kind.DateTime)
) {
return new HSpan({
start: startVal,
end: endVal,
})
}
}
return undefined
}
/**
* @returns Encodes the span as a string value.
*/
public toString(): string {
if (this.isRelative()) {
return this.mode as string
}
if (this.start && !this.end) {
return this.start.toZinc()
}
return `${this.start?.toZinc()},${this.end?.toZinc()}`
}
/**
* @returns an Axon representation of this span.
*/
public toAxon(): string {
return `toSpan(${HStr.make(this.toString()).toAxon()})`
}
/**
* @returns A JSON representation of a span.
*/
public toJSON(): SpanJsonData {
const data: SpanJsonData = {}
if (this.isRelative()) {
data.mode = this.mode
} else {
if (this.start) {
data.start = this.start.toJSON()
}
if (this.end) {
data.end = this.end.toJSON()
}
}
return data
}
/**
* @returns True if the span is relative.
*/
public isRelative(): boolean {
return !!this.mode
}
/**
* Convert the span to a [Luxon](https://moment.github.io/luxon/index.html) interval.
*
* The `haystack-core` has zero runtime dependencies and supports web, node and deno environments.
* However vanilla JavaScript lacks support for various calendar functions required to
* make `HSpan` useful.
*
* Warning: at the time of writing, the first day of the week does not vary between locales with Luxon.
*
* ```typescript
* import { DateTime, Interval } from 'luxon'
*
* ...
* const interval = new HSpan({ mode: SpanMode.today })
* .toLuxonInterval({ DateTime, Interval })
* ```
*
* @link [Luxon](https://moment.github.io/luxon/index.html)
*
* @param options.DateTime The luxon DateTime class.
* @param options.Interval The luxon Interval class.
* @returns A luxon interval.
*/
public toLuxonInterval<IntervalType>(
{
DateTime,
Interval,
}: {
DateTime: any
Interval: {
fromDateTimes(start: any, end: any): IntervalType
}
},
_now?: Date
): IntervalType {
const isValidTimeZone = (timezone: string): boolean =>
!!DateTime.now().setZone(timezone).isValid
const getTimeZone = (timezone: string): string =>
HDateTime.getIANATimeZone(timezone, isValidTimeZone) || 'local'
if (this.isRelative()) {
const now = DateTime.fromJSDate(_now ?? new Date(), {
zone: getTimeZone(this.timezone),
})
switch (this.mode) {
case SpanMode.today:
return Interval.fromDateTimes(
now.startOf('day'),
now.endOf('day')
)
case SpanMode.yesterday:
const yesterday = now.minus({ days: 1 })
return Interval.fromDateTimes(
yesterday.startOf('day'),
yesterday.endOf('day')
)
case SpanMode.thisWeek:
return Interval.fromDateTimes(
now.startOf('week'),
now.endOf('week')
)
case SpanMode.lastWeek:
const lastWeek = now.minus({ week: 1 })
return Interval.fromDateTimes(
lastWeek.startOf('week'),
lastWeek.endOf('week')
)
case SpanMode.thisMonth:
return Interval.fromDateTimes(
now.startOf('month'),
now.endOf('month')
)
case SpanMode.lastMonth:
const lastMonth = now.minus({ month: 1 })
return Interval.fromDateTimes(
lastMonth.startOf('month'),
lastMonth.endOf('month')
)
case SpanMode.thisQuarter:
return Interval.fromDateTimes(
now.startOf('quarter'),
now.endOf('quarter')
)
case SpanMode.lastQuarter:
const lastQuarter = now.minus({ quarter: 1 })
return Interval.fromDateTimes(
lastQuarter.startOf('quarter'),
lastQuarter.endOf('quarter')
)
case SpanMode.thisYear:
return Interval.fromDateTimes(
now.startOf('year'),
now.endOf('year')
)
case SpanMode.lastYear:
const lastYear = now.minus({ year: 1 })
return Interval.fromDateTimes(
lastYear.startOf('year'),
lastYear.endOf('year')
)
default:
throw new Error(`Invalid relative span ${this.mode}`)
}
} else {
if (valueIsKind<HDate>(this.start, Kind.Date) && !this.end) {
const start = DateTime.fromJSDate(this.start.date, {
zone: this.timezone,
})
return Interval.fromDateTimes(
start.startOf('day'),
start.endOf('day')
)
} else if (this.start && this.end) {
const start = valueIsKind<HDate>(this.start, Kind.Date)
? DateTime.fromJSDate(this.start.date, {
zone: getTimeZone(this.timezone),
}).startOf('day')
: DateTime.fromJSDate(this.start.date, {
zone: getTimeZone(
this.start.timezone || this.timezone
),
})
const end = valueIsKind<HDate>(this.end, Kind.Date)
? DateTime.fromJSDate(this.end.date, {
zone: getTimeZone(this.timezone),
}).endOf('day')
: DateTime.fromJSDate(this.end.date, {
zone: getTimeZone(
this.end.timezone || this.timezone
),
})
return Interval.fromDateTimes(start, end)
} else {
throw new Error('Invalid absolute span')
}
}
}
/**
* Convert the span to a moment range.
*
* ```typescript
* import * as Moment from 'moment-timezone'
* import { extendMoment } from 'moment-range'
* const moment = extendMoment(Moment)
* ...
* const range = new HSpan({ mode: SpanMode.today })
* .toMomentRange(moment)
* ```
*
* @link [Moment with timezones](https://momentjs.com/timezone/docs/)
* @link [Moment range](https://github.com/rotaready/moment-range)
*/
toMomentRange<MomentType>(
moment: () => MomentType,
_now?: Date
): MomentType {
const _moment = moment as any
const isValidTimeZone = (timezone: string): boolean =>
!!_moment.tz.zone(timezone)
const getTimeZone = (timezone: string): string =>
HDateTime.getIANATimeZone(timezone, isValidTimeZone) ||
_moment.tz.guess()
const now = _moment(_now ?? new Date()).tz(getTimeZone(this.timezone))
if (this.isRelative()) {
switch (this.mode) {
case SpanMode.today:
return _moment.range(
now.clone().startOf('day'),
now.clone().endOf('day')
)
case SpanMode.yesterday:
return _moment.range(
now.clone().subtract(1, 'day').startOf('day'),
now.clone().subtract(1, 'day').endOf('day')
)
case SpanMode.thisWeek:
return _moment.range(
now.clone().startOf('week'),
now.clone().endOf('week')
)
case SpanMode.lastWeek:
return _moment.range(
now.clone().subtract(1, 'week').startOf('week'),
now.clone().subtract(1, 'week').endOf('week')
)
case SpanMode.thisMonth:
return _moment.range(
now.clone().startOf('month'),
now.clone().endOf('month')
)
case SpanMode.lastMonth:
return _moment.range(
now.clone().subtract(1, 'month').startOf('month'),
now.clone().subtract(1, 'month').endOf('month')
)
case SpanMode.thisQuarter:
return _moment.range(
now.clone().startOf('quarter'),
now.clone().endOf('quarter')
)
case SpanMode.lastQuarter:
return _moment.range(
now.clone().subtract(1, 'quarter').startOf('quarter'),
now.clone().subtract(1, 'quarter').endOf('quarter')
)
case SpanMode.thisYear:
return _moment.range(
now.clone().startOf('year'),
now.clone().endOf('year')
)
case SpanMode.lastYear:
return _moment.range(
now.clone().subtract(1, 'year').startOf('year'),
now.clone().subtract(1, 'year').endOf('year')
)
default:
throw new Error(`Invalid relative span ${this.mode}`)
}
} else {
if (valueIsKind<HDate>(this.start, Kind.Date) && !this.end) {
const start = _moment(this.start.date).tz(
getTimeZone(this.timezone)
)
return _moment.range(
start.clone().startOf('day'),
start.clone().endOf('day')
)
} else if (this.start && this.end) {
const start = valueIsKind<HDate>(this.start, Kind.Date)
? _moment(this.start.date)
.tz(getTimeZone(this.timezone))
.startOf('day')
: _moment(this.start.date).tz(
getTimeZone(this.start.timezone || this.timezone)
)
const end = valueIsKind<HDate>(this.end, Kind.Date)
? _moment(this.end.date)
.tz(getTimeZone(this.timezone))
.endOf('day')
: _moment(this.end.date).tz(
getTimeZone(this.end.timezone || this.timezone)
)
return _moment.range(start, end)
} else {
throw new Error('Invalid absolute span')
}
}
}
}