summaryrefslogtreecommitdiffstats
path: root/src/wwwroot/scripts/prototypes.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/wwwroot/scripts/prototypes.js')
-rw-r--r--src/wwwroot/scripts/prototypes.js76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/wwwroot/scripts/prototypes.js b/src/wwwroot/scripts/prototypes.js
new file mode 100644
index 0000000..9496022
--- /dev/null
+++ b/src/wwwroot/scripts/prototypes.js
@@ -0,0 +1,76 @@
+"use strict";
+Date.prototype.addDays = function (days) {
+ let date = new Date(this.valueOf());
+ date.setDate(date.getDate() + days);
+ return date;
+};
+
+Date.prototype.substractDays = function (days) {
+ let date = new Date(this.valueOf());
+ date.setDate(date.getDate() - days);
+ return date;
+};
+
+Storage.prototype.removeRegex = function (r) {
+ if (!r) return;
+ let n = sessionStorage.length;
+ while (n--) {
+ let key = sessionStorage.key(n);
+ let rx = new RegExp(r);
+ if (rx.test(key)) {
+ sessionStorage.removeItem(key);
+ }
+ }
+};
+
+Storage.prototype.setStringified = function (key, value) {
+ let stringified = JSON.stringify(value);
+ sessionStorage.setItem(key, stringified);
+};
+
+Storage.prototype.getParsedValue = function (key) {
+ try {
+ let value = sessionStorage.getItem(key);
+ return JSON.parse(value);
+ } catch (e) {
+ return null;
+ }
+};
+
+//"Hello, {name}, are you feeling {adjective}?".formatUnicorn({name:"Gabriel", adjective: "OK"});
+String.prototype.formatUnicorn = function () {
+ let str = this.toString();
+ if (arguments.length) {
+ let t = typeof arguments[0];
+ let key;
+ let args = ("string" === t || "number" === t) ?
+ Array.prototype.slice.call(arguments)
+ : arguments[0];
+ for (key in args) {
+ str = str.replace(new RegExp("\\{" + key + "\\}", "gi"), args[key]);
+ }
+ }
+ return str;
+};
+
+//"Hello, {0}, are you feeling {1}?".format("Gabriel", "OK");
+String.prototype.format = function () {
+ let args = arguments;
+ return this.replace(/{(\d+)}/g, function (match, number) {
+ return typeof args[number] != 'undefined'
+ ? args[number]
+ : match
+ ;
+ });
+};
+
+String.prototype.toHash = function () {
+ let hash = 0, i, chr;
+ if (this.length === 0) return hash;
+ for (i = 0; i < this.length; i++) {
+ chr = this.charCodeAt(i);
+ hash = ((hash << 5) - hash) + chr;
+ hash |= 0; // Convert to 32bit integer
+ }
+ return hash;
+};