|
2 | 2 | // eslint-disable-next-line es/no-object-hasown -- safe |
3 | 3 | const has = Object.hasOwn || Function.call.bind({}.hasOwnProperty); |
4 | 4 |
|
5 | | -function semver(input) { |
6 | | - if (input instanceof semver) return input; |
7 | | - // eslint-disable-next-line new-cap -- ok |
8 | | - if (!(this instanceof semver)) return new semver(input); |
9 | | - const match = /(\d+)(?:\.(\d+))?(?:\.(\d+))?/.exec(input); |
10 | | - if (!match) throw new TypeError(`Invalid version: ${ input }`); |
11 | | - const [, $major, $minor, $patch] = match; |
12 | | - this.major = +$major; |
13 | | - this.minor = $minor ? +$minor : 0; |
14 | | - this.patch = $patch ? +$patch : 0; |
| 5 | +const VERSION_PATTERN = /(\d+)(?:\.(\d+))?(?:\.(\d+))?/; |
| 6 | + |
| 7 | +class SemVer { |
| 8 | + constructor(input) { |
| 9 | + const match = VERSION_PATTERN.exec(input); |
| 10 | + if (!match) throw new TypeError(`Invalid version: ${ input }`); |
| 11 | + const [, $major, $minor, $patch] = match; |
| 12 | + this.major = +$major; |
| 13 | + this.minor = $minor ? +$minor : 0; |
| 14 | + this.patch = $patch ? +$patch : 0; |
| 15 | + } |
| 16 | + toString() { |
| 17 | + return `${ this.major }.${ this.minor }.${ this.patch }`; |
| 18 | + } |
15 | 19 | } |
16 | 20 |
|
17 | | -semver.prototype.toString = function () { |
18 | | - return `${ this.major }.${ this.minor }.${ this.patch }`; |
19 | | -}; |
| 21 | +function semver(input) { |
| 22 | + return input instanceof SemVer ? input : new SemVer(input); |
| 23 | +} |
20 | 24 |
|
21 | 25 | function compare($a, operator, $b) { |
22 | 26 | const a = semver($a); |
|
0 commit comments