forked from deephaven/web-client-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DateInputUtils.ts
48 lines (45 loc) · 1.4 KB
/
DateInputUtils.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
import { SelectionSegment } from './MaskedInput';
export function getNextNumberSegmentValue(
delta: number,
segmentValue: string,
lowerBound: number,
upperBound: number,
length: number
): string {
const modValue = upperBound - lowerBound + 1;
const newSegmentValue =
((((parseInt(segmentValue, 10) - delta - lowerBound) % modValue) +
modValue) %
modValue) +
lowerBound;
return `${newSegmentValue}`.padStart(length, '0');
}
export function getNextSegmentValue(
range: SelectionSegment,
delta: number,
segmentValue: string
): string {
const { selectionStart } = range;
if (selectionStart === 0) {
return getNextNumberSegmentValue(delta, segmentValue, 1900, 2099, 4);
}
if (selectionStart === 5) {
return getNextNumberSegmentValue(delta, segmentValue, 1, 12, 2);
}
if (selectionStart === 8) {
return getNextNumberSegmentValue(delta, segmentValue, 1, 31, 2);
}
if (selectionStart === 11) {
// Hours input
return getNextNumberSegmentValue(delta, segmentValue, 0, 23, 2);
}
if (selectionStart === 17 || selectionStart === 14) {
// Minutes/seconds input
return getNextNumberSegmentValue(delta, segmentValue, 0, 59, 2);
}
if (selectionStart === 20 || selectionStart === 24 || selectionStart === 28) {
// Milli, micro, and nanosecond input
return getNextNumberSegmentValue(delta, segmentValue, 0, 999, 3);
}
return segmentValue;
}