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