Skip to content

Commit

Permalink
Merge pull request #318 from Fdawgs/refactor/index
Browse files Browse the repository at this point in the history
  • Loading branch information
Fdawgs committed Sep 17, 2023
2 parents 680aa50 + 1a25f55 commit 3b956cb
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module.exports = {
},
],
parserOptions: {
ecmaVersion: 2019,
ecmaVersion: 2023,
// Explicitly tell ESLint to parse JavaScript as CommonJS, as airbnb-base sets this to "modules" for ECMAScript
sourceType: "script",
},
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"devDependencies": {
"@commitlint/cli": "^17.7.1",
"@commitlint/config-conventional": "^17.7.0",
"@types/jest": "^29.5.5",
"eslint": "^8.47.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-prettier": "^9.0.0",
Expand All @@ -89,7 +90,7 @@
"license-checker": "^25.0.1",
"prettier": "^3.0.2",
"spdx-copyleft": "^1.0.0",
"typescript": "^5.1.6"
"typescript": "^5.2.2"
},
"dependencies": {
"semver": "^7.5.4",
Expand Down
1 change: 1 addition & 0 deletions scripts/license-checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ async function checkLicenses() {
});

const copyLeftLicensesList = Object.keys(licenses).filter((license) =>
// @ts-ignore: includes() returns false if undefined is passed
copyLeftLicenses.includes(licenses[license].licenses)
);

Expand Down
22 changes: 17 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,49 +13,62 @@ const execFileAsync = promisify(execFile);
* @description Checks each option provided is valid, of the correct type, and can be used by specified
* version of binary.
* @ignore
* @param {object} acceptedOptions - Object containing options that a binary accepts.
* @param {object} acceptedOptions - Object containing accepted options.
* @param {object} options - Object containing options to pass to binary.
* @param {string} [version] - Semantic version of binary.
* @returns {string[]} Array of CLI arguments.
* @throws If invalid arguments provided.
*/
function parseOptions(acceptedOptions, options, version) {
/** @type {string[]} */
const args = [];
/** @type {string[]} */
const invalidArgs = [];
Object.keys(options).forEach((key) => {
if (Object.prototype.hasOwnProperty.call(acceptedOptions, key)) {
if (Object.hasOwn(acceptedOptions, key)) {
// @ts-ignore
// eslint-disable-next-line valid-typeof
if (typeof options[key] === acceptedOptions[key].type) {
// Skip boolean options if false
// @ts-ignore
if (acceptedOptions[key].type === "boolean" && !options[key]) {
return;
}
// @ts-ignore
args.push(acceptedOptions[key].arg);
} else {
invalidArgs.push(
`Invalid value type provided for option '${key}', expected ${
// @ts-ignore
acceptedOptions[key].type
// @ts-ignore
} but received ${typeof options[key]}`
);
}

if (
// @ts-ignore
acceptedOptions[key].minVersion &&
version &&
// @ts-ignore
lt(version, acceptedOptions[key].minVersion)
) {
invalidArgs.push(
// @ts-ignore
`Invalid option provided for the current version of the binary used. '${key}' was introduced in v${acceptedOptions[key].minVersion}, but received v${version}`
);
}

/* istanbul ignore next: requires incredibly old version of UnRTF to test */
if (
// @ts-ignore
acceptedOptions[key].maxVersion &&
version &&
// @ts-ignore: type checking is done above
gt(version, acceptedOptions[key].maxVersion)
) {
invalidArgs.push(
// @ts-ignore
`Invalid option provided for the current version of the binary used. '${key}' is only present up to v${acceptedOptions[key].maxVersion}, but received v${version}`
);
}
Expand All @@ -70,9 +83,7 @@ function parseOptions(acceptedOptions, options, version) {
}

class UnRTF {
/**
* @param {string} [binPath] - Path of UnRTF binary.
*/
/** @param {string} [binPath] - Path of UnRTF binary. */
constructor(binPath) {
/* istanbul ignore else: requires specific OS */
if (binPath) {
Expand Down Expand Up @@ -187,6 +198,7 @@ class UnRTF {
* v0.19.3 returns "0.19.3\r\n"
* v0.21.0 returns "0.21.10\nsearch path is: /usr/share/unrtf/\n"
*/
// @ts-ignore: parseOptions checks if falsy
const versionInfo = /^(\d{1,2}\.\d{1,2}\.\d{1,2})/u.exec(stderr)[1];

const args = parseOptions(acceptedOptions, options, versionInfo);
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"declaration": true,
"emitDeclarationOnly": true,
"lib": ["ES2023"],
Expand Down
4 changes: 1 addition & 3 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
export default UnRTF;
export class UnRTF {
/**
* @param {string} [binPath] - Path of UnRTF binary.
*/
/** @param {string} [binPath] - Path of UnRTF binary. */
constructor(binPath?: string | undefined);
unrtfPath: string;
/**
Expand Down

0 comments on commit 3b956cb

Please sign in to comment.