This commit is contained in:
n1474335 2022-06-03 13:10:59 +01:00
commit 36e66ad5b4
2 changed files with 36 additions and 27 deletions

View File

@ -19,31 +19,37 @@ import OperationError from "../errors/OperationError.mjs";
* @returns {string} * @returns {string}
* *
* @example * @example
* // returns "00010000 00100000 00110000" * // returns "00001010 00010100 00011110"
* toBinary([10,20,30]); * toBinary([10,20,30]);
* *
* // returns "00010000 00100000 00110000" * // returns "00001010:00010100:00011110"
* toBinary([10,20,30], ":"); * toBinary([10,20,30], "Colon");
*
* // returns "1010:10100:11110"
* toBinary([10,20,30], "Colon", 0);
*/ */
export function toBinary(data, delim="Space", padding=8) { export function toBinary(data, delim="Space", padding=8) {
if (data === undefined || data === null)
throw new OperationError("Unable to convert to binary: Empty input data enocuntered");
delim = Utils.charRep(delim); delim = Utils.charRep(delim);
let output = ""; let output = "";
if (data.length) { // array if (data.length) { // array
for (let i = 0; i < data.length; i++) { for (let i = 0; i < data.length; i++) {
output += data[i].toString(2).padStart(padding, "0") + delim; output += data[i].toString(2).padStart(padding, "0");
if (i !== data.length - 1) output += delim;
} }
} else if (typeof data === "number") { // Single value } else if (typeof data === "number") { // Single value
return data.toString(2).padStart(padding, "0"); return data.toString(2).padStart(padding, "0");
} else { } else {
return ""; return "";
} }
if (delim.length) { if (delim.length) {
return output.slice(0, -delim.length); // Remove the delimiter from the end of the string.
} else { output = output.slice(0, -delim.length);
return output;
} }
return output;
} }
@ -57,10 +63,10 @@ export function toBinary(data, delim="Space", padding=8) {
* *
* @example * @example
* // returns [10,20,30] * // returns [10,20,30]
* fromBinary("00010000 00100000 00110000"); * fromBinary("00001010 00010100 00011110");
* *
* // returns [10,20,30] * // returns [10,20,30]
* fromBinary("00010000:00100000:00110000", "Colon"); * fromBinary("00001010:00010100:00011110", "Colon");
*/ */
export function fromBinary(data, delim="Space", byteLen=8) { export function fromBinary(data, delim="Space", byteLen=8) {
if (byteLen < 1 || Math.round(byteLen) !== byteLen) if (byteLen < 1 || Math.round(byteLen) !== byteLen)

View File

@ -37,24 +37,27 @@ class ToUpperCase extends Operation {
* @returns {string} * @returns {string}
*/ */
run(input, args) { run(input, args) {
if (!args || args.length === 0) {
throw new OperationException("No capitalization scope was provided.");
}
const scope = args[0]; const scope = args[0];
if (scope === "All") {
switch (scope) { return input.toUpperCase();
case "Word": }
return input.replace(/(\b\w)/gi, function(m) { const scopeRegex = {
return m.toUpperCase(); "Word": /(\b\w)/gi,
}); "Sentence": /(?:\.|^)\s*(\b\w)/gi,
case "Sentence": "Paragraph": /(?:\n|^)\s*(\b\w)/gi
return input.replace(/(?:\.|^)\s*(\b\w)/gi, function(m) { }[ scope ];
return m.toUpperCase(); if (scopeRegex !== undefined) {
}); // Use the regexes to capitalize the input.
case "Paragraph": return input.replace(scopeRegex, function(m) {
return input.replace(/(?:\n|^)\s*(\b\w)/gi, function(m) { return m.toUpperCase();
return m.toUpperCase(); });
}); }
case "All": /* falls through */ else {
default: // The selected scope was invalid.
return input.toUpperCase(); throw new OperationError("Unrecognized capitalization scope");
} }
} }