| 3222 | exports.SemVer = SemVer; |
| 3223 | |
| 3224 | function SemVer(version, loose) { |
| 3225 | if (version instanceof SemVer) { |
| 3226 | if (version.loose === loose) |
| 3227 | return version; |
| 3228 | else |
| 3229 | version = version.version; |
| 3230 | } else if (typeof version !== 'string') { |
| 3231 | throw new TypeError('Invalid Version: ' + version); |
| 3232 | } |
| 3233 | |
| 3234 | if (version.length > MAX_LENGTH) |
| 3235 | throw new TypeError('version is longer than ' + MAX_LENGTH + ' characters') |
| 3236 | |
| 3237 | if (!(this instanceof SemVer)) |
| 3238 | return new SemVer(version, loose); |
| 3239 | |
| 3240 | debug('SemVer', version, loose); |
| 3241 | this.loose = loose; |
| 3242 | var m = version.trim().match(loose ? re[LOOSE] : re[FULL]); |
| 3243 | |
| 3244 | if (!m) |
| 3245 | throw new TypeError('Invalid Version: ' + version); |
| 3246 | |
| 3247 | this.raw = version; |
| 3248 | |
| 3249 | // these are actually numbers |
| 3250 | this.major = +m[1]; |
| 3251 | this.minor = +m[2]; |
| 3252 | this.patch = +m[3]; |
| 3253 | |
| 3254 | if (this.major > MAX_SAFE_INTEGER || this.major < 0) |
| 3255 | throw new TypeError('Invalid major version') |
| 3256 | |
| 3257 | if (this.minor > MAX_SAFE_INTEGER || this.minor < 0) |
| 3258 | throw new TypeError('Invalid minor version') |
| 3259 | |
| 3260 | if (this.patch > MAX_SAFE_INTEGER || this.patch < 0) |
| 3261 | throw new TypeError('Invalid patch version') |
| 3262 | |
| 3263 | // numberify any prerelease numeric ids |
| 3264 | if (!m[4]) |
| 3265 | this.prerelease = []; |
| 3266 | else |
| 3267 | this.prerelease = m[4].split('.').map(function(id) { |
| 3268 | if (/^[0-9]+$/.test(id)) { |
| 3269 | var num = +id; |
| 3270 | if (num >= 0 && num < MAX_SAFE_INTEGER) |
| 3271 | return num; |
| 3272 | } |
| 3273 | return id; |
| 3274 | }); |
| 3275 | |
| 3276 | this.build = m[5] ? m[5].split('.') : []; |
| 3277 | this.format(); |
| 3278 | } |
| 3279 | |
| 3280 | SemVer.prototype.format = function() { |
| 3281 | this.version = this.major + '.' + this.minor + '.' + this.patch; |