-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
270 lines (218 loc) · 9.21 KB
/
index.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
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
import isInteger = require("is-integer");
import trunc = require("math-trunc");
import {isIntegerInRange, isNonNegativeFinite} from "./util";
/** A length of time. */
export interface Duration {
hours: number;
minutes: number;
seconds: number;
}
export type DurationLike = Readonly<Partial<Duration>>;
export function isValid(duration: DurationLike): boolean {
const hours = duration.hours == null ? 0 : duration.hours;
const minutes = duration.minutes == null ? 0 : duration.minutes;
const seconds = duration.seconds == null ? 0 : duration.seconds;
return isNonNegativeFinite(hours)
&& isNonNegativeFinite(minutes)
&& isNonNegativeFinite(seconds);
}
export function validate(duration: DurationLike): void {
if (!isValid(duration)) {
throw new TypeError("Invalid duration");
}
}
export function isNormal(duration: Readonly<Duration>): boolean {
return isInteger(duration.hours)
&& isIntegerInRange(duration.minutes, 0, 59)
&& isNonNegativeFinite(duration.seconds)
&& duration.seconds < 60;
}
export function normalize(duration: DurationLike): Duration {
return fromSeconds(toSeconds(duration));
}
export const zero: Duration = {hours: 0, minutes: 0, seconds: 0};
export function toSeconds(duration: DurationLike): number {
const hours = duration.hours == null ? 0 : duration.hours;
const minutes = duration.minutes == null ? 0 : duration.minutes;
const seconds = duration.seconds == null ? 0 : duration.seconds;
return hours * 3600 + minutes * 60 + seconds;
}
export function fromSeconds(seconds: number): Duration {
const hours = Math.floor(seconds / 3600);
seconds -= hours * 3600;
const minutes = Math.floor(seconds / 60);
seconds -= minutes * 60;
return {hours, minutes, seconds};
}
export function toMinutes(duration: DurationLike): number {
const hours = duration.hours == null ? 0 : duration.hours;
const minutes = duration.minutes == null ? 0 : duration.minutes;
const seconds = duration.seconds == null ? 0 : duration.seconds;
return hours * 60 + minutes + seconds / 60;
}
export function fromMinutes(minutes: number): Duration {
const hours = trunc(minutes / 60);
const seconds = (minutes % 1) * 60;
minutes = trunc(minutes - hours * 60);
return {hours, minutes, seconds};
}
export function toHours(duration: DurationLike): number {
const hours = duration.hours == null ? 0 : duration.hours;
const minutes = duration.minutes == null ? 0 : duration.minutes;
const seconds = duration.seconds == null ? 0 : duration.seconds;
return hours + minutes / 60 + seconds / 3600;
}
export function fromHours(hours: number): Duration {
const truncatedHours = trunc(hours);
const minutes = (hours - truncatedHours) * 60;
const truncatedMinutes = trunc(minutes);
const seconds = (minutes - truncatedMinutes) * 60;
return {hours: truncatedHours, minutes: truncatedMinutes, seconds};
}
export function parsehhmmss(duration: string): Duration | null {
const matches = /^\s*(?:(?:([0-9]+):)?([0-9]+):)?([0-9]+(?:\.[0-9]*)?|\.[0-9]+)\s*$/.exec(duration);
if (matches == null) {
return null;
} else {
const hours = matches[1] == null
? 0
: parseInt(matches[1], 10);
const minutes = matches[2] == null
? 0
: parseInt(matches[2], 10);
const seconds = matches[3] == null
? 0
: parseFloat(matches[3]);
return normalize({hours, minutes, seconds});
}
}
export function parsehhmm(duration: string): Duration | null {
const matches = /^\s*(?:([0-9]+):)?([0-9]+(?:\.[0-9]*)?|\.[0-9]+)\s*$/.exec(duration);
if (matches == null) {
return null;
} else {
const hours = matches[1] == null
? 0
: parseInt(matches[1], 10);
const minutes = matches[2] == null
? 0
: parseFloat(matches[2]);
return normalize({hours, minutes});
}
}
export function formathhmmss(duration: DurationLike): string {
const {hours, minutes, seconds} = normalize(duration);
return "" + hours + ":"
+ ("0" + minutes).substr(-2) + ":"
+ ("0" + seconds).replace(/^0?([0-9]{2})/, "$1");
}
export function formathhmmssFixed(duration: DurationLike, fractionDigits = 0): string {
const {hours, minutes, seconds} = normalize(duration);
return "" + hours + ":"
+ ("0" + minutes).substr(-2) + ":"
+ ("0" + seconds.toFixed(fractionDigits)).replace(/^0?([0-9]{2})/, "$1");
}
export function formathhmmssFixedFn(fractionDigits = 0): (duration: DurationLike) => string {
return duration => formathhmmssFixed(duration, fractionDigits);
}
export function formathhmm(duration: DurationLike): string {
const n = normalize(duration);
const {hours} = n;
const minutes = n.minutes + n.seconds / 60;
return "" + hours + ":" + ("0" + minutes).replace(/^0?([0-9]{2})/, "$1");
}
export function formathhmmFixed(duration: DurationLike, fractionDigits = 0): string {
const n = normalize(duration);
const {hours} = n;
const minutes = n.minutes + n.seconds / 60;
return "" + hours + ":"
+ ("0" + minutes.toFixed(fractionDigits)).replace(/^0?([0-9]{2})/, "$1");
}
export function formathhmmFixedFn(fractionDigits = 0): (duration: DurationLike) => string {
return duration => formathhmmFixed(duration, fractionDigits);
}
export function formatmmss(duration: DurationLike): string {
const n = normalize(duration);
const minutes = n.hours * 60 + n.minutes;
const {seconds} = n;
return "" + minutes + ":" + ("0" + seconds).replace(/^0?([0-9]{2})/, "$1");
}
export function formatmmssFixed(duration: DurationLike, fractionDigits = 0): string {
const n = normalize(duration);
const minutes = n.hours * 60 + n.minutes;
const {seconds} = n;
return "" + minutes + ":"
+ ("0" + seconds.toFixed(fractionDigits)).replace(/^0?([0-9]{2})/, "$1");
}
export function formatmmssFixedFn(fractionDigits = 0): (duration: DurationLike) => string {
return duration => formatmmssFixed(duration, fractionDigits);
}
export function formatHoursFixed(duration: DurationLike, fractionDigits = 0): string {
return toHours(duration).toFixed(fractionDigits);
}
export function formatHoursFixedFn(fractionDigits = 0): (duration: DurationLike) => string {
return duration => formatHoursFixed(duration, fractionDigits);
}
export function formatMinutesFixed(duration: DurationLike, fractionDigits = 0): string {
return toMinutes(duration).toFixed(fractionDigits);
}
export function formatMinutesFixedFn(fractionDigits = 0): (duration: DurationLike) => string {
return duration => formatMinutesFixed(duration, fractionDigits);
}
export function formatSecondsFixed(duration: DurationLike, fractionDigits = 0): string {
return toSeconds(duration).toFixed(fractionDigits);
}
export function formatSecondsFixedFn(fractionDigits = 0): (duration: DurationLike) => string {
return duration => formatSecondsFixed(duration, fractionDigits);
}
export function add(a: DurationLike, b: DurationLike): Duration {
return fromSeconds(toSeconds(a) + toSeconds(b));
}
export function addFn(b: DurationLike): (a: DurationLike) => Duration {
return a => add(a, b);
}
export function subtract(a: DurationLike, b: DurationLike): Duration {
return fromSeconds(toSeconds(a) - toSeconds(b));
}
export function subtractFn(b: DurationLike): (a: DurationLike) => Duration {
return a => subtract(a, b);
}
export function multiply(duration: DurationLike, scale: number): Duration {
return fromSeconds(toSeconds(duration) * scale);
}
export function multiplyFn(scale: number): (duration: DurationLike) => Duration {
return duration => multiply(duration, scale);
}
export function divide(duration: DurationLike, divisor: number): Duration {
return fromSeconds(toSeconds(duration) / divisor);
}
export function divideFn(divisor: number): (duration: DurationLike) => Duration {
return duration => divide(duration, divisor);
}
export function roundToSecond(duration: DurationLike): Duration {
return fromSeconds(Math.round(toSeconds(duration)));
}
export function roundToSeconds(duration: DurationLike, seconds: number): Duration {
return fromSeconds(Math.round(toSeconds(duration) / seconds) * seconds);
}
export function roundToSecondsFn(seconds: number): (duration: DurationLike) => Duration {
return duration => roundToSeconds(duration, seconds);
}
export function roundToMinute(duration: DurationLike): Duration {
return fromMinutes(Math.round(toMinutes(duration)));
}
export function roundToMinutes(duration: DurationLike, minutes: number): Duration {
return fromMinutes(Math.round(toMinutes(duration) / minutes) * minutes);
}
export function roundToMinutesFn(minutes: number): (duration: DurationLike) => Duration {
return duration => roundToMinutes(duration, minutes);
}
export function roundToHour(duration: DurationLike): Duration {
return fromHours(Math.round(toHours(duration)));
}
export function roundToHours(duration: DurationLike, hours: number): Duration {
return fromHours(Math.round(toHours(duration) / hours) * hours);
}
export function roundToHoursFn(hours: number): (duration: DurationLike) => Duration {
return duration => roundToHours(duration, hours);
}