Skip to content

Commit

Permalink
style: Convert function declarations to arrow functions
Browse files Browse the repository at this point in the history
There is no real reason behind this step - I might even undo this in the
future. Or I might figure out a reason why this is better. Who knows.
  • Loading branch information
RSeidelsohn committed Apr 9, 2023
1 parent 419437d commit 17dd533
Showing 1 changed file with 46 additions and 59 deletions.
105 changes: 46 additions & 59 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const debugLog = debug('license-checker-rseidelsohn:log');

debugLog.log = console.log.bind(console);

function first_or_second(obj1, obj2) {
const first_or_second = (obj1, obj2) => {
/*istanbul ignore next*/
if (obj1 !== undefined) {
return obj1;
Expand All @@ -37,11 +37,11 @@ function first_or_second(obj1, obj2) {
} else {
return undefined;
}
}
};

// This function calls itself recursively. On the first iteration, it collects the data of the main program, during the
// second iteration, it collects the data from all direct dependencies, then it collects their dependencies and so on.
const flatten = function flatten(options) {
const flatten = (options) => {
const moduleInfo = { licenses: UNKNOWN };
const { color: colorize, deps: json, unknown } = options;
const currentPackageNameAndVersion = `${json.name}@${json.version}`;
Expand Down Expand Up @@ -73,9 +73,7 @@ const flatten = function flatten(options) {
data[currentPackageNameAndVersion] = moduleInfo;

// Include property in output unless custom format has set property to false.
function mustInclude(property) {
return options?.customFormat?.[property] !== false;
}
const mustInclude = (property) => options?.customFormat?.[property] !== false;

if (mustInclude('repository')) {
if (clarification?.repository) {
Expand Down Expand Up @@ -184,7 +182,7 @@ const flatten = function flatten(options) {
});
}

licenseFilesInCurrentModuleDirectory.forEach(function (filename, index) {
licenseFilesInCurrentModuleDirectory.forEach(function findBetterLicenseData(filename, index) {
licenseFile = path.join(module_path, filename);
// Checking that the file is in fact a normal file and not a directory for example.
/*istanbul ignore else*/
Expand Down Expand Up @@ -395,15 +393,15 @@ const removeUnwantedDependencies = (json, args) => {
}
};

function prepareArgs(args) {
const prepareArgs = (args) => {
const normalizedArgs = { ...args };
if (args.direct === true) {
normalizedArgs.direct = 0;
} else if (typeof args.direct !== 'number') {
normalizedArgs.direct = Infinity;
}
return normalizedArgs;
}
};

exports.init = function init(initArgs, callback) {
const args = prepareArgs(initArgs);
Expand Down Expand Up @@ -490,19 +488,18 @@ exports.init = function init(initArgs, callback) {
.map((license) => license.replace(/\\,/g, ',').replace(/^\s+|\s+$/g, ''));
let inputError = null;

function colorizeString(string) {
const colorizeString = (string) =>
/*istanbul ignore next*/
return colorize ? chalk.bold.red(string) : string;
}
colorize ? chalk.bold.red(string) : string;

function filterDeletePrivatePackages(privatePackage) {
const filterDeletePrivatePackages = (privatePackage) => {
/*istanbul ignore next - I don't have access to private packages to test */
if (resultJson[privatePackage] && resultJson[privatePackage].private) {
delete resultJson[privatePackage];
}
}
};

function onlyIncludeWhitelist(whitelist, filtered) {
const onlyIncludeWhitelist = (whitelist, filtered) => {
const resultJson = {};

Object.keys(filtered).map((filteredPackage) => {
Expand All @@ -522,9 +519,9 @@ exports.init = function init(initArgs, callback) {
});

return resultJson;
}
};

function excludeBlacklist(blacklist, filtered) {
const excludeBlacklist = (blacklist, filtered) => {
const resultJson = {};

Object.keys(filtered).map((filteredPackage) => {
Expand All @@ -544,9 +541,9 @@ exports.init = function init(initArgs, callback) {
});

return resultJson;
}
};

function excludePackagesStartingWith(blacklist, currentResult) {
const excludePackagesStartingWith = (blacklist, currentResult) => {
const resultJson = { ...currentResult };

for (const pkgName in resultJson) {
Expand All @@ -556,28 +553,28 @@ exports.init = function init(initArgs, callback) {
}

return resultJson;
}
};

function exitIfCheckHits(packageName) {
const exitIfCheckHits = (packageName) => {
const currentLicense = resultJson[packageName]?.licenses;

if (currentLicense) {
checkForFailOn(currentLicense);
checkForOnlyAllow(currentLicense, packageName);
}
}
};

function checkForFailOn(currentLicense) {
const checkForFailOn = (currentLicense) => {
if (toCheckforFailOn.length > 0) {
if (toCheckforFailOn.indexOf(currentLicense) > -1) {
console.error(`Found license defined by the --failOn flag: "${currentLicense}". Exiting.`);

process.exit(1);
}
}
}
};

function checkForOnlyAllow(currentLicense, packageName) {
const checkForOnlyAllow = (currentLicense, packageName) => {
if (toCheckforOnlyAllow.length > 0) {
let hasOnlyAllowedPackages = false;

Expand All @@ -595,21 +592,16 @@ exports.init = function init(initArgs, callback) {
process.exit(1);
}
}
}
};

function transformBSD(spdx) {
return spdx === 'BSD' ? '(0BSD OR BSD-2-Clause OR BSD-3-Clause OR BSD-4-Clause)' : spdx;
}
const transformBSD = (spdx) =>
spdx === 'BSD' ? '(0BSD OR BSD-2-Clause OR BSD-3-Clause OR BSD-4-Clause)' : spdx;

function invertResultOf(fn) {
return (spdx) => !fn(spdx);
}
const invertResultOf = (fn) => (spdx) => !fn(spdx);

function spdxIsValid(spdx) {
return spdxCorrect(spdx) === spdx;
}
const spdxIsValid = (spdx) => spdxCorrect(spdx) === spdx;

function getLicenseMatch(licensesArr, filtered, packageName, packageData, compareLicenses) {
const getLicenseMatch = (licensesArr, filtered, packageName, packageData, compareLicenses) => {
const validSPDXLicenses = compareLicenses.map(transformBSD).filter(spdxIsValid);
const invalidSPDXLicenses = compareLicenses.map(transformBSD).filter(invertResultOf(spdxIsValid));
const spdxExcluder = `( ${validSPDXLicenses.join(' OR ')} )`;
Expand Down Expand Up @@ -640,7 +632,7 @@ exports.init = function init(initArgs, callback) {
});

return match;
}
};

// This following block stores the licenses in the sorted object (before, the sorted object is the empty object):
Object.keys(data)
Expand Down Expand Up @@ -780,7 +772,7 @@ exports.init = function init(initArgs, callback) {
});
};

exports.filterAttributes = function filterAttributes(attributes, json) {
exports.filterAttributes = (attributes, json) => {
let filteredJson = json;

if (attributes) {
Expand All @@ -793,15 +785,13 @@ exports.filterAttributes = function filterAttributes(attributes, json) {
return filteredJson;
};

exports.print = function print(sorted) {
exports.print = (sorted) => {
console.log(exports.asTree(sorted));
};

exports.asTree = function asTree(sorted) {
return treeify.asTree(sorted, true);
};
exports.asTree = (sorted) => treeify.asTree(sorted, true);

exports.asSummary = function asSummary(sorted) {
exports.asSummary = (sorted) => {
const licenseCountMap = new global.Map();
const licenseCountArray = [];
const sortedLicenseCountObj = {};
Expand All @@ -827,7 +817,7 @@ exports.asSummary = function asSummary(sorted) {
return treeify.asTree(sortedLicenseCountObj, true);
};

exports.asCSV = function asCSV(sorted, customFormat, csvComponentPrefix) {
exports.asCSV = (sorted, customFormat, csvComponentPrefix) => {
const csvHeaders = getCsvHeaders(customFormat, csvComponentPrefix);
const csvDataArr = getCsvData(sorted, customFormat, csvComponentPrefix);

Expand All @@ -841,7 +831,7 @@ exports.asCSV = function asCSV(sorted, customFormat, csvComponentPrefix) {
* @param {JSON} customFormat The custom format with information about the needed keys.
* @return {String} The returning plain text.
*/
exports.asMarkDown = function asMarkDown(sorted, customFormat) {
exports.asMarkDown = (sorted, customFormat) => {
let text = [];

if (customFormat && Object.keys(customFormat).length > 0) {
Expand All @@ -865,8 +855,8 @@ exports.asMarkDown = function asMarkDown(sorted, customFormat) {
/**
* Output data in plain vertical format like Angular CLI does: https://angular.io/3rdpartylicenses.txt
*/
exports.asPlainVertical = function asPlainVertical(sorted) {
return Object.entries(sorted)
exports.asPlainVertical = (sorted) =>
Object.entries(sorted)
.map(([moduleName, moduleData]) => {
let licenseText =
moduleName.substring(0, moduleName.lastIndexOf('@')) +
Expand Down Expand Up @@ -912,8 +902,7 @@ exports.asPlainVertical = function asPlainVertical(sorted) {
});
} else if (
typeof moduleData.licenseFile === 'object' &&
(moduleData.licenseFile.type || moduleData.licenseFile.name)
) {
(moduleData.licenseFile.type || moduleData.licenseFile.name)) {
licenseText += moduleData.licenseFile.type || moduleData.licenseFile.name;
} else if (typeof moduleData.licenseFile === 'string') {
licenseText += fs.readFileSync(moduleData.licenseFile, { encoding: 'utf8' });
Expand All @@ -922,9 +911,8 @@ exports.asPlainVertical = function asPlainVertical(sorted) {
return licenseText;
})
.join('\n\n');
};

exports.parseJson = function parseJson(jsonPath) {
exports.parseJson = (jsonPath) => {
if (typeof jsonPath !== 'string') {
return new Error('The path was not specified for the JSON file to parse.');
}
Expand All @@ -938,7 +926,7 @@ exports.parseJson = function parseJson(jsonPath) {
}
};

exports.asFiles = function asFiles(json, outDir) {
exports.asFiles = (json, outDir) => {
mkdirp.sync(outDir);

Object.keys(json).forEach((moduleName) => {
Expand All @@ -957,7 +945,7 @@ exports.asFiles = function asFiles(json, outDir) {
});
};

function getCsvHeaders(customFormat, csvComponentPrefix) {
const getCsvHeaders = (customFormat, csvComponentPrefix) => {
const prefixName = '"component"';
const entriesArr = [];

Expand All @@ -978,9 +966,9 @@ function getCsvHeaders(customFormat, csvComponentPrefix) {
}

return entriesArr.join(',');
}
};

function getCsvData(sorted, customFormat, csvComponentPrefix) {
const getCsvData = (sorted, customFormat, csvComponentPrefix) => {
const csvDataArr = [];

Object.entries(sorted).forEach(([key, module]) => {
Expand All @@ -1005,8 +993,7 @@ function getCsvData(sorted, customFormat, csvComponentPrefix) {
});

return csvDataArr;
}
};

function getOptionArray(option) {
return (Array.isArray(option) && option) || (typeof option === 'string' && option.split(';')) || false;
}
const getOptionArray = (option) =>
(Array.isArray(option) && option) || (typeof option === 'string' && option.split(';')) || false;

0 comments on commit 17dd533

Please sign in to comment.