Skip to content

Commit

Permalink
refactor: undefined to null
Browse files Browse the repository at this point in the history
Replace undefined with null as the return value for  tryGetLatestVersion since the null here represents an intentionally missing value. Also convert it to a named function to be more in line with other functions.
  • Loading branch information
ComradeVanti committed Sep 29, 2024
1 parent 200266a commit 0802dcd
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/cli/output-formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function formatAsTable(
if (packument.date) {
date = packument.date.toISOString().slice(0, 10);
}
assert(version !== undefined);
assert(version !== null);
return [name, version, date];
}

Expand All @@ -65,7 +65,7 @@ export function formatPackumentInfo(

const versionCount = recordKeys(packument.versions).length;
const ver = tryGetLatestVersion(packument);
assert(ver !== undefined);
assert(ver !== null);
const verInfo = packument.versions[ver]!;
const license = verInfo.license || "proprietary or unlicensed";
const displayName = verInfo.displayName;
Expand Down
10 changes: 5 additions & 5 deletions src/domain/packument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ const hasLatestDistTag = <T extends VersionedPackument>(
* @param packument The package. All properties are assumed to be potentially
* missing.
*/
export const tryGetLatestVersion = function (
export function tryGetLatestVersion(
packument: VersionedPackument
): SemanticVersion | undefined {
): SemanticVersion | null {
if (hasLatestDistTag(packument)) return packument["dist-tags"].latest;

if (packument.version !== undefined) return packument.version;

return undefined;
};
return null;
}

/**
* Attempts to get a specific version from a packument.
Expand Down Expand Up @@ -193,7 +193,7 @@ export function tryResolvePackumentVersion(
// Find the latest version
if (requestedVersion === undefined || requestedVersion === "latest") {
let latestVersion = tryGetLatestVersion(packument);
if (latestVersion === undefined) latestVersion = availableVersions.at(-1)!;
if (latestVersion === null) latestVersion = availableVersions.at(-1)!;
return Ok(tryGetPackumentVersion(packument, latestVersion)!);
}

Expand Down
2 changes: 1 addition & 1 deletion test/unit/domain/packument.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe("packument", () => {

const version = tryGetLatestVersion(packument);

expect(version).toBeUndefined();
expect(version).toBeNull();
});
});

Expand Down

0 comments on commit 0802dcd

Please sign in to comment.