diff --git a/src/helper_functions.js b/src/helper_functions.js new file mode 100644 index 0000000000000000000000000000000000000000..4e1b0512a33792790ae6f7a15fee43f80d3547a6 --- /dev/null +++ b/src/helper_functions.js @@ -0,0 +1,39 @@ +/** + * Contains misc helper functions. + */ + + +/** + * Update strings to have an equivalent the "format()" found in Python strings. + * + * Found from https://stackoverflow.com/a/4974690 + */ +String.prototype.format = function() { + let i = 0, args = arguments; + return this.replace(/{}/g, function () { + return typeof args[i] != 'undefined' ? args[i++] : ''; + }); +} + + +/** + * Gets human-readible string of current datetime. + * + * Date parsing logic from https://stackoverflow.com/a/5416970 + * Extra padding logic found at https://stackoverflow.com/a/20460414 + */ +function getCurrentDate() { + let currentDate = new Date; + + return '{}-{}-{} {}:{}:{}'.format( + ('0000' + currentDate.getFullYear()).slice(-4), + ('00' + (currentDate.getMonth() + 1)).slice(-2), + ('00' + currentDate.getDate()).slice(-2), + ('00' + currentDate.getHours()).slice(-2), + ('00' + currentDate.getMinutes()).slice(-2), + ('00' + currentDate.getSeconds()).slice(-2), + ); +} + + +module.exports = getCurrentDate;