UNPKG

4.79 kBJavaScriptView Raw
1/*
2 * Copyright 2018 Palantir Technologies, Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16var _a;
17import * as Classes from "./classes";
18import { get12HourFrom24Hour, get24HourFrom12Hour } from "./dateUtils";
19/** describes a component of time. `H:MM:SS.MS` */
20export var TimeUnit;
21(function (TimeUnit) {
22 // NOTE: string enum so we can use it in Record<> type at the end of this file, which requires string keys
23 TimeUnit["HOUR_24"] = "hour24";
24 TimeUnit["HOUR_12"] = "hour12";
25 TimeUnit["MINUTE"] = "minute";
26 TimeUnit["SECOND"] = "second";
27 TimeUnit["MS"] = "ms";
28})(TimeUnit || (TimeUnit = {}));
29/** Returns the given time unit component of the date. */
30export function getTimeUnit(unit, date) {
31 switch (unit) {
32 case TimeUnit.HOUR_24:
33 return date.getHours();
34 case TimeUnit.HOUR_12:
35 return get12HourFrom24Hour(date.getHours());
36 case TimeUnit.MINUTE:
37 return date.getMinutes();
38 case TimeUnit.SECOND:
39 return date.getSeconds();
40 case TimeUnit.MS:
41 return date.getMilliseconds();
42 default:
43 throw Error("Invalid TimeUnit");
44 }
45}
46/** Sets the given time unit to the given time in date object. Modifies given `date` object and returns it. */
47export function setTimeUnit(unit, time, date, isPm) {
48 switch (unit) {
49 case TimeUnit.HOUR_24:
50 date.setHours(time);
51 break;
52 case TimeUnit.HOUR_12:
53 date.setHours(get24HourFrom12Hour(time, isPm));
54 break;
55 case TimeUnit.MINUTE:
56 date.setMinutes(time);
57 break;
58 case TimeUnit.SECOND:
59 date.setSeconds(time);
60 break;
61 case TimeUnit.MS:
62 date.setMilliseconds(time);
63 break;
64 default:
65 throw Error("Invalid TimeUnit");
66 }
67 return date;
68}
69/** Returns true if `time` is a valid value */
70export function isTimeUnitValid(unit, time) {
71 return time != null && !isNaN(time) && getTimeUnitMin(unit) <= time && time <= getTimeUnitMax(unit);
72}
73/** If unit of time is greater than max, returns min. If less than min, returns max. Otherwise, returns time. */
74export function wrapTimeAtUnit(unit, time) {
75 var max = getTimeUnitMax(unit);
76 var min = getTimeUnitMin(unit);
77 if (time > max) {
78 return min;
79 }
80 else if (time < min) {
81 return max;
82 }
83 return time;
84}
85export function getTimeUnitClassName(unit) {
86 return TimeUnitMetadata[unit].className;
87}
88export function getTimeUnitMax(unit) {
89 return TimeUnitMetadata[unit].max;
90}
91export function getTimeUnitMin(unit) {
92 return TimeUnitMetadata[unit].min;
93}
94export function getDefaultMinTime() {
95 return new Date(0, 0, 0, DEFAULT_MIN_HOUR, DEFAULT_MIN_MINUTE, DEFAULT_MIN_SECOND, DEFAULT_MIN_MILLISECOND);
96}
97export function getDefaultMaxTime() {
98 return new Date(0, 0, 0, DEFAULT_MAX_HOUR, DEFAULT_MAX_MINUTE, DEFAULT_MAX_SECOND, DEFAULT_MAX_MILLISECOND);
99}
100var DEFAULT_MIN_HOUR = 0;
101var MERIDIEM_MIN_HOUR = 1;
102var DEFAULT_MIN_MINUTE = 0;
103var DEFAULT_MIN_SECOND = 0;
104var DEFAULT_MIN_MILLISECOND = 0;
105var DEFAULT_MAX_HOUR = 23;
106var MERIDIEM_MAX_HOUR = 12;
107var DEFAULT_MAX_MINUTE = 59;
108var DEFAULT_MAX_SECOND = 59;
109var DEFAULT_MAX_MILLISECOND = 999;
110/**
111 * A datastore (internal to this file) mapping TimeUnits to useful information about them.
112 * Use the `get*` methods above to access these fields.
113 */
114var TimeUnitMetadata = (_a = {},
115 _a[TimeUnit.HOUR_24] = {
116 className: Classes.TIMEPICKER_HOUR,
117 max: DEFAULT_MAX_HOUR,
118 min: DEFAULT_MIN_HOUR,
119 },
120 _a[TimeUnit.HOUR_12] = {
121 className: Classes.TIMEPICKER_HOUR,
122 max: MERIDIEM_MAX_HOUR,
123 min: MERIDIEM_MIN_HOUR,
124 },
125 _a[TimeUnit.MINUTE] = {
126 className: Classes.TIMEPICKER_MINUTE,
127 max: DEFAULT_MAX_MINUTE,
128 min: DEFAULT_MIN_MINUTE,
129 },
130 _a[TimeUnit.SECOND] = {
131 className: Classes.TIMEPICKER_SECOND,
132 max: DEFAULT_MAX_SECOND,
133 min: DEFAULT_MIN_SECOND,
134 },
135 _a[TimeUnit.MS] = {
136 className: Classes.TIMEPICKER_MILLISECOND,
137 max: DEFAULT_MAX_MILLISECOND,
138 min: DEFAULT_MIN_MILLISECOND,
139 },
140 _a);
141//# sourceMappingURL=timeUnit.js.map
\No newline at end of file