Created
October 31, 2021 03:41
-
-
Save ethansocal/ce6e4918ee8a6c01ace05ecc28e78f69 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function time_parser(input: string): number { | |
/** | |
* @name time_parser | |
* @description Parses a string of time into a number of seconds | |
* @param {string} input - String of time to parse | |
* | |
* @return {number} - Number of seconds | |
*/ | |
let total_seconds = 0; | |
const times: { [index: string]: number } = { | |
"y": 365 * 24 * 60 * 60, | |
"w": 7 * 24 * 60 * 60, | |
"d": 24 * 60 * 60, | |
"h": 60 * 60, | |
"m": 60, | |
"s": 1 | |
} | |
let current_time = ""; | |
for (var i = 0; i < input.length; i++) { | |
if (times[input[i]] !== undefined) { | |
total_seconds += times[input[i]] * parseInt(current_time); | |
current_time = ""; | |
} else { | |
current_time += input[i]; | |
} | |
} | |
return total_seconds; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment