Skip to content

Commit

Permalink
refactored into functions and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nedsalk committed Jan 4, 2024
1 parent 8cbd263 commit 29b9189
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 44 deletions.
40 changes: 40 additions & 0 deletions packages/versions/src/lib/semver.test.ts
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);
});
});
83 changes: 39 additions & 44 deletions packages/versions/src/lib/semver.ts
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;
}

0 comments on commit 29b9189

Please sign in to comment.