|
@@ -1,17 +1,17 @@
|
|
|
+/**
|
|
|
+ * @author n1474335 [n1474335@gmail.com]
|
|
|
+ * @copyright Crown Copyright 2016
|
|
|
+ * @license Apache-2.0
|
|
|
+ */
|
|
|
+
|
|
|
import utf8 from "utf8";
|
|
|
import moment from "moment-timezone";
|
|
|
|
|
|
|
|
|
/**
|
|
|
* Utility functions for use in operations, the core framework and the stage.
|
|
|
- *
|
|
|
- * @author n1474335 [n1474335@gmail.com]
|
|
|
- * @copyright Crown Copyright 2016
|
|
|
- * @license Apache-2.0
|
|
|
- *
|
|
|
- * @namespace
|
|
|
*/
|
|
|
-const Utils = {
|
|
|
+class Utils {
|
|
|
|
|
|
/**
|
|
|
* Translates an ordinal into a character.
|
|
@@ -23,7 +23,7 @@ const Utils = {
|
|
|
* // returns 'a'
|
|
|
* Utils.chr(97);
|
|
|
*/
|
|
|
- chr: function(o) {
|
|
|
+ static chr(o) {
|
|
|
// Detect astral symbols
|
|
|
// Thanks to @mathiasbynens for this solution
|
|
|
// https://mathiasbynens.be/notes/javascript-unicode
|
|
@@ -35,7 +35,7 @@ const Utils = {
|
|
|
}
|
|
|
|
|
|
return String.fromCharCode(o);
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -48,7 +48,7 @@ const Utils = {
|
|
|
* // returns 97
|
|
|
* Utils.ord('a');
|
|
|
*/
|
|
|
- ord: function(c) {
|
|
|
+ static ord(c) {
|
|
|
// Detect astral symbols
|
|
|
// Thanks to @mathiasbynens for this solution
|
|
|
// https://mathiasbynens.be/notes/javascript-unicode
|
|
@@ -62,7 +62,7 @@ const Utils = {
|
|
|
}
|
|
|
|
|
|
return c.charCodeAt(0);
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -88,8 +88,7 @@ const Utils = {
|
|
|
* // returns ["t", "e", "s", "t", 1, 1, 1, 1]
|
|
|
* Utils.padBytesRight("test", 8, 1);
|
|
|
*/
|
|
|
- padBytesRight: function(arr, numBytes, padByte) {
|
|
|
- padByte = padByte || 0;
|
|
|
+ static padBytesRight(arr, numBytes, padByte=0) {
|
|
|
const paddedBytes = new Array(numBytes);
|
|
|
paddedBytes.fill(padByte);
|
|
|
|
|
@@ -98,7 +97,7 @@ const Utils = {
|
|
|
});
|
|
|
|
|
|
return paddedBytes;
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -116,13 +115,12 @@ const Utils = {
|
|
|
* // returns "A long s-"
|
|
|
* Utils.truncate("A long string", 9, "-");
|
|
|
*/
|
|
|
- truncate: function(str, max, suffix) {
|
|
|
- suffix = suffix || "...";
|
|
|
+ static truncate(str, max, suffix="...") {
|
|
|
if (str.length > max) {
|
|
|
str = str.slice(0, max - suffix.length) + suffix;
|
|
|
}
|
|
|
return str;
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -139,11 +137,10 @@ const Utils = {
|
|
|
* // returns "6e"
|
|
|
* Utils.hex(110);
|
|
|
*/
|
|
|
- hex: function(c, length) {
|
|
|
+ static hex(c, length=2) {
|
|
|
c = typeof c == "string" ? Utils.ord(c) : c;
|
|
|
- length = length || 2;
|
|
|
return c.toString(16).padStart(length, "0");
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -160,11 +157,10 @@ const Utils = {
|
|
|
* // returns "01101110"
|
|
|
* Utils.bin(110);
|
|
|
*/
|
|
|
- bin: function(c, length) {
|
|
|
+ static bin(c, length=8) {
|
|
|
c = typeof c == "string" ? Utils.ord(c) : c;
|
|
|
- length = length || 8;
|
|
|
return c.toString(2).padStart(length, "0");
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -174,7 +170,7 @@ const Utils = {
|
|
|
* @param {boolean} [preserveWs=false] - Whether or not to print whitespace.
|
|
|
* @returns {string}
|
|
|
*/
|
|
|
- printable: function(str, preserveWs) {
|
|
|
+ static printable(str, preserveWs=false) {
|
|
|
if (ENVIRONMENT_IS_WEB() && window.app && !window.app.options.treatAsUtf8) {
|
|
|
str = Utils.byteArrayToChars(Utils.strToByteArray(str));
|
|
|
}
|
|
@@ -185,7 +181,7 @@ const Utils = {
|
|
|
str = str.replace(re, ".");
|
|
|
if (!preserveWs) str = str.replace(wsRe, ".");
|
|
|
return str;
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -201,7 +197,7 @@ const Utils = {
|
|
|
* // returns "\n"
|
|
|
* Utils.parseEscapedChars("\\n");
|
|
|
*/
|
|
|
- parseEscapedChars: function(str) {
|
|
|
+ static parseEscapedChars(str) {
|
|
|
return str.replace(/(\\)?\\([bfnrtv0'"]|x[\da-fA-F]{2}|u[\da-fA-F]{4}|u\{[\da-fA-F]{1,6}\})/g, function(m, a, b) {
|
|
|
if (a === "\\") return "\\"+b;
|
|
|
switch (b[0]) {
|
|
@@ -232,7 +228,7 @@ const Utils = {
|
|
|
return String.fromCharCode(parseInt(b.substr(1), 16));
|
|
|
}
|
|
|
});
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -246,9 +242,9 @@ const Utils = {
|
|
|
* // returns "\[example\]"
|
|
|
* Utils.escapeRegex("[example]");
|
|
|
*/
|
|
|
- escapeRegex: function(str) {
|
|
|
+ static escapeRegex(str) {
|
|
|
return str.replace(/([.*+?^=!:${}()|[\]/\\])/g, "\\$1");
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -267,7 +263,7 @@ const Utils = {
|
|
|
* // returns ["a", "b", "c", "d", "0", "-", "3"]
|
|
|
* Utils.expandAlphRange("a-d0\\-3")
|
|
|
*/
|
|
|
- expandAlphRange: function(alphStr) {
|
|
|
+ static expandAlphRange(alphStr) {
|
|
|
const alphArr = [];
|
|
|
|
|
|
for (let i = 0; i < alphStr.length; i++) {
|
|
@@ -291,7 +287,7 @@ const Utils = {
|
|
|
}
|
|
|
}
|
|
|
return alphArr;
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -312,7 +308,7 @@ const Utils = {
|
|
|
* // returns [208, 159, 209, 128, 208, 184, 208, 178, 208, 181, 209, 130]
|
|
|
* Utils.convertToByteArray("0JfQtNGA0LDQstGB0YLQstGD0LnRgtC1", "base64");
|
|
|
*/
|
|
|
- convertToByteArray: function(str, type) {
|
|
|
+ static convertToByteArray(str, type) {
|
|
|
switch (type.toLowerCase()) {
|
|
|
case "hex":
|
|
|
return Utils.fromHex(str);
|
|
@@ -324,7 +320,7 @@ const Utils = {
|
|
|
default:
|
|
|
return Utils.strToByteArray(str);
|
|
|
}
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -345,7 +341,7 @@ const Utils = {
|
|
|
* // returns "ÐдÑавÑÑвÑйÑе"
|
|
|
* Utils.convertToByteString("0JfQtNGA0LDQstGB0YLQstGD0LnRgtC1", "base64");
|
|
|
*/
|
|
|
- convertToByteString: function(str, type) {
|
|
|
+ static convertToByteString(str, type) {
|
|
|
switch (type.toLowerCase()) {
|
|
|
case "hex":
|
|
|
return Utils.byteArrayToChars(Utils.fromHex(str));
|
|
@@ -357,7 +353,7 @@ const Utils = {
|
|
|
default:
|
|
|
return str;
|
|
|
}
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -374,7 +370,7 @@ const Utils = {
|
|
|
* // returns [228,189,160,229,165,189]
|
|
|
* Utils.strToByteArray("你好");
|
|
|
*/
|
|
|
- strToByteArray: function(str) {
|
|
|
+ static strToByteArray(str) {
|
|
|
const byteArray = new Array(str.length);
|
|
|
let i = str.length, b;
|
|
|
while (i--) {
|
|
@@ -384,7 +380,7 @@ const Utils = {
|
|
|
if (b > 255) return Utils.strToUtf8ByteArray(str);
|
|
|
}
|
|
|
return byteArray;
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -400,7 +396,7 @@ const Utils = {
|
|
|
* // returns [228,189,160,229,165,189]
|
|
|
* Utils.strToUtf8ByteArray("你好");
|
|
|
*/
|
|
|
- strToUtf8ByteArray: function(str) {
|
|
|
+ static strToUtf8ByteArray(str) {
|
|
|
const utf8Str = utf8.encode(str);
|
|
|
|
|
|
if (str.length !== utf8Str.length) {
|
|
@@ -412,7 +408,7 @@ const Utils = {
|
|
|
}
|
|
|
|
|
|
return Utils.strToByteArray(utf8Str);
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -428,7 +424,7 @@ const Utils = {
|
|
|
* // returns [20320,22909]
|
|
|
* Utils.strToCharcode("你好");
|
|
|
*/
|
|
|
- strToCharcode: function(str) {
|
|
|
+ static strToCharcode(str) {
|
|
|
const charcode = [];
|
|
|
|
|
|
for (let i = 0; i < str.length; i++) {
|
|
@@ -446,7 +442,7 @@ const Utils = {
|
|
|
}
|
|
|
|
|
|
return charcode;
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -462,7 +458,7 @@ const Utils = {
|
|
|
* // returns "你好"
|
|
|
* Utils.byteArrayToUtf8([228,189,160,229,165,189]);
|
|
|
*/
|
|
|
- byteArrayToUtf8: function(byteArray) {
|
|
|
+ static byteArrayToUtf8(byteArray) {
|
|
|
const str = Utils.byteArrayToChars(byteArray);
|
|
|
try {
|
|
|
const utf8Str = utf8.decode(str);
|
|
@@ -479,7 +475,7 @@ const Utils = {
|
|
|
// If it fails, treat it as ANSI
|
|
|
return str;
|
|
|
}
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -495,14 +491,14 @@ const Utils = {
|
|
|
* // returns "你好"
|
|
|
* Utils.byteArrayToChars([20320,22909]);
|
|
|
*/
|
|
|
- byteArrayToChars: function(byteArray) {
|
|
|
+ static byteArrayToChars(byteArray) {
|
|
|
if (!byteArray) return "";
|
|
|
let str = "";
|
|
|
for (let i = 0; i < byteArray.length;) {
|
|
|
str += String.fromCharCode(byteArray[i++]);
|
|
|
}
|
|
|
return str;
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -516,17 +512,17 @@ const Utils = {
|
|
|
* // returns "hello"
|
|
|
* Utils.arrayBufferToStr(Uint8Array.from([104,101,108,108,111]).buffer);
|
|
|
*/
|
|
|
- arrayBufferToStr: function(arrayBuffer, utf8=true) {
|
|
|
+ static arrayBufferToStr(arrayBuffer, utf8=true) {
|
|
|
const byteArray = Array.prototype.slice.call(new Uint8Array(arrayBuffer));
|
|
|
return utf8 ? Utils.byteArrayToUtf8(byteArray) : Utils.byteArrayToChars(byteArray);
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
|
* Base64's the input byte array using the given alphabet, returning a string.
|
|
|
*
|
|
|
* @param {byteArray|Uint8Array|string} data
|
|
|
- * @param {string} [alphabet]
|
|
|
+ * @param {string} [alphabet="A-Za-z0-9+/="]
|
|
|
* @returns {string}
|
|
|
*
|
|
|
* @example
|
|
@@ -536,15 +532,14 @@ const Utils = {
|
|
|
* // returns "SGVsbG8="
|
|
|
* Utils.toBase64("Hello");
|
|
|
*/
|
|
|
- toBase64: function(data, alphabet) {
|
|
|
+ static toBase64(data, alphabet="A-Za-z0-9+/=") {
|
|
|
if (!data) return "";
|
|
|
if (typeof data == "string") {
|
|
|
data = Utils.strToByteArray(data);
|
|
|
}
|
|
|
|
|
|
- alphabet = alphabet ?
|
|
|
- Utils.expandAlphRange(alphabet).join("") :
|
|
|
- "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
|
|
|
+ alphabet = Utils.expandAlphRange(alphabet).join("");
|
|
|
+
|
|
|
let output = "",
|
|
|
chr1, chr2, chr3,
|
|
|
enc1, enc2, enc3, enc4,
|
|
@@ -571,14 +566,14 @@ const Utils = {
|
|
|
}
|
|
|
|
|
|
return output;
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
|
* UnBase64's the input string using the given alphabet, returning a byte array.
|
|
|
*
|
|
|
* @param {byteArray} data
|
|
|
- * @param {string} [alphabet]
|
|
|
+ * @param {string} [alphabet="A-Za-z0-9+/="]
|
|
|
* @param {string} [returnType="string"] - Either "string" or "byteArray"
|
|
|
* @param {boolean} [removeNonAlphChars=true]
|
|
|
* @returns {byteArray}
|
|
@@ -590,18 +585,12 @@ const Utils = {
|
|
|
* // returns [72, 101, 108, 108, 111]
|
|
|
* Utils.fromBase64("SGVsbG8=", null, "byteArray");
|
|
|
*/
|
|
|
- fromBase64: function(data, alphabet, returnType, removeNonAlphChars) {
|
|
|
- returnType = returnType || "string";
|
|
|
-
|
|
|
+ static fromBase64(data, alphabet="A-Za-z0-9+/=", returnType="string", removeNonAlphChars=true) {
|
|
|
if (!data) {
|
|
|
return returnType === "string" ? "" : [];
|
|
|
}
|
|
|
|
|
|
- alphabet = alphabet ?
|
|
|
- Utils.expandAlphRange(alphabet).join("") :
|
|
|
- "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
|
|
|
- if (removeNonAlphChars === undefined)
|
|
|
- removeNonAlphChars = true;
|
|
|
+ alphabet = Utils.expandAlphRange(alphabet).join("");
|
|
|
|
|
|
let output = [],
|
|
|
chr1, chr2, chr3,
|
|
@@ -638,7 +627,7 @@ const Utils = {
|
|
|
}
|
|
|
|
|
|
return returnType === "string" ? Utils.byteArrayToUtf8(output) : output;
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -656,11 +645,9 @@ const Utils = {
|
|
|
* // returns "0a:14:1e"
|
|
|
* Utils.toHex([10,20,30], ":");
|
|
|
*/
|
|
|
- toHex: function(data, delim, padding) {
|
|
|
+ static toHex(data, delim=" ", padding=2) {
|
|
|
if (!data) return "";
|
|
|
|
|
|
- delim = typeof delim == "string" ? delim : " ";
|
|
|
- padding = padding || 2;
|
|
|
let output = "";
|
|
|
|
|
|
for (let i = 0; i < data.length; i++) {
|
|
@@ -675,7 +662,7 @@ const Utils = {
|
|
|
return output.slice(0, -delim.length);
|
|
|
else
|
|
|
return output;
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -688,7 +675,7 @@ const Utils = {
|
|
|
* // returns "0a141e"
|
|
|
* Utils.toHex([10,20,30]);
|
|
|
*/
|
|
|
- toHexFast: function(data) {
|
|
|
+ static toHexFast(data) {
|
|
|
if (!data) return "";
|
|
|
|
|
|
const output = [];
|
|
@@ -699,7 +686,7 @@ const Utils = {
|
|
|
}
|
|
|
|
|
|
return output.join("");
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -717,11 +704,10 @@ const Utils = {
|
|
|
* // returns [10,20,30]
|
|
|
* Utils.fromHex("0a:14:1e", "Colon");
|
|
|
*/
|
|
|
- fromHex: function(data, delim, byteLen) {
|
|
|
+ static fromHex(data, delim, byteLen=2) {
|
|
|
delim = delim || (data.indexOf(" ") >= 0 ? "Space" : "None");
|
|
|
- byteLen = byteLen || 2;
|
|
|
if (delim !== "None") {
|
|
|
- const delimRegex = Utils.regexRep[delim];
|
|
|
+ const delimRegex = Utils.regexRep(delim);
|
|
|
data = data.replace(delimRegex, "");
|
|
|
}
|
|
|
|
|
@@ -730,7 +716,7 @@ const Utils = {
|
|
|
output.push(parseInt(data.substr(i, byteLen), 16));
|
|
|
}
|
|
|
return output;
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -743,8 +729,7 @@ const Utils = {
|
|
|
* // returns [["head1", "head2"], ["data1", "data2"]]
|
|
|
* Utils.parseCSV("head1,head2\ndata1,data2");
|
|
|
*/
|
|
|
- parseCSV: function(data) {
|
|
|
-
|
|
|
+ static parseCSV(data) {
|
|
|
let b,
|
|
|
ignoreNext = false,
|
|
|
inString = false,
|
|
@@ -783,26 +768,27 @@ const Utils = {
|
|
|
}
|
|
|
|
|
|
return lines;
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
|
* Removes all HTML (or XML) tags from the input string.
|
|
|
*
|
|
|
* @param {string} htmlStr
|
|
|
- * @param {boolean} removeScriptAndStyle - Flag to specify whether to remove entire script or style blocks
|
|
|
+ * @param {boolean} [removeScriptAndStyle=false]
|
|
|
+ * - Flag to specify whether to remove entire script or style blocks
|
|
|
* @returns {string}
|
|
|
*
|
|
|
* @example
|
|
|
* // returns "Test"
|
|
|
* Utils.stripHtmlTags("<div>Test</div>");
|
|
|
*/
|
|
|
- stripHtmlTags: function(htmlStr, removeScriptAndStyle) {
|
|
|
+ static stripHtmlTags(htmlStr, removeScriptAndStyle=false) {
|
|
|
if (removeScriptAndStyle) {
|
|
|
htmlStr = htmlStr.replace(/<(script|style)[^>]*>.*<\/(script|style)>/gmi, "");
|
|
|
}
|
|
|
return htmlStr.replace(/<[^>]+>/g, "");
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -816,7 +802,7 @@ const Utils = {
|
|
|
* // return "A <script> tag"
|
|
|
* Utils.escapeHtml("A <script> tag");
|
|
|
*/
|
|
|
- escapeHtml: function(str) {
|
|
|
+ static escapeHtml(str) {
|
|
|
const HTML_CHARS = {
|
|
|
"&": "&",
|
|
|
"<": "<",
|
|
@@ -830,7 +816,7 @@ const Utils = {
|
|
|
return str.replace(/[&<>"'/`]/g, function (match) {
|
|
|
return HTML_CHARS[match];
|
|
|
});
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -843,7 +829,7 @@ const Utils = {
|
|
|
* // return "A <script> tag"
|
|
|
* Utils.unescapeHtml("A <script> tag");
|
|
|
*/
|
|
|
- unescapeHtml: function(str) {
|
|
|
+ static unescapeHtml(str) {
|
|
|
const HTML_CHARS = {
|
|
|
"&": "&",
|
|
|
"<": "<",
|
|
@@ -857,7 +843,7 @@ const Utils = {
|
|
|
return str.replace(/&#?x?[a-z0-9]{2,4};/ig, function (match) {
|
|
|
return HTML_CHARS[match] || match;
|
|
|
});
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -882,7 +868,7 @@ const Utils = {
|
|
|
* @param {string} str
|
|
|
* @returns {string}
|
|
|
*/
|
|
|
- encodeURIFragment: function(str) {
|
|
|
+ static encodeURIFragment(str) {
|
|
|
const LEGAL_CHARS = {
|
|
|
"%2D": "-",
|
|
|
"%2E": ".",
|
|
@@ -909,7 +895,7 @@ const Utils = {
|
|
|
return str.replace(/%[0-9A-F]{2}/g, function (match) {
|
|
|
return LEGAL_CHARS[match] || match;
|
|
|
});
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -923,10 +909,10 @@ const Utils = {
|
|
|
* for users.
|
|
|
*
|
|
|
* @param {Object[]} recipeConfig
|
|
|
- * @param {boolean} newline - whether to add a newline after each operation
|
|
|
+ * @param {boolean} [newline=false] - whether to add a newline after each operation
|
|
|
* @returns {string}
|
|
|
*/
|
|
|
- generatePrettyRecipe: function(recipeConfig, newline) {
|
|
|
+ static generatePrettyRecipe(recipeConfig, newline=false) {
|
|
|
let prettyConfig = "",
|
|
|
name = "",
|
|
|
args = "",
|
|
@@ -949,17 +935,17 @@ const Utils = {
|
|
|
if (newline) prettyConfig += "\n";
|
|
|
});
|
|
|
return prettyConfig;
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
|
* Converts a recipe string to the JSON representation of the recipe.
|
|
|
- * Accepts either stringified JSON or bespoke "pretty" recipe format.
|
|
|
+ * Accepts either stringified JSON or the bespoke "pretty" recipe format.
|
|
|
*
|
|
|
* @param {string} recipe
|
|
|
* @returns {Object[]}
|
|
|
*/
|
|
|
- parseRecipeConfig: function(recipe) {
|
|
|
+ static parseRecipeConfig(recipe) {
|
|
|
recipe = recipe.trim();
|
|
|
if (recipe.length === 0) return [];
|
|
|
if (recipe[0] === "[") return JSON.parse(recipe);
|
|
@@ -989,7 +975,7 @@ const Utils = {
|
|
|
recipeConfig.push(op);
|
|
|
}
|
|
|
return recipeConfig;
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -1019,24 +1005,9 @@ const Utils = {
|
|
|
* // returns "5 days"
|
|
|
* Utils.fuzzyTime(456851321);
|
|
|
*/
|
|
|
- fuzzyTime: function(ms) {
|
|
|
+ static fuzzyTime(ms) {
|
|
|
return moment.duration(ms, "milliseconds").humanize();
|
|
|
- },
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * Adds the properties of one Object to another.
|
|
|
- *
|
|
|
- * @param {Object} a
|
|
|
- * @param {Object} b
|
|
|
- * @returns {Object}
|
|
|
- */
|
|
|
- extend: function(a, b){
|
|
|
- for (const key in b)
|
|
|
- if (b.hasOwnProperty(key))
|
|
|
- a[key] = b[key];
|
|
|
- return a;
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -1050,7 +1021,7 @@ const Utils = {
|
|
|
* @param {Object[]} files
|
|
|
* @returns {html}
|
|
|
*/
|
|
|
- displayFilesAsHTML: function(files) {
|
|
|
+ static displayFilesAsHTML(files) {
|
|
|
/* <NL> and <SP> used to denote newlines and spaces in HTML markup.
|
|
|
* If a non-html operation is used, all markup will be removed but these
|
|
|
* whitespace chars will remain for formatting purposes.
|
|
@@ -1131,7 +1102,7 @@ const Utils = {
|
|
|
return html.replace(/(?:(<pre>(?:\n|.)*<\/pre>)|\s{2,})/g, "$1") // Remove whitespace from markup
|
|
|
.replace(/<NL>/g, "\n") // Replace <NP> with newlines
|
|
|
.replace(/<SP>/g, " "); // Replace <SP> with spaces
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -1145,7 +1116,7 @@ const Utils = {
|
|
|
* Utils.parseURIParams("?a=abc&b=123")
|
|
|
* Utils.parseURIParams("#a=abc&b=123")
|
|
|
*/
|
|
|
- parseURIParams: function(paramStr) {
|
|
|
+ static parseURIParams(paramStr) {
|
|
|
if (paramStr === "") return {};
|
|
|
|
|
|
// Cut off ? or # and split on &
|
|
@@ -1167,7 +1138,7 @@ const Utils = {
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -1178,9 +1149,9 @@ const Utils = {
|
|
|
* @param {number} y
|
|
|
* @returns {number}
|
|
|
*/
|
|
|
- mod: function (x, y) {
|
|
|
+ static mod(x, y) {
|
|
|
return ((x % y) + y) % y;
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -1191,12 +1162,12 @@ const Utils = {
|
|
|
* @param {number} y
|
|
|
* @returns {number}
|
|
|
*/
|
|
|
- gcd: function(x, y) {
|
|
|
+ static gcd(x, y) {
|
|
|
if (!y) {
|
|
|
return x;
|
|
|
}
|
|
|
return Utils.gcd(y, x % y);
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -1207,55 +1178,63 @@ const Utils = {
|
|
|
* @param {number} y
|
|
|
* @returns {number}
|
|
|
*/
|
|
|
- modInv: function(x, y) {
|
|
|
+ static modInv(x, y) {
|
|
|
x %= y;
|
|
|
for (let i = 1; i < y; i++) {
|
|
|
if ((x * i) % 26 === 1) {
|
|
|
return i;
|
|
|
}
|
|
|
}
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
|
* A mapping of names of delimiter characters to their symbols.
|
|
|
- * @constant
|
|
|
+ *
|
|
|
+ * @param {string} token
|
|
|
+ * @returns {string}
|
|
|
*/
|
|
|
- charRep: {
|
|
|
- "Space": " ",
|
|
|
- "Comma": ",",
|
|
|
- "Semi-colon": ";",
|
|
|
- "Colon": ":",
|
|
|
- "Line feed": "\n",
|
|
|
- "CRLF": "\r\n",
|
|
|
- "Forward slash": "/",
|
|
|
- "Backslash": "\\",
|
|
|
- "0x": "0x",
|
|
|
- "\\x": "\\x",
|
|
|
- "Nothing (separate chars)": "",
|
|
|
- "None": "",
|
|
|
- },
|
|
|
+ static charRep(token) {
|
|
|
+ return {
|
|
|
+ "Space": " ",
|
|
|
+ "Comma": ",",
|
|
|
+ "Semi-colon": ";",
|
|
|
+ "Colon": ":",
|
|
|
+ "Line feed": "\n",
|
|
|
+ "CRLF": "\r\n",
|
|
|
+ "Forward slash": "/",
|
|
|
+ "Backslash": "\\",
|
|
|
+ "0x": "0x",
|
|
|
+ "\\x": "\\x",
|
|
|
+ "Nothing (separate chars)": "",
|
|
|
+ "None": "",
|
|
|
+ }[token];
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
|
* A mapping of names of delimiter characters to regular expressions which can select them.
|
|
|
- * @constant
|
|
|
+ *
|
|
|
+ * @param {string} token
|
|
|
+ * @returns {RegExp}
|
|
|
*/
|
|
|
- regexRep: {
|
|
|
- "Space": /\s+/g,
|
|
|
- "Comma": /,/g,
|
|
|
- "Semi-colon": /;/g,
|
|
|
- "Colon": /:/g,
|
|
|
- "Line feed": /\n/g,
|
|
|
- "CRLF": /\r\n/g,
|
|
|
- "Forward slash": /\//g,
|
|
|
- "Backslash": /\\/g,
|
|
|
- "0x": /0x/g,
|
|
|
- "\\x": /\\x/g,
|
|
|
- "None": /\s+/g // Included here to remove whitespace when there shouldn't be any
|
|
|
- },
|
|
|
+ static regexRep(token) {
|
|
|
+ return {
|
|
|
+ "Space": /\s+/g,
|
|
|
+ "Comma": /,/g,
|
|
|
+ "Semi-colon": /;/g,
|
|
|
+ "Colon": /:/g,
|
|
|
+ "Line feed": /\n/g,
|
|
|
+ "CRLF": /\r\n/g,
|
|
|
+ "Forward slash": /\//g,
|
|
|
+ "Backslash": /\\/g,
|
|
|
+ "0x": /0x/g,
|
|
|
+ "\\x": /\\x/g,
|
|
|
+ "None": /\s+/g // Included here to remove whitespace when there shouldn't be any
|
|
|
+ }[token];
|
|
|
+ }
|
|
|
|
|
|
-};
|
|
|
+}
|
|
|
|
|
|
export default Utils;
|
|
|
|