UNPKG

2.71 kBJavaScriptView Raw
1"use strict";
2// Copyright 2022 Google LLC
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.
15Object.defineProperty(exports, "__esModule", { value: true });
16exports.Duration = void 0;
17/**
18 * Duration class with an interface similar to the tc39 Temporal
19 * proposal. Since it's not fully finalized, and polyfills have
20 * inconsistent compatibility, for now this shim class will be
21 * used to set durations in Pub/Sub.
22 *
23 * This class will remain here for at least the next major version,
24 * eventually to be replaced by the tc39 Temporal built-in.
25 *
26 * https://tc39.es/proposal-temporal/docs/duration.html
27 */
28class Duration {
29 constructor(millis) {
30 this.millis = millis;
31 }
32 /**
33 * Calculates the total number of units of type 'totalOf' that would
34 * fit inside this duration.
35 */
36 totalOf(totalOf) {
37 switch (totalOf) {
38 case 'hour':
39 return this.millis / Duration.hourInMillis;
40 case 'minute':
41 return this.millis / Duration.minuteInMillis;
42 case 'second':
43 return this.millis / Duration.secondInMillis;
44 case 'millisecond':
45 return this.millis;
46 default:
47 throw new Error(`Invalid unit in call to totalOf(): ${totalOf}`);
48 }
49 }
50 /**
51 * Creates a Duration from a DurationLike, which is an object
52 * containing zero or more of the following: hours, seconds,
53 * minutes, millis.
54 */
55 static from(durationLike) {
56 var _a, _b, _c, _d;
57 let millis = (_a = durationLike.millis) !== null && _a !== void 0 ? _a : 0;
58 millis += ((_b = durationLike.seconds) !== null && _b !== void 0 ? _b : 0) * Duration.secondInMillis;
59 millis += ((_c = durationLike.minutes) !== null && _c !== void 0 ? _c : 0) * Duration.minuteInMillis;
60 millis += ((_d = durationLike.hours) !== null && _d !== void 0 ? _d : 0) * Duration.hourInMillis;
61 return new Duration(millis);
62 }
63}
64exports.Duration = Duration;
65Duration.secondInMillis = 1000;
66Duration.minuteInMillis = Duration.secondInMillis * 60;
67Duration.hourInMillis = Duration.minuteInMillis * 60;
68//# sourceMappingURL=temporal.js.map
\No newline at end of file