2022-10-17 18:03:20 +08:00
|
|
|
const { MAX_LENGTH } = require('../internal/constants')
|
2021-04-10 08:16:12 +08:00
|
|
|
const { re, t } = require('../internal/re')
|
|
|
|
const SemVer = require('../classes/semver')
|
|
|
|
|
|
|
|
const parseOptions = require('../internal/parse-options')
|
|
|
|
const parse = (version, options) => {
|
|
|
|
options = parseOptions(options)
|
|
|
|
|
|
|
|
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
|