Revert "Minor adjustments"

This reverts commit 2574a63975.
This commit is contained in:
Michael Rowley 2022-03-23 14:54:36 +00:00
parent 61a19e45cc
commit 82da498c24
2 changed files with 29 additions and 31 deletions

View file

@ -19,29 +19,27 @@ import OperationError from "../errors/OperationError.mjs";
* @returns {string} * @returns {string}
* *
* @example * @example
* // returns "00001010 00010100 00011110" * // returns "00010000 00100000 00110000"
* toBinary([10,20,30]); * toBinary([10,20,30]);
* *
* // returns "00001010:00010100:00011110" * // returns "00010000 00100000 00110000"
* toBinary([10,20,30], "Colon"); * toBinary([10,20,30], ":");
*
* // 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) throw new OperationError("Empty input data enocuntered"); if (!data) return "";
delim = Utils.charRep(delim); delim = Utils.charRep(delim);
let output = ""; let output = "";
for (let i = 0; i < data.length; i++) { for (let i = 0; i < data.length; i++) {
output += data[i].toString(2).padStart(padding, "0"); output += data[i].toString(2).padStart(padding, "0") + delim;
if (i !== data.length - 1) output += delim;
} }
if (delim.length) { if (delim.length) {
// Remove the delimiter from the end of the string. return output.slice(0, -delim.length);
output = output.slice(0, -delim.length); } else {
return output;
} }
return output;
} }
@ -55,10 +53,10 @@ export function toBinary(data, delim="Space", padding=8) {
* *
* @example * @example
* // returns [10,20,30] * // returns [10,20,30]
* fromBinary("00001010 00010100 00011110"); * fromBinary("00010000 00100000 00110000");
* *
* // returns [10,20,30] * // returns [10,20,30]
* fromBinary("00001010:00010100:00011110", "Colon"); * fromBinary("00010000:00100000:00110000", "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

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