From 7833444ef5150f286544c11a1720fd899e2411aa Mon Sep 17 00:00:00 2001 From: Roman Seidelsohn Date: Fri, 13 Oct 2023 13:50:39 +0200 Subject: [PATCH] refactor: Use easier to understand functions and variable names The old naming led to me breaking the code when I wanted to refactor it. "hasOnlyAllowedPackages" could be easily interpreted as "contains no license not in the allow list", which is not what the function does. --- lib/index.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/index.js b/lib/index.js index ebf2a77..c8e3fa0 100644 --- a/lib/index.js +++ b/lib/index.js @@ -536,17 +536,24 @@ exports.init = function init(args, callback) { } }; + /** + * Check if the current license contains (eventually among others) at least one of the allowed licenses + * + * @param {string} currentLicense The current license + * @param {} packageName The package name + */ const checkForOnlyAllow = (currentLicense, packageName) => { if (toCheckforOnlyAllow.length > 0) { - let hasOnlyAllowedPackages = false; + let containsOneOfAllowedPackages = false; - toCheckforOnlyAllow.forEach((allowedLicense) => { - if (currentLicense.indexOf(allowedLicense) >= 0) { - hasOnlyAllowedPackages = true; + for (const allowedLicense of toCheckforOnlyAllow) { + if (currentLicense.includes(allowedLicense)) { + containsOneOfAllowedPackages = true; + break; } - }); + } - if (!hasOnlyAllowedPackages) { + if (!containsOneOfAllowedPackages) { console.error( `Package "${packageName}" is licensed under "${currentLicense}" which is not permitted by the --onlyAllow flag. Exiting.`, );