UNPKG

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