Skip to content

Commit

Permalink
refactor: replace indexOf with includes and || with ?? where appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
BePo65 committed Oct 13, 2024
1 parent 03ad12d commit 7b76f60
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/addPackageDataFromRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export async function addPackageDataFromRepository(packageEntry) {
const installedVersionData = json.versions[installedVersion];
if (installedVersionData !== undefined) {
installedFrom =
extractInstalledFrom(installedVersionData) || notAvailableText;
extractInstalledFrom(installedVersionData) ?? notAvailableText;
} else {
installedFrom = notAvailableText;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/addPackagesToIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ export function addPackagesToIndex(
exclusions,
exclusionRegex,
) {
exclusions = exclusions || [];
exclusions = exclusions ?? [];

// iterate over packages and prepare urls before I call the registry
for (let key in packages) {
if (exclusions.indexOf(key) !== -1 || exclusionRegex?.test(key)) {
if (exclusions.includes(key) || exclusionRegex?.test(key)) {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/extractAuthor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
export function extractAuthor(packageJSONContent) {
let author = 'n/a';
if (isObject(packageJSONContent.author)) {
author = packageJSONContent.author.name || "";
author = packageJSONContent.author.name ?? "";
if (packageJSONContent.author.email) {
if (author.length > 0) {
author += ' ';
Expand Down
8 changes: 4 additions & 4 deletions lib/getDependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function getDependencies(
!Array.isArray(inclusions) ||
inclusions.length === 0;
if (
(noDepsTypeDefined || inclusions.indexOf("prod") > -1) &&
(noDepsTypeDefined || inclusions.includes("prod")) &&
packageJson.dependencies !== undefined
) {
addPackagesToIndex(
Expand All @@ -37,7 +37,7 @@ export function getDependencies(
}

if (
(noDepsTypeDefined || inclusions.indexOf("dev") > -1) &&
(noDepsTypeDefined || inclusions.includes("dev")) &&
packageJson.devDependencies !== undefined
) {
addPackagesToIndex(
Expand All @@ -49,7 +49,7 @@ export function getDependencies(
}

if (
(noDepsTypeDefined || inclusions.indexOf("peer") > -1) &&
(noDepsTypeDefined || inclusions.includes("peer")) &&
packageJson.peerDependencies !== undefined
) {
addPackagesToIndex(
Expand All @@ -61,7 +61,7 @@ export function getDependencies(
}

if (
(noDepsTypeDefined || inclusions.indexOf("opt") > -1) &&
(noDepsTypeDefined || inclusions.includes("opt")) &&
packageJson.optionalDependencies !== undefined
) {
addPackagesToIndex(
Expand Down
2 changes: 1 addition & 1 deletion lib/getPackageDataFromRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export async function getPackageDataFromRepository(name) {
`the environment variable '${config.npmTokenEnvVar}' containing the token used by npm to access the private repository should not be a configuration parameter (this is a severe security problem!)`,
);
}
const npmToken = process.env[config.npmTokenEnvVar] || "";
const npmToken = process.env[config.npmTokenEnvVar] ?? "";
if (npmToken.trim().length > 0) {
options['headers'] = { Authorization: `Bearer ${npmToken}` };
}
Expand Down
2 changes: 1 addition & 1 deletion test/getPackageDataFromRepository.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ describe('getPackageDataFromRepository', () => {
config.npmTokenEnvVar = envVarName;
process.env[envVarName] = testToken;

const npmTokenFromConfig = process.env[config.npmTokenEnvVar] || "";
const npmTokenFromConfig = process.env[config.npmTokenEnvVar] ?? "";

assert.strictEqual(npmTokenFromConfig, testToken);
});
Expand Down

0 comments on commit 7b76f60

Please sign in to comment.