-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
93 lines (77 loc) · 2.13 KB
/
index.js
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const SemVersion = (() => {
let version = {
major: 0,
minor: 0,
patch: 0,
pre: undefined,
meta: undefined,
}
const isSemVersion = v =>
v &&
v.hasOwnProperty('major') &&
typeof v.major === 'number' &&
v.hasOwnProperty('minor') &&
typeof v.minor === 'number' &&
v.hasOwnProperty('patch') &&
typeof v.patch === 'number'
const parse = v => {
if (isSemVersion(v)) return v
const [nonMeta, meta] = v.split('+', 2)
const [nonPre, pre] = nonMeta.split('-', 2)
const [major = 0, minor = 0, patch = 0] = nonPre
.split('.', 3)
.map(part => parseInt(part, 10))
return { major, minor, patch, pre, meta }
}
function from(v) {
version = parse(v)
return this
}
const get = () => version
const toString = (v = version) => {
let string = [v.major, v.minor, v.patch].join('.')
if (v.pre) string += `-${v.pre}`
if (v.meta) string += `+${v.meta}`
return string
}
const cmp = (a, b) => {
if (a.major == b.major && a.minor == b.minor && a.patch == b.patch) {
if (typeof a.pre == 'undefined' && typeof b.pre == 'undefined') {
return 0
}
if (typeof a.pre == 'undefined' || typeof b.pre == 'undefined') {
if (typeof a.pre !== 'undefined') return 1
if (typeof b.pre !== 'undefined') return -1
}
if (a.pre == b.pre) return 0
return a.pre > b.pre ? 1 : -1
}
if (
a.major > b.major ||
(a.major == b.major && a.minor > b.minor) ||
(a.major == b.major && a.minor === b.minor && a.patch > b.patch)
)
return 1
if (
a.major < b.major ||
(a.major == b.major && a.minor < b.minor) ||
(a.major == b.major && a.minor === b.minor && a.patch < b.patch)
)
return -1
return 0
}
return {
isSemVersion,
parse,
from,
get,
toString,
cmp: (a, b) => cmp(parse(a), parse(b)),
eq: v => cmp(version, parse(v)) === 0,
gt: v => cmp(version, parse(v)) > 0,
ge: v => cmp(version, parse(v)) >= 0,
lt: v => cmp(version, parse(v)) < 0,
le: v => cmp(version, parse(v)) <= 0,
}
})()
module.exports = SemVersion