2023-07-27 07:04:13 +00:00
|
|
|
const { MAX_LENGTH } = require('../internal/constants')
|
|
|
|
const { re, t } = require('../internal/re')
|
2023-07-24 03:13:08 +00:00
|
|
|
const SemVer = require('../classes/semver')
|
2023-07-27 07:04:13 +00:00
|
|
|
|
|
|
|
const parseOptions = require('../internal/parse-options')
|
|
|
|
const parse = (version, options) => {
|
|
|
|
options = parseOptions(options)
|
|
|
|
|
2023-07-24 03:13:08 +00:00
|
|
|
if (version instanceof SemVer) {
|
|
|
|
return version
|
|
|
|
}
|
2023-07-27 07:04:13 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-07-24 03:13:08 +00:00
|
|
|
try {
|
|
|
|
return new SemVer(version, options)
|
|
|
|
} catch (er) {
|
2023-07-27 07:04:13 +00:00
|
|
|
return null
|
2023-07-24 03:13:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = parse
|