1 | const SemVer = require('../classes/semver')
|
2 | const parse = require('./parse')
|
3 | const {re, t} = require('../internal/re')
|
4 |
|
5 | const coerce = (version, options) => {
|
6 | if (version instanceof SemVer) {
|
7 | return version
|
8 | }
|
9 |
|
10 | if (typeof version === 'number') {
|
11 | version = String(version)
|
12 | }
|
13 |
|
14 | if (typeof version !== 'string') {
|
15 | return null
|
16 | }
|
17 |
|
18 | options = options || {}
|
19 |
|
20 | let match = null
|
21 | if (!options.rtl) {
|
22 | match = version.match(re[t.COERCE])
|
23 | } else {
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | let next
|
33 | while ((next = re[t.COERCERTL].exec(version)) &&
|
34 | (!match || match.index + match[0].length !== version.length)
|
35 | ) {
|
36 | if (!match ||
|
37 | next.index + next[0].length !== match.index + match[0].length) {
|
38 | match = next
|
39 | }
|
40 | re[t.COERCERTL].lastIndex = next.index + next[1].length + next[2].length
|
41 | }
|
42 |
|
43 | re[t.COERCERTL].lastIndex = -1
|
44 | }
|
45 |
|
46 | if (match === null)
|
47 | return null
|
48 |
|
49 | return parse(`${match[2]}.${match[3] || '0'}.${match[4] || '0'}`, options)
|
50 | }
|
51 | module.exports = coerce
|