index.js
3.14 KB
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
'use strict'
module.exports = {
fromMs,
fromS,
toMs,
toS
}
const zeroFill = require('zero-fill')
// Time units with their corresponding values in miliseconds
const HOUR = 3600000
const MINUTE = 60000
const SECOND = 1000
const TIME_FORMAT_ERRMSG = 'Time format error'
// =============================================================================
// Export functions
// =============================================================================
function fromMs (ms, format = 'mm:ss') {
if (typeof ms !== 'number' || Number.isNaN(ms)) {
throw new Error('NaN error')
}
let absMs = Math.abs(ms)
let negative = (ms < 0)
let hours = Math.floor(absMs / HOUR)
let minutes = Math.floor(absMs % HOUR / MINUTE)
let seconds = Math.floor(absMs % MINUTE / SECOND)
let miliseconds = Math.floor(absMs % SECOND)
return formatTime({
negative, hours, minutes, seconds, miliseconds
}, format)
}
function fromS (s, format = 'mm:ss') {
if (typeof s !== 'number' || Number.isNaN(s)) {
throw new Error('NaN error')
}
let ms = s * SECOND
return fromMs(ms, format)
}
function toMs (time, format = 'mm:ss') {
let re
if (['mm:ss', 'mm:ss.sss', 'hh:mm:ss', 'hh:mm:ss.sss'].includes(format)) {
re = /^(-)?(?:(\d\d+):)?(\d\d):(\d\d)(\.\d+)?$/
} else if (format === 'hh:mm') {
re = /^(-)?(\d\d):(\d\d)(?::(\d\d)(?:(\.\d+))?)?$/
} else {
throw new Error(TIME_FORMAT_ERRMSG)
}
let result = re.exec(time)
if (!result) throw new Error()
let negative = result[1] === '-'
let hours = result[2] | 0
let minutes = result[3] | 0
let seconds = result[4] | 0
let miliseconds = Math.floor(1000 * result[5] | 0)
if (minutes > 60 || seconds > 60) {
throw new Error()
}
return (negative ? -1 : 1) * (
hours * HOUR + minutes * MINUTE + seconds * SECOND + miliseconds
)
}
function toS (time, format = 'mm:ss') {
let ms = toMs(time, format)
return Math.floor(ms / SECOND)
}
// =============================================================================
// Utility functions
// =============================================================================
function formatTime (time, format) {
let showMs
let showSc
let showHr
switch (format.toLowerCase()) {
case 'hh:mm:ss.sss':
showMs = true
showSc = true
showHr = true
break
case 'hh:mm:ss':
showMs = !(!time.miliseconds)
showSc = true
showHr = true
break
case 'hh:mm':
showMs = !(!time.miliseconds)
showSc = showMs || !(!time.seconds)
showHr = true
break
case 'mm:ss':
showMs = !(!time.miliseconds)
showSc = true
showHr = !(!time.hours)
break
case 'mm:ss.sss':
showMs = true
showSc = true
showHr = !(!time.hours)
break
default:
throw new Error(TIME_FORMAT_ERRMSG)
}
let hh = zeroFill(2, time.hours)
let mm = zeroFill(2, time.minutes)
let ss = zeroFill(2, time.seconds)
let sss = zeroFill(3, time.miliseconds)
return (time.negative ? '-' : '') + (showHr ? (
showMs ? `${hh}:${mm}:${ss}.${sss}` : showSc ? `${hh}:${mm}:${ss}` : `${hh}:${mm}`
) : (
showMs ? `${mm}:${ss}.${sss}` : `${mm}:${ss}`
))
}