Tidied up Base85 ops

This commit is contained in:
n1474335 2018-08-23 22:05:31 +01:00
parent c818370123
commit 8e9fece77d
3 changed files with 15 additions and 14 deletions

View File

@ -12,15 +12,15 @@
export const ALPHABET_OPTIONS = [ export const ALPHABET_OPTIONS = [
{ {
name: "Standard", name: "Standard",
value: "!&quot;#$%&&apos;()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[&bsol;]^_`abcdefghijklmnopqrstu", value: "!-u",
}, },
{ {
name: "Z85 (ZeroMQ)", name: "Z85 (ZeroMQ)",
value: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#", value: "0-9a-zA-Z.#\\-:+=^!/*?&<>()[]{}@%$#",
}, },
{ {
name: "IPv6", name: "IPv6",
value: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|~}", value: "0-9A-Za-z!#$%&()*+\\-;<=>?@^_`{|~}",
} }
]; ];

View File

@ -6,6 +6,7 @@
import Operation from "../Operation"; import Operation from "../Operation";
import OperationError from "../errors/OperationError"; import OperationError from "../errors/OperationError";
import Utils from "../Utils";
import {alphabetName, ALPHABET_OPTIONS} from "../lib/Base85"; import {alphabetName, ALPHABET_OPTIONS} from "../lib/Base85";
/** /**
@ -21,7 +22,7 @@ class FromBase85 extends Operation {
this.name = "From Base85"; this.name = "From Base85";
this.module = "Default"; this.module = "Default";
this.description = "Base85 (similar to Base64) is a notation for encoding arbitrary byte data. It is usually more efficient that Base64.<br><br>This operation decodes data from an ASCII string (with an alphabet of your choosing, presets included).<br><br>e.g. <code>BOu!rD]j7BEbo7</code> becomes <code>hello world</code><br><br>Base85 is commonly used in Adobe's PostScript and PDF file formats."; this.description = "Base85 (also called Ascii85) is a notation for encoding arbitrary byte data. It is usually more efficient that Base64.<br><br>This operation decodes data from an ASCII string (with an alphabet of your choosing, presets included).<br><br>e.g. <code>BOu!rD]j7BEbo7</code> becomes <code>hello world</code><br><br>Base85 is commonly used in Adobe's PostScript and PDF file formats.";
this.infoURL = "https://wikipedia.org/wiki/Ascii85"; this.infoURL = "https://wikipedia.org/wiki/Ascii85";
this.inputType = "string"; this.inputType = "string";
this.outputType = "byteArray"; this.outputType = "byteArray";
@ -40,7 +41,7 @@ class FromBase85 extends Operation {
* @returns {byteArray} * @returns {byteArray}
*/ */
run(input, args) { run(input, args) {
const alphabet = args[0] || ALPHABET_OPTIONS[0].value, const alphabet = Utils.expandAlphRange(args[0]).join(""),
encoding = alphabetName(alphabet), encoding = alphabetName(alphabet),
result = []; result = [];
@ -68,7 +69,7 @@ class FromBase85 extends Operation {
.map((chr, idx) => { .map((chr, idx) => {
const digit = alphabet.indexOf(chr); const digit = alphabet.indexOf(chr);
if (digit < 0 || digit > 84) { if (digit < 0 || digit > 84) {
throw "Invalid character '" + chr + "' at index " + idx; throw `Invalid character '${chr}' at index ${idx}`;
} }
return digit; return digit;
}); });

View File

@ -6,6 +6,7 @@
import Operation from "../Operation"; import Operation from "../Operation";
import OperationError from "../errors/OperationError"; import OperationError from "../errors/OperationError";
import Utils from "../Utils";
import {alphabetName, ALPHABET_OPTIONS} from "../lib/Base85"; import {alphabetName, ALPHABET_OPTIONS} from "../lib/Base85";
/** /**
@ -21,7 +22,7 @@ class ToBase85 extends Operation {
this.name = "To Base85"; this.name = "To Base85";
this.module = "Default"; this.module = "Default";
this.description = "Base85 (similar to Base64) is a notation for encoding arbitrary byte data. It is usually more efficient that Base64.<br><br>This operation encodes data in an ASCII string (with an alphabet of your choosing, presets included).<br><br>e.g. <code>hello world</code> becomes <code>BOu!rD]j7BEbo7</code><br><br>Base85 is commonly used in Adobe's PostScript and PDF file formats.<br><br><strong>Options</strong><br><u>Alphabet</u><ul><li>Standard - The standard alphabet, referred to as Ascii85</li><li>Z85 (ZeroMQ) - A string-safe variant of Base85, which avoids quote marks and backslash characters</li><li>IPv6 - A variant of Base85 suitable for encoding IPv6 addresses (RFC 1924)</li></ul><u>Include delimiter</u><br>Adds a '<~' and '~>' delimiter to the start and end of the data. This is standard for Adobe's implementation of Base85."; this.description = "Base85 (also called Ascii85) is a notation for encoding arbitrary byte data. It is usually more efficient that Base64.<br><br>This operation encodes data in an ASCII string (with an alphabet of your choosing, presets included).<br><br>e.g. <code>hello world</code> becomes <code>BOu!rD]j7BEbo7</code><br><br>Base85 is commonly used in Adobe's PostScript and PDF file formats.<br><br><strong>Options</strong><br><u>Alphabet</u><ul><li>Standard - The standard alphabet, referred to as Ascii85</li><li>Z85 (ZeroMQ) - A string-safe variant of Base85, which avoids quote marks and backslash characters</li><li>IPv6 - A variant of Base85 suitable for encoding IPv6 addresses (RFC 1924)</li></ul><u>Include delimiter</u><br>Adds a '<~' and '~>' delimiter to the start and end of the data. This is standard for Adobe's implementation of Base85.";
this.infoURL = "https://wikipedia.org/wiki/Ascii85"; this.infoURL = "https://wikipedia.org/wiki/Ascii85";
this.inputType = "byteArray"; this.inputType = "byteArray";
this.outputType = "string"; this.outputType = "string";
@ -32,7 +33,7 @@ class ToBase85 extends Operation {
value: ALPHABET_OPTIONS value: ALPHABET_OPTIONS
}, },
{ {
name: "Include Delimeter", name: "Include delimeter",
type: "boolean", type: "boolean",
value: false value: false
} }
@ -45,13 +46,14 @@ class ToBase85 extends Operation {
* @returns {string} * @returns {string}
*/ */
run(input, args) { run(input, args) {
const alphabet = args[0] || ALPHABET_OPTIONS[0].value, const alphabet = Utils.expandAlphRange(args[0]).join(""),
encoding = alphabetName(alphabet); encoding = alphabetName(alphabet),
includeDelim = args[1];
let result = ""; let result = "";
if (alphabet.length !== 85 || if (alphabet.length !== 85 ||
[].unique.call(alphabet).length !== 85) { [].unique.call(alphabet).length !== 85) {
throw new OperationError("Error: alphabet must be of length 85"); throw new OperationError("Error: Alphabet must be of length 85");
} }
if (input.length === 0) return ""; if (input.length === 0) return "";
@ -84,9 +86,7 @@ class ToBase85 extends Operation {
} }
} }
if (args[1] === true) result = "<~" + result + "~>"; return includeDelim ? `<~${result}~>` : result;
return result;
} }
} }