From af496fd2e0c0967b386a4d30e7158b9f823409aa Mon Sep 17 00:00:00 2001 From: Brandon Rodriguez <brodriguez8774@gmail.com> Date: Wed, 5 Aug 2020 17:41:24 -0400 Subject: [PATCH] Create a few helper functions for later use --- src/helper_functions.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/helper_functions.js diff --git a/src/helper_functions.js b/src/helper_functions.js new file mode 100644 index 0000000..4e1b051 --- /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; -- GitLab