-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
154 lines (141 loc) · 4.38 KB
/
main.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
// timey.js
// 2018 Julian Lepinski
//////////////////////////
// extremely core logic //
//////////////////////////
// when running this with the "node main.js xxx" command,
// we pop off the first param so the next part works properly
if (/node/i.test(process.argv[0])) process.argv.shift();
if (process.argv.length === 2) {
var timeIn = process.argv[1];
var inputType = parseTimeInput(timeIn);
if (inputType === "human") {
var timeInOrig = timeIn;
var timeInMS = processHumanTime(timeIn);
console.log(timeInOrig + " in milliseconds is " + timeInMS);
} else if (inputType === "ms") {
var timeInOrig = timeIn;
var timeInHuman = processMilliseconds(timeIn);
console.log(timeInOrig + "ms in human time is " + timeInHuman);
} else {
console.log("whoops: timey can't interpret '" + timeIn + "'");
console.log("timey expects a single argument, i.e.");
console.log("");
console.log(" timey 01:02:03");
console.log("or");
console.log(" timey 230000");
process.exit(1);
}
} else {
console.log("whoops: timey expects a single argument, i.e.");
console.log("");
console.log(" timey 01:02:03");
console.log("or");
console.log(" timey 230000");
process.exit(1);
}
//////////////////////////////
// conversion & convenience //
//////////////////////////////
/**
* Processes a human-readable (i.e. 03:04.20) time string and
* returns the time in milliseconds.
* @param {string} timeIn - A formatted time value
* @returns {integer} The time in milliseconds
*/
function processHumanTime(timeIn) {
var msComponent = 0;
var sComponent = 0;
var mComponent = 0;
var hComponent = 0;
if (timeIn.indexOf(".") !== -1) {
var timeSplit = timeIn.split(".");
msComponent = parseFloat("0." + timeSplit[1]);
msComponent = msComponent * 1000.0;
timeIn = timeSplit[0];
}
var colonSplit = timeIn.split(":");
sComponent = parseInt(colonSplit.pop());
if (colonSplit.length > 0) {
mComponent = parseInt(colonSplit.pop());
}
if (colonSplit.length > 0) {
hComponent = parseInt(colonSplit.pop());
}
var timeInMS = (hComponent * 60 * 60000) + (mComponent * 60000) + (sComponent * 1000) + msComponent;
timeInMS = parseInt(timeInMS);
return timeInMS;
}
/**
* Processes a time in millseconds and returns
* it in a human-readable format
* @param {integer} msIn - A time value in milliseconds
* @returns {string} A formatted time string
*/
function processMilliseconds(msIn) {
var hComponent = ~~(msIn / (60 * 60000));
msIn -= hComponent * (60 * 60000);
var mComponent = ~~(msIn / 60000);
msIn -= mComponent * 60000;
var sComponent = ~~(msIn / 1000);
var msComponent = msIn % 1000;
var humanReadable = pad2(hComponent) + ":" +
pad2(mComponent) + ":" +
pad2(sComponent) + "." +
padToDigits(msComponent, 3);
return humanReadable;
}
/**
* Left-pads a number with zeroes to two digits;
* returns the number untouched if it's >= 2 digits.
* @param {integer} numberIn - The number to pad
* @returns {string} The number, padded to 2 digits
*/
function pad2(numberIn) {
return padToDigits(numberIn, 2);
}
/**
* Left-pads a number with zeroes to an arbitrary number of digits;
* returns the number untouched if its length is >= the digits value.
* @param {integer} numberIn - The number to pad
* @param {integer} digits – The number of digits to pad to
* @returns {string} The number, padded
*/
function padToDigits(numberIn, digits) {
numberIn = "" + numberIn;
while (numberIn.length < digits) {
numberIn = "0" + numberIn;
}
return numberIn;
}
/**
* Parses a time value to determine whether it is
* formatted, or milliseconds (or invalid)
* @param {string} timeInput - The input to parse
* @returns {string} "human" if human input, "ms" if milliseconds,
* and null if invalid / unparseable
*/
function parseTimeInput(timeInput) {
// we're looking for a string that is digit groups separated by at least
// one colon, and optionally one final group separated by a period
var humanRx = /(\d{1,2}:)?\d{1,2}:\d{1,2}(\.\d+)?/g;
// we're looking for a string that is *only* digits
var msRx = /^\d+$/g;
if (humanRx.test(timeInput)) {
return "human";
} else if (msRx.test(timeInput)) {
return "ms";
} else {
return;
}
}
/////////////////////////
// exports for testing //
/////////////////////////
module.exports = {
pad2: pad2,
padToDigits: padToDigits,
processHumanTime: processHumanTime,
processMilliseconds: processMilliseconds,
parseTimeInput: parseTimeInput
};