UNPKG

662 BJavaScriptView Raw
1const { MAX_LENGTH } = require('../internal/constants')
2const { re, t } = require('../internal/re')
3const SemVer = require('../classes/semver')
4
5const parseOptions = require('../internal/parse-options')
6const parse = (version, options) => {
7 options = parseOptions(options)
8
9 if (version instanceof SemVer) {
10 return version
11 }
12
13 if (typeof version !== 'string') {
14 return null
15 }
16
17 if (version.length > MAX_LENGTH) {
18 return null
19 }
20
21 const r = options.loose ? re[t.LOOSE] : re[t.FULL]
22 if (!r.test(version)) {
23 return null
24 }
25
26 try {
27 return new SemVer(version, options)
28 } catch (er) {
29 return null
30 }
31}
32
33module.exports = parse