-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
515 lines (457 loc) · 18.1 KB
/
index.ts
File metadata and controls
515 lines (457 loc) · 18.1 KB
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
514
515
/** @file Data types and functions for working with abstract times of day. */
import {hasProperty} from "unknown";
import isIntegerInRange from "is-integer-in-range";
import * as format from "@softwareventures/format-time";
import type {TimeFormatter} from "@softwareventures/format-time";
import type {Comparator} from "@softwareventures/ordered";
import {Comparison} from "@softwareventures/ordered";
import {mapOptional} from "@softwareventures/nullable";
import {JsDate} from "./js-date";
/** An abstract time of day with no associated timezone or date. */
export interface Time {
/** Type discriminator identifying the object as a `Time`. */
readonly type: "Time";
/** The hours component of the time of day. Should be an integer in the
* range `0`-`23`. */
readonly hours: number;
/** The minutes component of the time of day. Should be an integer in the
* range `0`-`59`. */
readonly minutes: number;
/** The seconds component of the time of day. Should be in the range
* `0`-`60`, inclusive of `0` but exclusive of `60`. May be fractional
* to represent an instant in time with sub-second accuracy. */
readonly seconds: number;
}
/** Options for creating a {@link Time}.
*
* An instance of {@link Time} may always be used in place of `TimeOptions`. */
export type TimeOptions = Partial<Time>;
/** Returns `true` if the specified value has the shape of a `Time` object.
*
* The `hours`, `minutes` and `seconds` fields may be non-integers or outside
* the expected range, meaning that the object may not represent a valid time.
*
* To test if the object represents a valid time, call {@link isValid} or
* {@link isValidTime}. */
export function isTime(value: unknown): value is Time {
return (
typeof value === "object" &&
value != null &&
hasProperty(value, "type") &&
value.type === "Time" &&
hasProperty(value, "hours") &&
typeof value.hours === "number" &&
hasProperty(value, "minutes") &&
typeof value.minutes === "number" &&
hasProperty(value, "seconds") &&
typeof value.seconds === "number"
);
}
/** Tests if the specified value is a {@link Time} object representing a valid
* time.
*
* Returns `true` if the value has the shape of a `Time` object, the `hours` and
* `minutes` fields are integers within the expected range, and `seconds`
* is a number within the expected range. */
export function isValidTime(value: unknown): value is Time {
return isTime(value) && isValid(value);
}
/** Tests if the specified {@link Time} object represents a valid time.
*
* Returns true if `hours` and `minutes` are integers within the expected
* range, and `seconds` is a number within the expected range.
*
* Times returned by functions in this library are always valid. */
export function isValid(time: Time): boolean {
return (
(!hasProperty(time, "type") || time.type === "Time") &&
isIntegerInRange(time.hours, 0, 23) &&
isIntegerInRange(time.minutes, 0, 59) &&
time.seconds >= 0 &&
time.seconds < 60
);
}
/** Tests if the specified {@link Time} object represents a valid time.
*
* Returns true if `hours` and `minutes` are integers within the expected
* range, and `seconds` is a number within the expected range.
*
* Times returned by functions in this library are always valid. */
export const isTimeValid = isValid;
/**
* Asserts that the specified {@link Time} object represents a valid time.
*
* Times returned by functions in this library are always valid.
*
* @throws {Error} if any of the `hour` or `minute` fields are non-integers,
* or if any of the `hour`, `minute` or `second` fields are outside the
* valid range.
*/
export function validate(time: Time): void {
if (!isValid(time)) {
throw new Error("Invalid time");
}
}
/**
* Asserts that the specified {@link Time} object represents a valid time.
*
* Times returned by functions in this library are always valid.
*
* Alias of {@link validate}, useful for disambiguation from similar functions
* that operate on other types.
*
* @throws {Error} if any of the `hour` or `minute` fields are non-integers,
* or if any of the `hour`, `minute` or `second` fields are outside the
* valid range.
*/
export const validateTime = validate;
/** Creates a {@link Time} with the specified options.
*
* If any numeric components are unspecified, they default to zero.
*
* If any numeric components are outside the expected range, then they
* will roll over into the next larger component. If the time as a whole
* is outside the 24-hour range, then the time will wrap around by as
* many 24-hour periods as needed to put it in the valid range.
*
* @throws {Error} if any of the numeric components are non-finite. */
export function time(time: TimeOptions): Time {
return fromReferenceSeconds(toReferenceSeconds(time));
}
/** Normalizes the specified {@link Time} object so that it represents a valid
* time.
*
* If any numeric components are unspecified, they default to zero.
*
* If any numeric components are outside the expected range, then they
* will roll over into the next larger component. If the time as a whole
* is outside the 24-hour range, then the time will wrap around by as
* many 24-hour periods as needed to put it in the valid range.
*
* Alias of {@link time}. Calling the function by this name instead might make
* code clearer in cases where the purpose is to normalize an existing `Time`
* object.
*
* @throws {Error} if any of the numeric components are non-finite. */
export const normalize = time;
/** Normalizes the specified {@link Time} object so that it represents a valid
* time.
*
* If any numeric components are unspecified, they default to zero.
*
* If any numeric components are outside the expected range, then they
* will roll over into the next larger component. If the time as a whole
* is outside the 24-hour range, then the time will wrap around by as
* many 24-hour periods as needed to put it in the valid range.
*
* Alias of {@link time}. Calling the function by this name instead might make
* code clearer in cases where the purpose is to normalize an existing `Time`
* object.
*
* @throws {Error} if any of the numeric components are non-finite. */
export const normalizeTime = time;
/** Converts the specified {@link Time} to a count of seconds since
* midnight. */
export function toReferenceSeconds(time: TimeOptions): number {
const hours = time.hours ?? 0;
const minutes = time.minutes ?? 0;
const seconds = time.seconds ?? 0;
return (86400 + ((hours * 3600 + minutes * 60 + seconds) % 86400)) % 86400;
}
/** Converts the specified {@link Time} to a count of seconds since
* midnight.
*
* Alias of {@link toReferenceSeconds}, useful for disambiguation from similar
* functions that operate on other types. */
export const timeToReferenceSeconds = toReferenceSeconds;
/** Creates a {@link Time} corresponding to the specified count of seconds
* since midnight.
*
* @throws {Error} if seconds is not a finite value. */
export function fromReferenceSeconds(seconds: number): Time {
if (!isFinite(seconds)) {
throw new Error("Non-finite seconds");
}
const hours = Math.floor(seconds / 3600);
const seconds2 = seconds - hours * 3600;
const minutes = Math.floor(seconds2 / 60);
const seconds3 = seconds2 - minutes * 60;
return {type: "Time", hours, minutes, seconds: seconds3};
}
/** Creates a {@link Time} corresponding to the specified count of seconds
* since midnight.
*
* Alias of {@link fromReferenceSeconds}, useful for disambiguation from
* similar functions that operate on other types.
*
* @throws {Error} if seconds is not a finite value. */
export const timeFromReferenceSeconds = fromReferenceSeconds;
/** Tests if two {@link Time}s are equal. */
export function equal(a: TimeOptions, b: TimeOptions): boolean {
return toReferenceSeconds(a) === toReferenceSeconds(b);
}
/** Tests if two {@link Time}s are equal.
*
* Alias of {@link equal}, useful for disambiguation from other equality
* functions. */
export const timeEqual = equal;
/** Tests if two {@link Time}s are equal.
*
* Curried variant of {@link equal}. */
export function equalFn(b: TimeOptions): (a: TimeOptions) => boolean {
return a => equal(a, b);
}
/** Tests if two {@link Time}s are equal.
*
* Curried variant of {@link timeEqual}. */
export const timeEqualFn = equalFn;
/** Tests if two {@link Time}s are not equal. */
export function notEqual(a: TimeOptions, b: TimeOptions): boolean {
return toReferenceSeconds(a) !== toReferenceSeconds(b);
}
/** Tests if two {@link Time}s are not equal.
*
* Alias of {@link notEqual}, useful for disambiguation from other inequality
* functions. */
export const timeNotEqual = notEqual;
/** Tests if two {@link Time}s are not equal.
*
* Curried variant of {@link notEqual}. */
export function notEqualFn(b: TimeOptions): (a: TimeOptions) => boolean {
return a => notEqual(a, b);
}
/** Tests if two {@link Time}s are not equal.
*
* Curried variant of {@link timeNotEqual}. */
export const timeNotEqualFn = notEqualFn;
/** Compares two {@link Time}s.
*
* Time `a` is considered to be `before` time `b` if time `a` is
* earlier in the day. */
export const compare: Comparator<TimeOptions> = (a, b) => {
const as = toReferenceSeconds(a);
const bs = toReferenceSeconds(b);
if (as < bs) {
return Comparison.before;
} else if (as > bs) {
return Comparison.after;
} else if (as === bs) {
return Comparison.equal;
} else {
return Comparison.undefined;
}
};
/** Compares two {@link Time}s.
*
* Time `a` is considered to be `before` time `b` if time `a` is
* earlier in the day.
*
* Alias of {@link compare}, useful for disambiguation from other comparison
* functions. */
export const timeCompare = compare;
/** Compares two {@link Time}s.
*
* Time `a` is considered to be `before` time `b` if time `a` is
* earlier in the day.
*
* Curried variant of {@link compare}. */
export function compareFn(b: TimeOptions): (a: TimeOptions) => Comparison {
return a => compare(a, b);
}
/** Compares two {@link Time}s.
*
* Time `a` is considered to be `before` time `b` if time `a` is
* earlier in the day.
*
* Curried variant of {@link timeCompare}. */
export const timeCompareFn = compareFn;
/** Tests if {@link Time} `a` is earlier in the day than {@link Time} `b`. */
export function before(a: TimeOptions, b: TimeOptions): boolean {
return toReferenceSeconds(a) < toReferenceSeconds(b);
}
/** Tests if {@link Time} `a` is earlier in the day than {@link Time} `b`.
*
* Alias of {@link before}, useful for disambiguation from similar functions
* that operate on other date/time types. */
export const timeBefore = before;
/** Tests if {@link Time} `a` is earlier in the day than {@link Time} `b`.
*
* Curried variant of {@link before}. */
export function beforeFn(b: TimeOptions): (a: TimeOptions) => boolean {
return a => before(a, b);
}
/** Tests if {@link Time} `a` is earlier in the day than {@link Time} `b`.
*
* Curried variant of {@link timeBefore}. */
export const timeBeforeFn = beforeFn;
/** Tests if {@link Time} `a` is equal to or earlier in the day than
* {@link Time} `b`. */
export function beforeOrEqual(a: TimeOptions, b: TimeOptions): boolean {
return toReferenceSeconds(a) <= toReferenceSeconds(b);
}
/** Tests if {@link Time} `a` is equal to or earlier in the day than
* {@link Time} `b`.
*
* Alias of {@link beforeOrEqual}, useful for disambiguation from similar
* functions that operate on other date/time types. */
export const timeBeforeOrEqual = beforeOrEqual;
/** Tests if {@link Time} `a` is equal to or earlier in the day than
* {@link Time} `b`.
*
* Curried variant of {@link beforeOrEqual}. */
export function beforeOrEqualFn(b: TimeOptions): (a: TimeOptions) => boolean {
return a => beforeOrEqual(a, b);
}
/** Tests if {@link Time} `a` is equal to or earlier in the day than
* {@link Time} `b`.
*
* Curried variant of {@link timeBeforeOrEqual}. */
export const timeBeforeOrEqualFn = beforeOrEqualFn;
/** Tests if {@link Time} `a` is later in the day than {@link Time} `b`. */
export function after(a: TimeOptions, b: TimeOptions): boolean {
return toReferenceSeconds(a) > toReferenceSeconds(b);
}
/** Tests if {@link Time} `a` is later in the day than {@link Time} `b`.
*
* Alias of {@link after}, useful for disambiguation from similar functions
* that operate on other date/time types. */
export const timeAfter = after;
/** Tests if {@link Time} `a` is later in the day than {@link Time} `b`.
*
* Curried variant of {@link after}. */
export function afterFn(b: TimeOptions): (a: TimeOptions) => boolean {
return a => after(a, b);
}
/** Tests if {@link Time} `a` is later in the day than {@link Time} `b`.
*
* Curried variant of {@link timeAfter}. */
export const timeAfterFn = afterFn;
/** Returns the current time of day according to UTC. */
export function nowUtc(): Time {
const now = new JsDate();
return {
type: "Time",
hours: now.getUTCHours(),
minutes: now.getUTCMinutes(),
seconds: now.getUTCSeconds() + 0.001 * now.getUTCMilliseconds()
};
}
/** Returns the current time of day according to UTC.
*
* Alias of {@link nowUtc}, useful for disambiguation from similar functions
* that operate on other date/time types. */
export const timeNowUtc = nowUtc;
/** Returns the current time of day according to the device's local
* timezone. */
export function nowDeviceLocal(): Time {
const now = new JsDate();
return {
type: "Time",
hours: now.getHours(),
minutes: now.getMinutes(),
seconds: now.getSeconds() + 0.001 * now.getMilliseconds()
};
}
/** Returns the current time of day according to the device's local timezone.
*
* Alias of {@link nowDeviceLocal}, useful for disambiguation from similar
* functions that operate on other date/time types. */
export const timeNowDeviceLocal = nowDeviceLocal;
/**
* Parses a {@link Time} from text in ISO 8601 format.
*
* The ISO 8601 text must not specify a time zone offset.
*
* If the specified text is not a valid ISO 8601 time then this function
* returns `null`.
*
* Both extended `Thh:mm:ss.sss` and basic `Thhmmss.sss` ISO 8601 formats are
* accepted, and the initial `T` is optional in both cases. The seconds field
* may be omitted, and the minutes field may also be omitted if the seconds
* field is omitted. Omitted fields default to zero.
*/
export function parseIso8601(text: string): Time | null {
const match = /^T?(\d{2})(?::?(\d{2})(?::?(\d{2}(?:\.\d*)?))?)?$/iu.exec(text);
if (match?.[1] == null) {
return null;
}
const hours = parseInt(match[1], 10);
const minutes = mapOptional(match[2], text => parseInt(text, 10)) ?? 0;
const seconds = mapOptional(match[3], text => parseFloat(text.replace(",", "."))) ?? 0;
return {type: "Time", hours, minutes, seconds};
}
/**
* Parses a {@link Time} from text in ISO 8601 format.
*
* The ISO 8601 text must not specify a time zone offset.
*
* If the specified text is not a valid ISO 8601 time then this function
* returns `null`.
*
* Both extended `Thh:mm:ss.sss` and basic `Thhmmss.sss` ISO 8601 formats are
* accepted, and the initial `T` is optional in both cases. The seconds field
* may be omitted, and the minutes field may also be omitted if the seconds
* field is omitted. Omitted fields default to zero.
*
* Alias of {@link parseIso8601}, useful for disambiguation from similar
* functions that operate on other date/time types. */
export const parseTimeIso8601 = parseIso8601;
export type {Iso8601Options, TimeFormatter} from "@softwareventures/format-time";
/** Returns a {@link TimeFormatter} that formats the specified {@link Time} as
* ISO 8601, with the specified options.
*
* By default, the {@link Time} is formatted in the "extended" ISO 8601 format,
* with the leading `"T"`, and without rounding, for example
* `"T11:57:23.723615"`.
*
* If the `format` option is set to `"basic"`, then the colons are omitted,
* for example `"T115723.723615"`.
*
* If the `round` option is set to `"seconds"`, then the time is rounded down
* to the next lower second, for example `"T11:57:23"`.
*
* If the `round` option is set to `"ms"`, then the time is rounded down to
* the next lower millisecond, for example `"T11:57:23.723"`.
*
* If the `leadingT` option is set to `false`, then the leading `"T"` is
* omitted, for example `"11:57:23.363215"`.
*
* For other formats, see `@softwareventures/format-time`. */
export const formatIso8601 = format.iso8601;
/** Returns a {@link TimeFormatter} that formats the specified {@link Time} as
* ISO 8601, with the specified options.
*
* By default, the {@link Time} is formatted in the "extended" ISO 8601 format,
* with the leading `"T"`, and without rounding, for example
* `"T11:57:23.723615"`.
*
* If the `format` option is set to `"basic"`, then the colons are omitted,
* for example `"T115723.723615"`.
*
* If the `round` option is set to `"seconds"`, then the time is rounded down
* to the next lower second, for example `"T11:57:23"`.
*
* If the `round` option is set to `"ms"`, then the time is rounded down to
* the next lower millisecond, for example `"T11:57:23.723"`.
*
* If the `leadingT` option is set to `false`, then the leading `"T"` is
* omitted, for example `"11:57:23.363215"`.
*
* Alias of {@link formatIso8601}, useful for disambiguation from similar
* functions that operate on other date/time types.
*
* For other formats, see `@softwareventures/format-time`. */
export const formatTimeIso8601 = format.iso8601;
/** Formats the specified {@link Time} as ISO 8601 extended, rounded down to
* the next lower second, and with no leading `"T"`.
*
* This format is intended to be reasonable for display to humans. */
export const formatHumanIso8601 = format.humanIso8601;
/** Formats the specified {@link Time} as ISO 8601 extended, rounded down to
* the next lower second, and with no leading `"T"`.
*
* This format is intended to be reasonable for display to humans.
*
* Alias of {@link formatHumanIso8601}, useful for disambiguation from similar
* functions that operate on other date/time types. */
export const formatTimeHumanIso8601 = format.humanIso8601;