Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(semver): unify param names #5700

Merged
merged 4 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions semver/can_parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import { parse } from "./parse.ts";
* assertFalse(canParse("invalid"));
* ```
*
* @param version The version string to check
* @param value The version string to check
* @returns `true` if the string can be parsed as SemVer, `false` otherwise
*/
export function canParse(version: string): boolean {
export function canParse(value: string): boolean {
try {
parse(version);
parse(value);
return true;
} catch {
return false;
Expand Down
35 changes: 16 additions & 19 deletions semver/compare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
/**
* Compare two SemVers.
*
* Returns `0` if `s0` equals `s1`, or `1` if `s0` is greater, or `-1` if `s1` is
* Returns `0` if `version1` equals `version2`, or `1` if `version1` is greater, or `-1` if `version2` is
* greater.
*
* Sorts in ascending order if passed to {@linkcode Array.sort}.
Expand All @@ -20,28 +20,25 @@ import {
* import { parse, compare } from "@std/semver";
* import { assertEquals } from "@std/assert";
*
* const s0 = parse("1.2.3");
* const s1 = parse("1.2.4");
* const version1 = parse("1.2.3");
* const version2 = parse("1.2.4");
*
* assertEquals(compare(s0, s1), -1);
* assertEquals(compare(s1, s0), 1);
* assertEquals(compare(s0, s0), 0);
* assertEquals(compare(version1, version2), -1);
* assertEquals(compare(version2, version1), 1);
* assertEquals(compare(version1, version1), 0);
* ```
*
* @param s0 The first SemVer to compare
* @param s1 The second SemVer to compare
* @returns `1` if `s0` is greater, `0` if equal, or `-1` if `s1` is greater
* @param version1 The first SemVer to compare
* @param version2 The second SemVer to compare
* @returns `1` if `version1` is greater, `0` if equal, or `-1` if `version2` is greater
*/
export function compare(
s0: SemVer,
s1: SemVer,
): 1 | 0 | -1 {
if (s0 === s1) return 0;
export function compare(version1: SemVer, version2: SemVer): 1 | 0 | -1 {
if (version1 === version2) return 0;
return (
compareNumber(s0.major, s1.major) ||
compareNumber(s0.minor, s1.minor) ||
compareNumber(s0.patch, s1.patch) ||
checkIdentifier(s0.prerelease, s1.prerelease) ||
compareIdentifier(s0.prerelease, s1.prerelease)
compareNumber(version1.major, version2.major) ||
compareNumber(version1.minor, version2.minor) ||
compareNumber(version1.patch, version2.patch) ||
checkIdentifier(version1.prerelease, version2.prerelease) ||
compareIdentifier(version1.prerelease, version2.prerelease)
);
}
42 changes: 26 additions & 16 deletions semver/difference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,39 @@ import { compareIdentifier } from "./_shared.ts";
* import { parse, difference } from "@std/semver";
* import { assertEquals } from "@std/assert";
*
* const s0 = parse("1.2.3");
* const s1 = parse("1.2.4");
* const s2 = parse("1.3.0");
* const s3 = parse("2.0.0");
* const version1 = parse("1.2.3");
* const version2 = parse("1.2.4");
* const version3 = parse("1.3.0");
* const version4 = parse("2.0.0");
*
* assertEquals(difference(s0, s1), "patch");
* assertEquals(difference(s0, s2), "minor");
* assertEquals(difference(s0, s3), "major");
* assertEquals(difference(s0, s0), undefined);
* assertEquals(difference(version1, version2), "patch");
* assertEquals(difference(version1, version3), "minor");
* assertEquals(difference(version1, version4), "major");
* assertEquals(difference(version1, version1), undefined);
* ```
*
* @param s0 The first SemVer to compare
* @param s1 The second SemVer to compare
* @param version1 The first SemVer to compare
* @param version2 The second SemVer to compare
* @returns The release type difference or `undefined` if the versions are the same
*/
export function difference(s0: SemVer, s1: SemVer): ReleaseType | undefined {
const hasPrerelease = s0.prerelease?.length || s1.prerelease?.length;
export function difference(
version1: SemVer,
version2: SemVer,
): ReleaseType | undefined {
const hasPrerelease = version1.prerelease?.length ||
version2.prerelease?.length;

if (s0.major !== s1.major) return hasPrerelease ? "premajor" : "major";
if (s0.minor !== s1.minor) return hasPrerelease ? "preminor" : "minor";
if (s0.patch !== s1.patch) return hasPrerelease ? "prepatch" : "patch";
if (version1.major !== version2.major) {
return hasPrerelease ? "premajor" : "major";
}
if (version1.minor !== version2.minor) {
return hasPrerelease ? "preminor" : "minor";
}
if (version1.patch !== version2.patch) {
return hasPrerelease ? "prepatch" : "patch";
}

if (compareIdentifier(s0.prerelease, s1.prerelease) !== 0) {
if (compareIdentifier(version1.prerelease, version2.prerelease) !== 0) {
return "prerelease";
}
}
20 changes: 10 additions & 10 deletions semver/equals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ import type { SemVer } from "./types.ts";
/**
* Returns `true` if both SemVers are equivalent.
*
* This is equal to `compare(s0, s1) === 0`.
* This is equal to `compare(version1, version2) === 0`.
*
* @example Usage
* ```ts
* import { parse, equals } from "@std/semver";
* import { assert } from "@std/assert";
*
* const s0 = parse("1.2.3");
* const s1 = parse("1.2.3");
* const version1 = parse("1.2.3");
* const version2 = parse("1.2.3");
*
* assert(equals(s0, s1));
* assert(!equals(s0, parse("1.2.4")));
* assert(equals(version1, version2));
* assert(!equals(version1, parse("1.2.4")));
* ```
*
* @param s0 The first SemVer to compare
* @param s1 The second SemVer to compare
* @returns `true` if `s0` is equal to `s1`, `false` otherwise
* @param version1 The first SemVer to compare
* @param version2 The second SemVer to compare
* @returns `true` if `version1` is equal to `version2`, `false` otherwise
*/
export function equals(s0: SemVer, s1: SemVer): boolean {
return compare(s0, s1) === 0;
export function equals(version1: SemVer, version2: SemVer): boolean {
return compare(version1, version2) === 0;
}
14 changes: 7 additions & 7 deletions semver/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ function formatNumber(value: number) {
* assertEquals(format(semver), "1.2.3");
* ```
*
* @param semver The SemVer to format
* @param version The SemVer to format
* @returns The string representation of a semantic version.
*/
export function format(semver: SemVer): string {
const major = formatNumber(semver.major);
const minor = formatNumber(semver.minor);
const patch = formatNumber(semver.patch);
const pre = semver.prerelease?.join(".") ?? "";
const build = semver.build?.join(".") ?? "";
export function format(version: SemVer): string {
const major = formatNumber(version.major);
const minor = formatNumber(version.minor);
const patch = formatNumber(version.patch);
const pre = version.prerelease?.join(".") ?? "";
const build = version.build?.join(".") ?? "";

const primary = `${major}.${minor}.${patch}`;
const release = [primary, pre].filter((v) => v).join("-");
Expand Down
22 changes: 11 additions & 11 deletions semver/greater_or_equal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@ import { compare } from "./compare.ts";
/**
* Greater than or equal to comparison for two SemVers.
*
* This is equal to `compare(s0, s1) >= 0`.
* This is equal to `compare(version1, version2) >= 0`.
*
* @example Usage
* ```ts
* import { parse, greaterOrEqual } from "@std/semver";
* import { assert } from "@std/assert";
*
* const s0 = parse("1.2.3");
* const s1 = parse("1.2.4");
* const version1 = parse("1.2.3");
* const version2 = parse("1.2.4");
*
* assert(greaterOrEqual(s1, s0));
* assert(!greaterOrEqual(s0, s1));
* assert(greaterOrEqual(s0, s0));
* assert(greaterOrEqual(version2, version1));
* assert(!greaterOrEqual(version1, version2));
* assert(greaterOrEqual(version1, version1));
* ```
*
* @param s0 The first version to compare
* @param s1 The second version to compare
* @returns `true` if `s0` is greater than or equal to `s1`, `false` otherwise
* @param version1 The first version to compare
* @param version2 The second version to compare
* @returns `true` if `version1` is greater than or equal to `version2`, `false` otherwise
*/
export function greaterOrEqual(s0: SemVer, s1: SemVer): boolean {
return compare(s0, s1) >= 0;
export function greaterOrEqual(version1: SemVer, version2: SemVer): boolean {
return compare(version1, version2) >= 0;
}
22 changes: 11 additions & 11 deletions semver/greater_than.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@ import { compare } from "./compare.ts";
/**
* Greater than comparison for two SemVers.
*
* This is equal to `compare(s0, s1) > 0`.
* This is equal to `compare(version1, version2) > 0`.
*
* @example Usage
* ```ts
* import { parse, greaterThan } from "@std/semver";
* import { assert } from "@std/assert";
*
* const s0 = parse("1.2.3");
* const s1 = parse("1.2.4");
* const version1 = parse("1.2.3");
* const version2 = parse("1.2.4");
*
* assert(greaterThan(s1, s0));
* assert(!greaterThan(s0, s1));
* assert(!greaterThan(s0, s0));
* assert(greaterThan(version2, version1));
* assert(!greaterThan(version1, version2));
* assert(!greaterThan(version1, version1));
* ```
*
* @param s0 The first version to compare
* @param s1 The second version to compare
* @returns `true` if `s0` is greater than `s1`, `false` otherwise
* @param version1 The first version to compare
* @param version2 The second version to compare
* @returns `true` if `version1` is greater than `version2`, `false` otherwise
*/
export function greaterThan(s0: SemVer, s1: SemVer): boolean {
return compare(s0, s1) > 0;
export function greaterThan(version1: SemVer, version2: SemVer): boolean {
return compare(version1, version2) > 0;
}
24 changes: 12 additions & 12 deletions semver/greater_than_range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,41 @@ import { compare } from "./compare.ts";
* import { parse, parseRange, greaterThanRange } from "@std/semver";
* import { assert } from "@std/assert";
*
* const v0 = parse("1.2.3");
* const v1 = parse("1.2.4");
* const version1 = parse("1.2.3");
* const version2 = parse("1.2.4");
* const range = parseRange(">=1.2.3 <1.2.4");
*
* assert(!greaterThanRange(v0, range));
* assert(greaterThanRange(v1, range));
* assert(!greaterThanRange(version1, range));
* assert(greaterThanRange(version2, range));
* ```
*
* @param semver The version to check.
* @param version The version to check.
* @param range The range to check against.
* @returns `true` if the semver is greater than the range, `false` otherwise.
*/
export function greaterThanRange(semver: SemVer, range: Range): boolean {
export function greaterThanRange(version: SemVer, range: Range): boolean {
return range.every((comparatorSet) =>
greaterThanComparatorSet(semver, comparatorSet)
greaterThanComparatorSet(version, comparatorSet)
);
}

function greaterThanComparatorSet(
semver: SemVer,
version: SemVer,
comparatorSet: Comparator[],
): boolean {
// If the comparator set contains wildcard, then the semver is not greater than the range.
if (comparatorSet.some(isWildcardComparator)) return false;
// If the semver satisfies the comparator set, then it's not greater than the range.
if (testComparatorSet(semver, comparatorSet)) return false;
if (testComparatorSet(version, comparatorSet)) return false;
// If the semver is less than any of the comparator set, then it's not greater than the range.
if (
comparatorSet.some((comparator) => lessThanComparator(semver, comparator))
comparatorSet.some((comparator) => lessThanComparator(version, comparator))
) return false;
return true;
}

function lessThanComparator(semver: SemVer, comparator: Comparator): boolean {
const cmp = compare(semver, comparator);
function lessThanComparator(version: SemVer, comparator: Comparator): boolean {
const cmp = compare(version, comparator);
switch (comparator.operator) {
case "=":
case undefined:
Expand Down
16 changes: 8 additions & 8 deletions semver/increment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ export interface IncrementOptions {
export function increment(
version: SemVer,
release: ReleaseType,
options?: IncrementOptions,
options: IncrementOptions = {},
): SemVer {
const build = options?.build !== undefined
? parseBuild(options?.build)
const build = options.build !== undefined
? parseBuild(options.build)
: version.build;

switch (release) {
Expand All @@ -99,23 +99,23 @@ export function increment(
major: version.major + 1,
minor: 0,
patch: 0,
prerelease: bumpPrerelease(version.prerelease, options?.prerelease),
prerelease: bumpPrerelease(version.prerelease, options.prerelease),
build,
};
case "preminor":
return {
major: version.major,
minor: version.minor + 1,
patch: 0,
prerelease: bumpPrerelease(version.prerelease, options?.prerelease),
prerelease: bumpPrerelease(version.prerelease, options.prerelease),
build,
};
case "prepatch":
return {
major: version.major,
minor: version.minor,
patch: version.patch + 1,
prerelease: bumpPrerelease(version.prerelease, options?.prerelease),
prerelease: bumpPrerelease(version.prerelease, options.prerelease),
build,
};
case "prerelease": {
Expand All @@ -126,7 +126,7 @@ export function increment(
major: version.major,
minor: version.minor,
patch,
prerelease: bumpPrerelease(version.prerelease, options?.prerelease),
prerelease: bumpPrerelease(version.prerelease, options.prerelease),
build,
};
}
Expand Down Expand Up @@ -186,7 +186,7 @@ export function increment(
major: version.major,
minor: version.minor,
patch: version.patch,
prerelease: bumpPrerelease(version.prerelease, options?.prerelease),
prerelease: bumpPrerelease(version.prerelease, options.prerelease),
build,
};
}
Expand Down
Loading