From 3c3071ff8155df103d5ad0d545ff8efc88096bc4 Mon Sep 17 00:00:00 2001 From: kirillgroshkov Date: Tue, 23 Jul 2024 16:05:21 +0200 Subject: [PATCH] fix: refactor SemVer method naming --- scripts/semverBench.ts | 2 +- src/semver.test.ts | 6 +++--- src/semver.ts | 34 ++++++++++++++++++---------------- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/scripts/semverBench.ts b/scripts/semverBench.ts index 6df2c8cc..b220bc07 100644 --- a/scripts/semverBench.ts +++ b/scripts/semverBench.ts @@ -18,7 +18,7 @@ runBenchScript({ const _a: any[] = [] _range(data.length).forEach(i => { - _a.push(semver2(data[i]!).cmp(data2[i]!)) + _a.push(semver2(data[i]!).compare(data2[i]!)) }) }, semver: () => { diff --git a/src/semver.test.ts b/src/semver.test.ts index 1412e3e6..cc99731f 100644 --- a/src/semver.test.ts +++ b/src/semver.test.ts @@ -12,8 +12,8 @@ test('basic', () => { expect(s.minor).toBe(2) expect(s.patch).toBe(3) - const s2 = semver2.of('1.2.5') - expect(s.cmp(s2)).toBe(-1) + const s2 = semver2.fromInput('1.2.5') + expect(s.compare(s2)).toBe(-1) expect(s.isAfter(s2)).toBe(false) expect(s.isSameOrAfter(s2)).toBe(false) expect(s.isBefore(s2)).toBe(true) @@ -63,7 +63,7 @@ test.each([ ['.', '0.0.0'], ['x', '0.0.0'], ])('parse', (str, expected) => { - expect(semver2.parseOrNull(str)?.toString()).toBe(expected) + expect(semver2.fromInputOrUndefined(str)?.toString()).toBe(expected) }) test.each([ diff --git a/src/semver.ts b/src/semver.ts index 3e1f1634..0d7b00fd 100644 --- a/src/semver.ts +++ b/src/semver.ts @@ -36,19 +36,19 @@ export class Semver { return this.tokens[2] } - isAfter = (other: SemverInput): boolean => this.cmp(other) > 0 - isSameOrAfter = (other: SemverInput): boolean => this.cmp(other) >= 0 - isBefore = (other: SemverInput): boolean => this.cmp(other) < 0 - isSameOrBefore = (other: SemverInput): boolean => this.cmp(other) <= 0 - isSame = (other: SemverInput): boolean => this.cmp(other) === 0 + isAfter = (other: SemverInput): boolean => this.compare(other) > 0 + isSameOrAfter = (other: SemverInput): boolean => this.compare(other) >= 0 + isBefore = (other: SemverInput): boolean => this.compare(other) < 0 + isSameOrBefore = (other: SemverInput): boolean => this.compare(other) <= 0 + isSame = (other: SemverInput): boolean => this.compare(other) === 0 /** * Returns 1 if this > other * returns 0 if they are equal * returns -1 if this < other */ - cmp(other: SemverInput): -1 | 0 | 1 { - const { tokens } = semver2.of(other) + compare(other: SemverInput): -1 | 0 | 1 { + const { tokens } = semver2.fromInput(other) for (let i = 0; i < 3; i++) { if (this.tokens[i]! < tokens[i]!) return -1 if (this.tokens[i]! > tokens[i]!) return 1 @@ -64,18 +64,18 @@ export class Semver { } class SemverFactory { - of(input: SemverInput): Semver { - const s = this.parseOrNull(input) + fromInput(input: SemverInput): Semver { + const s = this.fromInputOrUndefined(input) - _assert(s !== null, `Cannot parse "${input}" into Semver`, { + _assert(s, `Cannot parse "${input}" into Semver`, { input, }) return s } - parseOrNull(input: SemverInputNullable): Semver | null { - if (!input) return null + fromInputOrUndefined(input: SemverInputNullable): Semver | undefined { + if (!input) return if (input instanceof Semver) return input const t = input.split('.') @@ -96,7 +96,9 @@ class SemverFactory { max(items: SemverInputNullable[]): Semver { const items2 = items.filter(_isTruthy) _assert(items2.length, 'semver.max called on empty array') - return items2.map(i => this.of(i)).reduce((max, item) => (max.isSameOrAfter(item) ? max : item)) + return items2 + .map(i => this.fromInput(i)) + .reduce((max, item) => (max.isSameOrAfter(item) ? max : item)) } /** @@ -114,7 +116,7 @@ class SemverFactory { const items2 = items.filter(_isTruthy) _assert(items2.length, 'semver.min called on empty array') return items2 - .map(i => this.of(i)) + .map(i => this.fromInput(i)) .reduce((min, item) => (min.isSameOrBefore(item) ? min : item)) } @@ -123,7 +125,7 @@ class SemverFactory { */ sort(items: Semver[], dir: SortDirection = 'asc', mutate = false): Semver[] { const mod = dir === 'desc' ? -1 : 1 - return (mutate ? items : [...items]).sort((a, b) => a.cmp(b) * mod) + return (mutate ? items : [...items]).sort((a, b) => a.compare(b) * mod) } } @@ -133,7 +135,7 @@ interface SemverFn extends SemverFactory { const semverFactory = new SemverFactory() -export const semver2 = semverFactory.of.bind(semverFactory) as SemverFn +export const semver2 = semverFactory.fromInput.bind(semverFactory) as SemverFn // The line below is the blackest of black magic I have ever written in 2024. // And probably 2023 as well.