-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonly-time.js
200 lines (179 loc) · 4.07 KB
/
only-time.js
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
'use strict';
/**
* @author Przemyslaw Hardyn | przemyslawhardyn.com
* Converting time to minutes, seconds and back.
* https://github.com/phardyn/only-time
*/
class OnlyTime {
/**
* @desc Class constructor
*
* @param {string} [separator]
*/
constructor(separator = ':') {
if (separator && String(separator).length > 1 || !isNaN(separator)) {
this.throwError(
'Invalid separator - has to be one character and cannot be a digit.'
);
}
this.separator = separator;
}
/**
* @desc Add 0 upfront, return 00 or integer.
* For formatting hours, minutes and seconds.
*
* @param {integer} int
*
* @returns {string}
*/
format(int) {
let n = `${int}`;
if (int === 0) {
n = '00';
} else if (int < 10) {
n = `0${int}`;
}
return n;
}
/**
* @desc Convert time to minutes.
* Example: '12:12' -> 732
*
* @param {string} time
*
* @returns {integer}
*/
toMinutes(time) {
this.checkTimeMinutes(time);
const t = time.split(this.separator);
return Number(t[0]) * 60 + Number(t[1]);
}
/**
* @desc Convert time to seconds.
* Example: '01:12:01' -> 4321
*
* @param {string} time
*
* @returns {integer}
*/
toSeconds(time) {
this.checkTimeSeconds(time);
const t = time.split(this.separator);
return Number(t[0]) * 3600 + Number(t[1]) * 60 + Number(t[2]);
}
/**
* @desc Convert minutes to time.
* Example: 129 -> '02:09'
*
* @param {integer} minutes
*
* @returns {string}
*/
fromMinutes(minutes) {
this.checkMinutes(minutes);
const modulo = this.checkModulo(minutes);
return String.prototype.concat(
this.format((minutes - modulo) / 60),
this.separator,
this.format(modulo)
);
}
/**
* @desc Convert seconds to time.
* Example: 3661 -> '01:01:01'
*
* @param {integer} seconds
*
* @returns {string}
*/
fromSeconds(seconds) {
this.checkSeconds(seconds);
const modulo = this.checkModulo(seconds);
return String.prototype.concat(
this.fromMinutes((seconds - modulo) / 60),
this.separator,
this.format(modulo)
);
}
/**
* @desc Verify time modulo for minutes and seconds
*
* @param {integer} time
*
* @returns {number}
*/
checkModulo(time) {
return time % 60;
}
/**
* @desc Check if number of minutes is valid.
*
* @param {integer} minutes
*/
checkMinutes(minutes) {
if (minutes < 0 || minutes > 1439) {
this.throwError(
`Wrong number of minutes: ${minutes}. Should be in the range [0, 1439].`
);
}
}
/**
* @desc Check if number of seconds is valid.
*
* @param {integer} seconds
*/
checkSeconds(seconds) {
if (seconds < 0 || seconds > 86399) {
this.throwError(
`Wrong number of seconds: ${seconds}. Should be in the range [0, 86399].`
);
}
}
/**
* @desc Check time string for errors.
*
* @param {string} time
*/
checkTimeMinutes(time) {
this.minutesTest = this.minutesTest
? this.minutesTest
: new RegExp(`([01]\\d|2[0-3])\\${this.separator}([0-5]\\d)`);
if (time.length !== 5 || !this.minutesTest.exec(time)) {
this.throwError(`Invalid time with minutes: ${time}.`);
}
}
/**
* @desc Check time string with seconds for errors.
*
* @param {string} time
*/
checkTimeSeconds(time) {
this.secondsTest = this.secondsTest
? this.secondsTest
: new RegExp(
`([01]\\d|2[0-3])\\${this.separator}([0-5]\\d)\\${this.separator}([0-5]\\d)`
);
if (time.length !== 8 || !this.secondsTest.exec(time)) {
this.throwError(`Invalid time with seconds: ${time}.`);
}
}
/**
* @desc Throw custom only-time error in the console
*
* @param {string} reason
*/
throwError(reason) {
throw new Error(`only-time: ${reason}`);
}
/**
* @desc Alias to keep the backward compatibility.
*
* @param {integer} minutes
*
* @returns {string}
*/
toTime(minutes) {
return this.fromMinutes(minutes);
}
}
module.exports = OnlyTime;