2020-08-25 23:57:08 +00:00
|
|
|
const {MAX_LENGTH} = require('../internal/constants')
|
|
|
|
const { re, t } = require('../internal/re')
|
|
|
|
const SemVer = require('../classes/semver')
|
|
|
|
|
2021-02-26 03:58:33 +00:00
|
|
|
const parseOptions = require('../internal/parse-options')
|
2020-08-25 23:57:08 +00:00
|
|
|
const parse = (version, options) => {
|
2021-02-26 03:58:33 +00:00
|
|
|
options = parseOptions(options)
|
2020-08-25 23:57:08 +00:00
|
|
|
|
|
|
|
if (version instanceof SemVer) {
|
|
|
|
return version
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof version !== 'string') {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
if (version.length > MAX_LENGTH) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
const r = options.loose ? re[t.LOOSE] : re[t.FULL]
|
|
|
|
if (!r.test(version)) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return new SemVer(version, options)
|
|
|
|
} catch (er) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = parse
|