(version, options)
| 24 | |
| 25 | class SemVer { |
| 26 | constructor (version, options) { |
| 27 | options = parseOptions(options) |
| 28 | |
| 29 | if (version instanceof SemVer) { |
| 30 | if (version.loose === !!options.loose && |
| 31 | version.includePrerelease === !!options.includePrerelease) { |
| 32 | return version |
| 33 | } else { |
| 34 | version = version.version |
| 35 | } |
| 36 | } else if (typeof version !== 'string') { |
| 37 | throw new TypeError(`Invalid version. Must be a string. Got type "${typeof version}".`) |
| 38 | } |
| 39 | |
| 40 | if (version.length > MAX_LENGTH) { |
| 41 | throw new TypeError( |
| 42 | `version is longer than ${MAX_LENGTH} characters` |
| 43 | ) |
| 44 | } |
| 45 | |
| 46 | debug('SemVer', version, options) |
| 47 | this.options = options |
| 48 | this.loose = !!options.loose |
| 49 | // this isn't actually relevant for versions, but keep it so that we |
| 50 | // don't run into trouble passing this.options around. |
| 51 | this.includePrerelease = !!options.includePrerelease |
| 52 | |
| 53 | const m = version.trim().match(options.loose ? re[t.LOOSE] : re[t.FULL]) |
| 54 | |
| 55 | if (!m) { |
| 56 | throw new TypeError(`Invalid Version: ${version}`) |
| 57 | } |
| 58 | |
| 59 | this.raw = version |
| 60 | |
| 61 | // these are actually numbers |
| 62 | this.major = +m[1] |
| 63 | this.minor = +m[2] |
| 64 | this.patch = +m[3] |
| 65 | |
| 66 | if (this.major > MAX_SAFE_INTEGER || this.major < 0) { |
| 67 | throw new TypeError('Invalid major version') |
| 68 | } |
| 69 | |
| 70 | if (this.minor > MAX_SAFE_INTEGER || this.minor < 0) { |
| 71 | throw new TypeError('Invalid minor version') |
| 72 | } |
| 73 | |
| 74 | if (this.patch > MAX_SAFE_INTEGER || this.patch < 0) { |
| 75 | throw new TypeError('Invalid patch version') |
| 76 | } |
| 77 | |
| 78 | // numberify any prerelease numeric ids |
| 79 | if (!m[4]) { |
| 80 | this.prerelease = [] |
| 81 | } else { |
| 82 | this.prerelease = m[4].split('.').map((id) => { |
| 83 | if (/^[0-9]+$/.test(id)) { |
nothing calls this directly
no test coverage detected