UNPKG

817 BPlain TextView Raw
1import { Version } from './types';
2
3/**
4 * Comparator function that allows sorting version from lowest to highest
5 * @param versionA version A to compare
6 * @param versionB version B to compare
7 * @returns -1 if versionA comes before versionB, 0 if versionA is equal to version B, and 1 if version A comes after version B
8 */
9export function versionComparator(
10 versionA: Version,
11 versionB: Version
12): -1 | 0 | 1 {
13 if (versionA.major < versionB.major) {
14 return -1;
15 } else if (versionA.major > versionB.major) {
16 return 1;
17 } else if (versionA.minor < versionB.minor) {
18 return -1;
19 } else if (versionA.minor > versionB.minor) {
20 return 1;
21 } else if (versionA.patch < versionB.patch) {
22 return -1;
23 } else if (versionA.patch > versionB.patch) {
24 return 1;
25 } else {
26 return 0;
27 }
28}