Skip to content

Commit

Permalink
perf(index): optimise parseOptions (#624)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fdawgs authored Feb 6, 2025
1 parent 98a10a3 commit 604ad25
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
13 changes: 13 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ module.exports = {
"jsdoc/require-description-complete-sentence": "error",
"jsdoc/require-hyphen-before-param-description": "error",
"no-multiple-empty-lines": ["error", { max: 1 }],
"no-restricted-syntax": [
"error",
{
selector: "LabeledStatement",
message:
"Labels are a form of GOTO; using them makes code confusing and hard to maintain and understand.",
},
{
selector: "WithStatement",
message:
"`with` is disallowed in strict mode because it makes code impossible to predict and optimize.",
},
],
"prefer-destructuring": ["error", { object: true, array: false }],
"promise/prefer-await-to-callbacks": "warn",
"promise/prefer-await-to-then": "warn",
Expand Down
39 changes: 20 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const pdfInfoFileSizesRegex = /(File\s+size:\s+)0(\s+)bytes/u;
* version of binary.
* @ignore
* @param {object} acceptedOptions - Object containing accepted options.
* @param {object} options - Object containing options to pass to binary.
* @param {Record<string, any>} options - Object containing options to pass to binary.
* @param {string} [version] - Version of binary.
* @returns {string[]} Array of CLI arguments.
* @throws If invalid arguments provided.
Expand All @@ -38,45 +38,46 @@ function parseOptions(acceptedOptions, options, version) {
const args = [];
/** @type {string[]} */
const invalidArgs = [];
Object.keys(options).forEach((key) => {
for (const key of Object.keys(options)) {
if (Object.hasOwn(acceptedOptions, key)) {
const option = options[key];
const acceptedOption = acceptedOptions[key];

// eslint-disable-next-line valid-typeof -- `type` is a string
if (acceptedOptions[key].type === typeof options[key]) {
if (acceptedOption.type === typeof option) {
// Skip boolean options if false
if (acceptedOptions[key].type === "boolean" && !options[key]) {
return;
}

// Arg will be empty for some non-standard options
if (acceptedOptions[key].arg !== "") {
args.push(acceptedOptions[key].arg);
}
if (acceptedOption.type !== "boolean" || option) {
// Arg will be empty for some non-standard options
if (acceptedOption.arg !== "") {
args.push(acceptedOption.arg);
}

if (typeof options[key] !== "boolean") {
args.push(options[key]);
if (typeof option !== "boolean") {
args.push(option);
}
}
} else {
invalidArgs.push(
`Invalid value type provided for option '${key}', expected ${
acceptedOptions[key].type
} but received ${typeof options[key]}`
acceptedOption.type
} but received ${typeof option}`
);
}

if (
acceptedOptions[key].minVersion &&
acceptedOption.minVersion &&
version &&
// @ts-ignore: type checking is done above
lt(version, acceptedOptions[key].minVersion, { loose: true })
lt(version, acceptedOption.minVersion, { loose: true })
) {
invalidArgs.push(
`Invalid option provided for the current version of the binary used. '${key}' was introduced in v${acceptedOptions[key].minVersion}, but received v${version}`
`Invalid option provided for the current version of the binary used. '${key}' was introduced in v${acceptedOption.minVersion}, but received v${version}`
);
}
} else {
invalidArgs.push(`Invalid option provided '${key}'`);
}
});
}
if (invalidArgs.length === 0) {
return args;
}
Expand Down

0 comments on commit 604ad25

Please sign in to comment.