Skip to content

Commit

Permalink
Fixed prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
hiimjasmine00 committed May 2, 2023
1 parent 27fb287 commit d75d4f0
Show file tree
Hide file tree
Showing 7 changed files with 285 additions and 100 deletions.
9 changes: 6 additions & 3 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
**/**
!**/*.js
!**/*.ts
*
!*.js
!*.ts
.git
.vscode
dist
!bin
!src
!test
node_modules
109 changes: 82 additions & 27 deletions bin/decodeme.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,119 @@ const { program } = require("commander");
const dencodeme = require("../dist");

const encodingOption = program
.createOption("-e, --encoding <format>", "The encoding to write the output data in")
.createOption(
"-e, --encoding <format>",
"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("<input>", "The input data to decode");
const outOption = program
.createOption("-o, --out <path>", "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(
"<input>",
"The input data to decode"
);
const outOption = program.createOption(
"-o, --out <path>",
"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("<radix> [options] <input>")
.argument("<radix>", "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(
"<radix>",
"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)
.addOption(outOption)
.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] <input>")
.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));
}
Expand Down
109 changes: 82 additions & 27 deletions bin/encodeme.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,119 @@ const { program } = require("commander");
const dencodeme = require("../dist");

const encodingOption = program
.createOption("-e, --encoding <format>", "The encoding to read the input data in")
.createOption(
"-e, --encoding <format>",
"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("<input>", "The input data to encode");
const outOption = program
.createOption("-o, --out <path>", "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(
"<input>",
"The input data to encode"
);
const outOption = program.createOption(
"-o, --out <path>",
"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("<radix> [options] <input>")
.argument("<radix>", "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(
"<radix>",
"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)
.addOption(outOption)
.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] <input>")
.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));
}
Expand Down
19 changes: 16 additions & 3 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
};
}
Loading

0 comments on commit d75d4f0

Please sign in to comment.