From d75d4f0f2667c79f0557227270770fb712380d21 Mon Sep 17 00:00:00 2001 From: Justin Pridgen Date: Tue, 2 May 2023 18:56:40 -0400 Subject: [PATCH] Fixed prettier --- .prettierignore | 9 ++-- bin/decodeme.js | 109 ++++++++++++++++++++++++++++++++++++------------ bin/encodeme.js | 109 ++++++++++++++++++++++++++++++++++++------------ src/base.ts | 19 +++++++-- src/base32.ts | 50 +++++++++++++++------- src/base64.ts | 59 ++++++++++++++++++-------- src/util.ts | 30 +++++++++---- 7 files changed, 285 insertions(+), 100 deletions(-) diff --git a/.prettierignore b/.prettierignore index 4d1a0d2..47c82cf 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,7 +1,10 @@ -**/** -!**/*.js -!**/*.ts +* +!*.js +!*.ts .git .vscode dist +!bin +!src +!test node_modules diff --git a/bin/decodeme.js b/bin/decodeme.js index 9d55599..8b7bce9 100644 --- a/bin/decodeme.js +++ b/bin/decodeme.js @@ -6,30 +6,53 @@ const { program } = require("commander"); const dencodeme = require("../dist"); const encodingOption = program - .createOption("-e, --encoding ", "The encoding to write the output data in") + .createOption( + "-e, --encoding ", + "The encoding to write the output data in" + ) .default("utf8") .choices(["ascii", "binary", "latin1", "ucs2", "utf8", "utf16le"]); -const fileFlag = program - .createOption("-f, --file", "Interprets the input as a file path and decodes the file at the path"); -const inputArgument = program - .createArgument("", "The input data to decode"); -const outOption = program - .createOption("-o, --out ", "The file path to write the output data to"); +const fileFlag = program.createOption( + "-f, --file", + "Interprets the input as a file path and decodes the file at the path" +); +const inputArgument = program.createArgument( + "", + "The input data to decode" +); +const outOption = program.createOption( + "-o, --out ", + "The file path to write the output data to" +); -program.name("decodeme") +program + .name("decodeme") .usage("[command] [options]") .description("Decode data using various encoding schemes.") - .version(require("../package.json").version, "-v, --version", "Outputs the current version") + .version( + require("../package.json").version, + "-v, --version", + "Outputs the current version" + ) .helpOption("-h, --help", "Outputs this help menu") .addHelpCommand("help [command]", "Outputs help for command"); -program.command("base") - .description("Decodes the specified input data from the specified base/radix") +program + .command("base") + .description( + "Decodes the specified input data from the specified base/radix" + ) .usage(" [options] ") - .argument("", "The base/radix to decode from, clamped to range 2-36", x => { - const parsed = parseInt(x, 10); - return Number.isNaN(parsed) ? program.error("Base/Radix is not a valid base 10 number") : parsed; - }) + .argument( + "", + "The base/radix to decode from, clamped to range 2-36", + x => { + const parsed = parseInt(x, 10); + return Number.isNaN(parsed) + ? program.error("Base/Radix is not a valid base 10 number") + : parsed; + } + ) .addArgument(inputArgument) .addOption(encodingOption) .addOption(fileFlag) @@ -37,33 +60,65 @@ program.command("base") .alias("radix") .action((radix, input, options) => { try { - const output = dencodeme.base(radix).decode(options.file ? - fs.readFileSync(path.resolve(process.cwd(), input), "utf8") : input); - options.out ? fs.writeFileSync(path.resolve(process.cwd(), options.out), output, options.encoding) : - process.stdout.write(output.toString(options.encoding)); + const output = dencodeme + .base(radix) + .decode( + options.file + ? fs.readFileSync( + path.resolve(process.cwd(), input), + "utf8" + ) + : input + ); + options.out + ? fs.writeFileSync( + path.resolve(process.cwd(), options.out), + output, + options.encoding + ) + : process.stdout.write(output.toString(options.encoding)); } catch (err) { program.error(String(err)); } }); // Create a command for each base/radix in the dencodeme object -for (const [command, base] of Object.entries(dencodeme).map(x => [x[0], typeof x[1] == "function" ? NaN : x[1].radix]) +for (const [command, base] of Object.entries(dencodeme) + .map(x => [x[0], typeof x[1] == "function" ? NaN : x[1].radix]) .filter(x => !Number.isNaN(x[1]))) { - program.command(command) + program + .command(command) .summary(`Decodes the specified input data from base ${base}`) - .description(`Decodes the specified input data from a base/radix of ${base}`) + .description( + `Decodes the specified input data from a base/radix of ${base}` + ) .usage("[options] ") .addArgument(inputArgument) .addOption(encodingOption) .addOption(fileFlag) .addOption(outOption) - .aliases(command.startsWith("base") ? [`b${command.slice(4)}`] : [command.slice(0, 3), `base${base}`, `b${base}`]) + .aliases( + command.startsWith("base") + ? [`b${command.slice(4)}`] + : [command.slice(0, 3), `base${base}`, `b${base}`] + ) .action((input, options) => { try { - const output = dencodeme[command].decode(options.file ? - fs.readFileSync(path.resolve(process.cwd(), input), "utf8") : input); - options.out ? fs.writeFileSync(path.resolve(process.cwd(), options.out), output, options.encoding) : - process.stdout.write(output.toString(options.encoding)); + const output = dencodeme[command].decode( + options.file + ? fs.readFileSync( + path.resolve(process.cwd(), input), + "utf8" + ) + : input + ); + options.out + ? fs.writeFileSync( + path.resolve(process.cwd(), options.out), + output, + options.encoding + ) + : process.stdout.write(output.toString(options.encoding)); } catch (err) { program.error(String(err)); } diff --git a/bin/encodeme.js b/bin/encodeme.js index 27e793f..b61548f 100644 --- a/bin/encodeme.js +++ b/bin/encodeme.js @@ -6,30 +6,53 @@ const { program } = require("commander"); const dencodeme = require("../dist"); const encodingOption = program - .createOption("-e, --encoding ", "The encoding to read the input data in") + .createOption( + "-e, --encoding ", + "The encoding to read the input data in" + ) .default("utf8") .choices(["ascii", "binary", "latin1", "ucs2", "utf8", "utf16le"]); -const fileFlag = program - .createOption("-f, --file", "Interprets the input as a file path and encodes the file at the path"); -const inputArgument = program - .createArgument("", "The input data to encode"); -const outOption = program - .createOption("-o, --out ", "The file path to write the output data to"); +const fileFlag = program.createOption( + "-f, --file", + "Interprets the input as a file path and encodes the file at the path" +); +const inputArgument = program.createArgument( + "", + "The input data to encode" +); +const outOption = program.createOption( + "-o, --out ", + "The file path to write the output data to" +); -program.name("encodeme") +program + .name("encodeme") .usage("[command] [options]") .description("Encode data using various encoding schemes.") - .version(require("../package.json").version, "-v, --version", "Outputs the current version") + .version( + require("../package.json").version, + "-v, --version", + "Outputs the current version" + ) .helpOption("-h, --help", "Outputs this help menu") .addHelpCommand("help [command]", "Outputs help for command"); -program.command("base") - .description("Encodes the specified input data with the specified base/radix") +program + .command("base") + .description( + "Encodes the specified input data with the specified base/radix" + ) .usage(" [options] ") - .argument("", "The base/radix to encode with, clamped to range 2-36", x => { - const parsed = parseInt(x, 10); - return Number.isNaN(parsed) ? program.error("Base/Radix is not a valid base 10 number") : parsed; - }) + .argument( + "", + "The base/radix to encode with, clamped to range 2-36", + x => { + const parsed = parseInt(x, 10); + return Number.isNaN(parsed) + ? program.error("Base/Radix is not a valid base 10 number") + : parsed; + } + ) .addArgument(inputArgument) .addOption(encodingOption) .addOption(fileFlag) @@ -37,33 +60,65 @@ program.command("base") .alias("radix") .action((radix, input, options) => { try { - const output = dencodeme.base(radix).encode(options.file ? - fs.readFileSync(path.resolve(process.cwd(), input), options.encoding) : input); - options.out ? fs.writeFileSync(path.resolve(process.cwd(), options.out), output, options.encoding) : - process.stdout.write(output.toString(options.encoding)); + const output = dencodeme + .base(radix) + .encode( + options.file + ? fs.readFileSync( + path.resolve(process.cwd(), input), + options.encoding + ) + : input + ); + options.out + ? fs.writeFileSync( + path.resolve(process.cwd(), options.out), + output, + options.encoding + ) + : process.stdout.write(output.toString(options.encoding)); } catch (err) { program.error(String(err)); } }); // Create a command for each base/radix in the dencodeme object -for (const [command, base] of Object.entries(dencodeme).map(x => [x[0], typeof x[1] == "function" ? NaN : x[1].radix]) +for (const [command, base] of Object.entries(dencodeme) + .map(x => [x[0], typeof x[1] == "function" ? NaN : x[1].radix]) .filter(x => !Number.isNaN(x[1]))) { - program.command(command) + program + .command(command) .summary(`Encodes the specified input data with base ${base}`) - .description(`Encodes the specified input data with a base/radix of ${base}`) + .description( + `Encodes the specified input data with a base/radix of ${base}` + ) .usage("[options] ") .addArgument(inputArgument) .addOption(encodingOption) .addOption(fileFlag) .addOption(outOption) - .aliases(command.startsWith("base") ? [`b${command.slice(4)}`] : [command.slice(0, 3), `base${base}`, `b${base}`]) + .aliases( + command.startsWith("base") + ? [`b${command.slice(4)}`] + : [command.slice(0, 3), `base${base}`, `b${base}`] + ) .action((input, options) => { try { - const output = dencodeme[command].encode(options.file ? - fs.readFileSync(path.resolve(process.cwd(), input), options.encoding) : input); - options.out ? fs.writeFileSync(path.resolve(process.cwd(), options.out), output, options.encoding) : - process.stdout.write(output.toString(options.encoding)); + const output = dencodeme[command].encode( + options.file + ? fs.readFileSync( + path.resolve(process.cwd(), input), + options.encoding + ) + : input + ); + options.out + ? fs.writeFileSync( + path.resolve(process.cwd(), options.out), + output, + options.encoding + ) + : process.stdout.write(output.toString(options.encoding)); } catch (err) { program.error(String(err)); } diff --git a/src/base.ts b/src/base.ts index c69594b..757fff3 100644 --- a/src/base.ts +++ b/src/base.ts @@ -13,13 +13,26 @@ export function base(radix: number): NumberSystem { regex: new RegExp(`[${charset}]`, "gi"), encode(data, encoding = "utf8") { // For each byte of data, encode it into a character. - return Buffer.from(data.toString(encoding), encoding).toJSON().data.map(x => x.toString(rdx).padStart(max, "0")).join(""); + return Buffer.from(data.toString(encoding), encoding) + .toJSON() + .data.map(x => x.toString(rdx).padStart(max, "0")) + .join(""); }, decode(data, encoding) { // For each character of data, decode it into a byte. - const decoded = Buffer.from(util.padStart((data.toString().toLowerCase().match(this.regex) ?? []).join(""), max).map(x => parseInt(x, rdx))); + const decoded = Buffer.from( + util + .padStart( + ( + data.toString().toLowerCase().match(this.regex) ?? + [] + ).join(""), + max + ) + .map(x => parseInt(x, rdx)) + ); // Return the decoded buffer, or a string if a character encoding is provided. return encoding ? decoded.toString(encoding) : decoded; } - } + }; } diff --git a/src/base32.ts b/src/base32.ts index 6c8be65..a587bde 100644 --- a/src/base32.ts +++ b/src/base32.ts @@ -6,33 +6,51 @@ export const regex = /[A-Z2-7]/gi; // The base 32 regex. const bits = util.quickMap(5, x => 2 ** (x * 8), true); // 4294967296, 16777216, 65536, 256, 1 const chars = util.quickMap(8, x => 2 ** (x * 5), true); // 34359738368, 1073741824, 33554432, 1048576, 32768, 1024, 32, 1 -const pads = util.quickMap(5, x => (8 - Math.ceil(x * 8 / 5)) % 8, false); // 0, 6, 4, 3, 1 +const pads = util.quickMap(5, x => (8 - Math.ceil((x * 8) / 5)) % 8, false); // 0, 6, 4, 3, 1 // Encodes the given data into a Base 32 encoded string. -export function encode(data: string | Buffer, encoding: BufferEncoding = "utf8") { +export function encode( + data: string | Buffer, + encoding: BufferEncoding = "utf8" +) { // The number of padding characters to add to the end of the encoded string. let padding = 0; // For each 5 bytes of data, encode them into 8 characters. (With padding if necessary.) - return Buffer.from(data.toString(encoding), encoding).reduce((a, x, i, r) => { - if (i % 5 == 0) a.push(0); - a[a.length - 1] += x * bits[i % 5]; + return ( + Buffer.from(data.toString(encoding), encoding) + .reduce((a, x, i, r) => { + if (i % 5 == 0) a.push(0); + a[a.length - 1] += x * bits[i % 5]; - if ((i + 1) >= r.length) padding = pads[r.length % 5]; - return a; - }, [] as number[]).map((x, i, r) => - chars.slice(0, 8 - Number((i + 1) >= r.length) * padding).map(y => - charset[Math.floor(x / y) % 32]).join("")).join("") + "=".repeat(padding); + if (i + 1 >= r.length) padding = pads[r.length % 5]; + return a; + }, [] as number[]) + .map((x, i, r) => + chars + .slice(0, 8 - Number(i + 1 >= r.length) * padding) + .map(y => charset[Math.floor(x / y) % 32]) + .join("") + ) + .join("") + "=".repeat(padding) + ); } // Decodes the given Base 32 encoded string into a buffer, or a string if a character encoding is provided. export function decode(data: string, encoding?: BufferEncoding) { // For each 8 characters of data, decode them into 5 bytes. - const decoded = Buffer.from(util.padEnd((data.toString().toUpperCase().match(regex) ?? []).join(""), 8) - .flatMap(x => { - const value = util.stringToNumber(x, charset); - return bits.slice(0, pads.indexOf((x.match(/=/g) ?? []).length) || 5) - .map(y => Math.floor(value / y) % 256); - })); + const decoded = Buffer.from( + util + .padEnd( + (data.toString().toUpperCase().match(regex) ?? []).join(""), + 8 + ) + .flatMap(x => { + const value = util.stringToNumber(x, charset); + return bits + .slice(0, pads.indexOf((x.match(/=/g) ?? []).length) || 5) + .map(y => Math.floor(value / y) % 256); + }) + ); // Return the decoded buffer, or a string if a character encoding is provided. return encoding ? decoded.toString(encoding) : decoded; } diff --git a/src/base64.ts b/src/base64.ts index 40d7fbe..aaa4c30 100644 --- a/src/base64.ts +++ b/src/base64.ts @@ -1,38 +1,63 @@ import * as util from "./util"; -export const charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; // The base 64 character set. +export const charset = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; // The base 64 character set. export const radix = 64; // The base 64 radix. export const regex = /[A-Za-z0-9+/]/gi; // The base 64 regex. const bits = util.quickMap(3, x => 2 ** (x * 8), true); // 65536, 256, 1 const chars = util.quickMap(4, x => 2 ** (x * 6), true); // 262144, 4096, 64, 1 -const pads = util.quickMap(3, x => (4 - Math.ceil(x * 4 / 3)) % 4, false); // 0, 2, 1 +const pads = util.quickMap(3, x => (4 - Math.ceil((x * 4) / 3)) % 4, false); // 0, 2, 1 // Encodes the given data into a Base 64 encoded string. -export function encode(data: string | Buffer, encoding: BufferEncoding = "utf8") { +export function encode( + data: string | Buffer, + encoding: BufferEncoding = "utf8" +) { // The number of padding characters to add to the end of the encoded string. let padding = 0; // For each 3 bytes of data, encode them into 4 characters. (With padding if necessary.) - return Buffer.from(data.toString(encoding), encoding).reduce((a, x, i, r) => { - if (i % 3 == 0) a.push(0); - a[a.length - 1] += x * bits[i % 3]; + return ( + Buffer.from(data.toString(encoding), encoding) + .reduce((a, x, i, r) => { + if (i % 3 == 0) a.push(0); + a[a.length - 1] += x * bits[i % 3]; - if ((i + 1) >= r.length) padding = pads[r.length % 3]; - return a; - }, [] as number[]).map((x, i, r) => - chars.slice(0, 4 - Number((i + 1) >= r.length) * padding).map(y => - charset[Math.floor(x / y) % 64]).join("")).join("") + "=".repeat(padding); + if (i + 1 >= r.length) padding = pads[r.length % 3]; + return a; + }, [] as number[]) + .map((x, i, r) => + chars + .slice(0, 4 - Number(i + 1 >= r.length) * padding) + .map(y => charset[Math.floor(x / y) % 64]) + .join("") + ) + .join("") + "=".repeat(padding) + ); } // Decodes the given Base 64 encoded string into a buffer, or a string if a character encoding is provided. export function decode(data: string, encoding?: BufferEncoding) { // For each 4 characters of data, decode them into 3 bytes. - const decoded = Buffer.from(util.padEnd((data.toString().replaceAll("-", "+").replaceAll("_", "/").match(regex) ?? []).join(""), 4) - .flatMap(x => { - const value = util.stringToNumber(x, charset); - return bits.slice(0, pads.indexOf((x.match(/=/g) ?? []).length) || 3) - .map(y => Math.floor(value / y) % 256); - })); + const decoded = Buffer.from( + util + .padEnd( + ( + data + .toString() + .replaceAll("-", "+") + .replaceAll("_", "/") + .match(regex) ?? [] + ).join(""), + 4 + ) + .flatMap(x => { + const value = util.stringToNumber(x, charset); + return bits + .slice(0, pads.indexOf((x.match(/=/g) ?? []).length) || 3) + .map(y => Math.floor(value / y) % 256); + }) + ); // Return the decoded buffer, or a string if a character encoding is provided. return encoding ? decoded.toString(encoding) : decoded; } diff --git a/src/util.ts b/src/util.ts index a25b072..d598082 100644 --- a/src/util.ts +++ b/src/util.ts @@ -2,26 +2,42 @@ function getMapper(length: number) { return (a: string[], x: string, i: number) => { if (i % length == 0) a.push(""); - return a[a.length - 1] += x, a; - } + return (a[a.length - 1] += x), a; + }; } // Pads the given string with zeros to the left, and splits it into an array of strings of the given length. export function padStart(data: string, length: number): string[] { - return [ ...Array((length - data.length % length) % length).fill("0"), ...data ].reduce(getMapper(length), []); + return [ + ...Array((length - (data.length % length)) % length).fill("0"), + ...data + ].reduce(getMapper(length), []); } // Pads the given string with equals signs to the right, and splits it into an array of strings of the given length. export function padEnd(data: string, length: number): string[] { - return [ ...data, ...Array((length - data.length % length) % length).fill("=") ].reduce(getMapper(length), []); + return [ + ...data, + ...Array((length - (data.length % length)) % length).fill("=") + ].reduce(getMapper(length), []); } // Creates an array of numbers from 0 to length - 1, and multiplies each number by the given multiplier. -export function quickMap(length: number, multiplier: (x: number) => number, reverse: boolean = false) { - return Array(length).fill(0).map((_, x) => multiplier(reverse ? -x + length - 1 : x)); +export function quickMap( + length: number, + multiplier: (x: number) => number, + reverse: boolean = false +) { + return Array(length) + .fill(0) + .map((_, x) => multiplier(reverse ? -x + length - 1 : x)); } // Converts the given string to a number using the given character set. export function stringToNumber(str: string, charset: string) { - return str.split("").reverse().map(x => charset.indexOf(x)).reduce((a, x, i) => a + (x >= 0 ? x * charset.length ** i : 0), 0); + return str + .split("") + .reverse() + .map(x => charset.indexOf(x)) + .reduce((a, x, i) => a + (x >= 0 ? x * charset.length ** i : 0), 0); }