-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactored into functions and added tests
- Loading branch information
Showing
2 changed files
with
79 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { eq, gt, majorEq, minorEq, patchEq } from './semver'; | ||
|
||
describe('semver', () => { | ||
test('majorEq', () => { | ||
expect(majorEq('1.2.3', '1.2.3')).toBe(true); | ||
expect(majorEq('1.2.3', '1.2.4')).toBe(true); | ||
expect(majorEq('1.2.3', '1.3.3')).toBe(true); | ||
expect(majorEq('1.2.3', '2.2.3')).toBe(false); | ||
}); | ||
test('minorEq', () => { | ||
expect(minorEq('1.2.3', '1.2.3')).toBe(true); | ||
expect(minorEq('1.2.3', '1.2.4')).toBe(true); | ||
expect(minorEq('1.2.3', '2.2.3')).toBe(true); | ||
expect(minorEq('1.2.3', '1.3.3')).toBe(false); | ||
}); | ||
test('patchEq', () => { | ||
expect(patchEq('1.2.3', '1.2.3')).toBe(true); | ||
expect(patchEq('1.2.3', '1.3.3')).toBe(true); | ||
expect(patchEq('1.2.3', '2.2.3')).toBe(true); | ||
expect(patchEq('1.2.3', '1.2.4')).toBe(false); | ||
}); | ||
test('gt', () => { | ||
expect(gt('1.2.3', '0.2.3')).toBe(true); | ||
expect(gt('1.2.3', '1.1.3')).toBe(true); | ||
expect(gt('1.2.3', '1.2.2')).toBe(true); | ||
expect(gt('1.2.3', '1.2.3')).toBe(false); | ||
expect(gt('1.2.3', '2.2.3')).toBe(false); | ||
expect(gt('1.2.3', '1.3.3')).toBe(false); | ||
expect(gt('1.2.3', '1.2.4')).toBe(false); | ||
}); | ||
test('eq', () => { | ||
expect(eq('1.2.3', '1.2.3')).toBe(true); | ||
expect(eq('1.2.3', '0.2.3')).toBe(false); | ||
expect(eq('1.2.3', '2.2.3')).toBe(false); | ||
expect(eq('1.2.3', '1.1.3')).toBe(false); | ||
expect(eq('1.2.3', '1.3.3')).toBe(false); | ||
expect(eq('1.2.3', '1.2.2')).toBe(false); | ||
expect(eq('1.2.3', '1.2.4')).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,46 @@ | ||
export class Semver { | ||
private major: number; | ||
private minor: number; | ||
private patch: number; | ||
|
||
private constructor(version: string) { | ||
const [major, minor, patch] = version.split('.').map((v) => parseInt(v, 10)); | ||
this.major = major; | ||
this.minor = minor; | ||
this.patch = patch; | ||
} | ||
|
||
private static compareVersions(version1: string, version2: string): number { | ||
const semver1 = new Semver(version1); | ||
const semver2 = new Semver(version2); | ||
function parseVersion(version: string): { major: number; minor: number; patch: number } { | ||
const [major, minor, patch] = version.split('.').map((v) => parseInt(v, 10)); | ||
return { major, minor, patch }; | ||
} | ||
|
||
if (semver1.major !== semver2.major) { | ||
return semver1.major - semver2.major; | ||
} | ||
if (semver1.minor !== semver2.minor) { | ||
return semver1.minor - semver2.minor; | ||
} | ||
return semver1.patch - semver2.patch; | ||
} | ||
function versionDiffs( | ||
version1: string, | ||
version2: string | ||
): { major: number; minor: number; patch: number; fullVersionDiff: number } { | ||
const semver1 = parseVersion(version1); | ||
const semver2 = parseVersion(version2); | ||
const major = semver1.major - semver2.major; | ||
const minor = semver1.minor - semver2.minor; | ||
const patch = semver1.patch - semver2.patch; | ||
return { | ||
major, | ||
minor, | ||
patch, | ||
fullVersionDiff: major || minor || patch, | ||
}; | ||
} | ||
|
||
public static gt(version1: string, version2: string): boolean { | ||
return this.compareVersions(version1, version2) > 0; | ||
} | ||
export function gt(version1: string, version2: string): boolean { | ||
const { fullVersionDiff } = versionDiffs(version1, version2); | ||
return fullVersionDiff > 0; | ||
} | ||
|
||
public static eq(version1: string, version2: string): boolean { | ||
return this.compareVersions(version1, version2) === 0; | ||
} | ||
export function eq(version1: string, version2: string): boolean { | ||
const { fullVersionDiff } = versionDiffs(version1, version2); | ||
return fullVersionDiff === 0; | ||
} | ||
|
||
public static majorEq(version1: string, version2: string): boolean { | ||
const semver1 = new Semver(version1); | ||
const semver2 = new Semver(version2); | ||
return semver1.major === semver2.major; | ||
} | ||
export function majorEq(version1: string, version2: string): boolean { | ||
const { major } = versionDiffs(version1, version2); | ||
return major === 0; | ||
} | ||
|
||
public static minorEq(version1: string, version2: string): boolean { | ||
const semver1 = new Semver(version1); | ||
const semver2 = new Semver(version2); | ||
return semver1.minor === semver2.minor; | ||
} | ||
export function minorEq(version1: string, version2: string): boolean { | ||
const { minor } = versionDiffs(version1, version2); | ||
return minor === 0; | ||
} | ||
|
||
public static patchEq(version1: string, version2: string): boolean { | ||
const semver1 = new Semver(version1); | ||
const semver2 = new Semver(version2); | ||
return semver1.patch === semver2.patch; | ||
} | ||
export function patchEq(version1: string, version2: string): boolean { | ||
const { patch } = versionDiffs(version1, version2); | ||
return patch === 0; | ||
} |