-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #459 from jellyfin/drop-compare-versions
Drop compare-versions dependency
- Loading branch information
Showing
7 changed files
with
146 additions
and
26 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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,87 @@ | ||
/** | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
import { isVersionLess } from '..'; | ||
|
||
/** | ||
* Versioning tests | ||
* | ||
* @group unit/utils | ||
*/ | ||
describe('Versioning checks', () => { | ||
it("doesn't allow versions with negative numbers", () => { | ||
expect(() => isVersionLess('-10.5.3', '10.8.-9')).toThrow(TypeError); | ||
}); | ||
it("doesn't allow versions with non-numeric characters", () => { | ||
expect(() => isVersionLess('10.2.7-beta', '15.8.9')).toThrow(TypeError); | ||
}); | ||
/** | ||
* Equal | ||
*/ | ||
it('false on equal version: 0.0.0', () => { | ||
expect(isVersionLess('0.0.0', '0.0.0')).toBe(false); | ||
}); | ||
it('false on equal version: 0.0.5', () => { | ||
expect(isVersionLess('0.0.5', '0.0.5')).toBe(false); | ||
}); | ||
it('false on equal version: 0.5.0', () => { | ||
expect(isVersionLess('0.5.0', '0.5.0')).toBe(false); | ||
}); | ||
it('false on equal version: 5.0.0', () => { | ||
expect(isVersionLess('5.0.0', '5.0.0')).toBe(false); | ||
}); | ||
it('false on equal version: 10.8.9', () => { | ||
expect(isVersionLess('10.8.9', '10.8.9')).toBe(false); | ||
}); | ||
/** | ||
* Greater | ||
*/ | ||
it('1.0.0 is greater than 0.0.7', () => { | ||
expect(isVersionLess('1.0.0', '0.0.7')).toBe(false); | ||
}); | ||
it('1.0.0 is greater than 0.5.0', () => { | ||
expect(isVersionLess('1.0.0', '0.5.0')).toBe(false); | ||
}); | ||
it('1.0.0 is greater than 0.5.7', () => { | ||
expect(isVersionLess('1.0.0', '0.5.7')).toBe(false); | ||
}); | ||
it('2.0.0 is greater than 1.0.0', () => { | ||
expect(isVersionLess('2.0.0', '1.0.0')).toBe(false); | ||
}); | ||
it('0.5.0 is greater than 0.0.7', () => { | ||
expect(isVersionLess('0.5.0', '0.0.7')).toBe(false); | ||
}); | ||
it('0.5.7 is greater than 0.0.7', () => { | ||
expect(isVersionLess('0.5.7', '0.0.7')).toBe(false); | ||
}); | ||
it('2-digit versions: 12.25.34 is greater than 12.17.89', () => { | ||
expect(isVersionLess('12.25.34', '12.17.89')).toBe(false); | ||
}); | ||
/** | ||
* Less | ||
*/ | ||
it('0.0.7 is less than 1.0.0', () => { | ||
expect(isVersionLess('0.0.7', '1.0.0')).toBe(true); | ||
}); | ||
it('0.5.0 is less than 1.0.0', () => { | ||
expect(isVersionLess('0.5.0', '1.0.0')).toBe(true); | ||
}); | ||
it('0.5.7 is less than 1.0.0', () => { | ||
expect(isVersionLess('0.5.7', '1.0.0')).toBe(true); | ||
}); | ||
it('1.0.0 is less than 2.0.0', () => { | ||
expect(isVersionLess('1.0.0', '2.0.0')).toBe(true); | ||
}); | ||
it('0.0.7 is less than 0.5.0', () => { | ||
expect(isVersionLess('0.0.7', '0.5.0')).toBe(true); | ||
}); | ||
it('0.0.7 is less than 0.5.7', () => { | ||
expect(isVersionLess('0.0.7', '0.5.7')).toBe(true); | ||
}); | ||
it('2-digit versions: 12.17.89 is less than 12.25.34', () => { | ||
expect(isVersionLess('12.17.89', '12.25.34')).toBe(true); | ||
}); | ||
}); |
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
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,37 @@ | ||
/** | ||
* @returns - Array of 3 numbers or undefined | ||
* (if the passed string it's not in the MAJOR.MINOR.PATCH numeric-only format) | ||
*/ | ||
function parseVersion(version: string): number[] | undefined { | ||
const arr = version.split('.').map(Number); | ||
|
||
if (arr.length === 3 && arr.every((n) => n >= 0)) { | ||
return arr; | ||
} | ||
} | ||
|
||
/** | ||
* Check if given version is less than a baseline | ||
* | ||
* Versions must be in semver format: X.Y.Z (Major.Minor.Patch) | ||
* @throws If version is not in a numeric-only semver format | ||
* @param check - The version to check | ||
* @param baseline - The minimum version considered supported | ||
*/ | ||
export function isVersionLess(check: string, baseline: string): boolean { | ||
const parsedCheck = parseVersion(check); | ||
const parsedBaseline = parseVersion(baseline); | ||
|
||
if (!parsedCheck || !parsedBaseline) { | ||
throw new TypeError("Version doesn't match a numeric-only semver format"); | ||
} | ||
|
||
const [majorCheck, minorCheck, patchCheck] = parsedCheck; | ||
const [majorBaseline, minorBaseline, patchBaseline] = parsedBaseline; | ||
|
||
return ( | ||
majorCheck < majorBaseline || | ||
(majorCheck === majorBaseline && minorCheck < minorBaseline) || | ||
(majorCheck === majorBaseline && minorCheck === minorBaseline && patchCheck < patchBaseline) | ||
); | ||
} |