mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-22 07:46:16 -04:00
Minor adjustments
This commit is contained in:
parent
ae1b12c120
commit
2574a63975
2 changed files with 31 additions and 29 deletions
|
@ -19,27 +19,29 @@ 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) return "";
|
if (!data) throw new OperationError("Empty input data enocuntered");
|
||||||
|
|
||||||
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") + delim;
|
output += data[i].toString(2).padStart(padding, "0");
|
||||||
|
if (i !== data.length - 1) output += delim;
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -53,10 +55,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)
|
||||||
|
|
|
@ -38,24 +38,24 @@ class ToUpperCase extends Operation {
|
||||||
*/
|
*/
|
||||||
run(input, args) {
|
run(input, args) {
|
||||||
const scope = args[0];
|
const scope = args[0];
|
||||||
|
if (scope === "All") {
|
||||||
switch (scope) {
|
|
||||||
case "Word":
|
|
||||||
return input.replace(/(\b\w)/gi, function(m) {
|
|
||||||
return m.toUpperCase();
|
|
||||||
});
|
|
||||||
case "Sentence":
|
|
||||||
return input.replace(/(?:\.|^)\s*(\b\w)/gi, function(m) {
|
|
||||||
return m.toUpperCase();
|
|
||||||
});
|
|
||||||
case "Paragraph":
|
|
||||||
return input.replace(/(?:\n|^)\s*(\b\w)/gi, function(m) {
|
|
||||||
return m.toUpperCase();
|
|
||||||
});
|
|
||||||
case "All": /* falls through */
|
|
||||||
default:
|
|
||||||
return input.toUpperCase();
|
return input.toUpperCase();
|
||||||
}
|
}
|
||||||
|
const scopeRegex = {
|
||||||
|
"Word": /(\b\w)/gi,
|
||||||
|
"Sentence": /(?:\.|^)\s*(\b\w)/gi,
|
||||||
|
"Paragraph": /(?:\n|^)\s*(\b\w)/gi
|
||||||
|
}[ scope ];
|
||||||
|
if (scopeRegex !== undefined) {
|
||||||
|
// Use the regexes to capitalize the input.
|
||||||
|
return input.replace(scopeRegex, function(m) {
|
||||||
|
return m.toUpperCase();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// The selected scope was invalid.
|
||||||
|
throw new OperationError("Unrecognized capitalization scope");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue