-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathversion.js
More file actions
65 lines (60 loc) · 2.06 KB
/
version.js
File metadata and controls
65 lines (60 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { scriptVersion } from './1.variables.js';
import * as api from './4.api.js';
const regex = /^(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:-(\d+))?$/;
function parse(string = '') {
const [
_,
major = 0,
minor = 0,
patch = 0,
pre,
] = regex.exec(string) || [];
return {
major: parseInt(major, 10),
minor: parseInt(minor, 10),
patch: parseInt(patch, 10),
pre: pre !== undefined ? parseInt(pre, 10) : undefined,
};
}
// return TRUE for new, FALSE for old, UNDEFINED for same
export default function compare(to, from = scriptVersion) {
if (from === 'L') return false; // Local wins every time
const h = parse(from);
const d = parse(to);
if (h.major === d.major) { // Same major
if (h.minor === d.minor) { // Same minor
if (h.patch === d.patch) { // Same Patch
if (h.pre === undefined && d.pre === undefined) { // Completely same version
return undefined; // Not newer, but also not older
}
// 1.0.0-0 < 1.0.0 (Think of it as subtracting from the main parts)
const noLongerPre = h.pre !== undefined && d.pre === undefined;
return noLongerPre || h.pre < d.pre; // or New pre
}
return h.patch < d.patch; // New patch
}
return h.minor < d.minor; // New minor
}
return h.major < d.major; // New Major
}
function emptyString(...args) {
return args.some((arg) => typeof arg !== 'string' || !arg.trim());
}
api.register('semver', {
isNewer: (ver, current) => {
if (emptyString(ver)) throw new Error('Expected non-empty string');
return compare(ver.trim(), current) === true;
},
atLeast: (ver, current) => {
if (emptyString(ver)) throw new Error('Expected non-empty string');
return compare(ver.trim(), current) !== false;
},
isOlder: (ver, current) => {
if (emptyString(ver)) throw new Error('Expected non-empty string');
return compare(ver.trim(), current) !== true;
},
compare: (newer, current) => {
if (emptyString(newer, current)) throw new Error('Expected non-empty strings');
return compare(newer.trim(), current.trim());
},
});