|
| 1 | +import coerce from 'semver/functions/coerce'; |
| 2 | +import gte from 'semver/functions/gte'; |
| 3 | +import lte from 'semver/functions/lte'; |
| 4 | +import valid from 'semver/functions/valid'; |
| 5 | + |
| 6 | +/** |
| 7 | + * @param {string | null} version1 |
| 8 | + * @param {string | null} version2 |
| 9 | + * @returns {-1|0|1} |
| 10 | + */ |
| 11 | +export function greatestVersionAsc(version1, version2) { |
| 12 | + const t1Version = validateVersion(version1); |
| 13 | + const t2Version = validateVersion(version2); |
| 14 | + if (t1Version !== null && t2Version !== null) { |
| 15 | + const t1VersionLt = lte(t1Version, t2Version); |
| 16 | + return t1VersionLt ? -1 : 1; |
| 17 | + } |
| 18 | + return 0; |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * @param {string | null} version1 |
| 23 | + * @param {string | null} version2 |
| 24 | + * @returns {-1|0|1} |
| 25 | + */ |
| 26 | +export function greatestVersionDesc(version1, version2) { |
| 27 | + const t1Version = validateVersion(version1); |
| 28 | + const t2Version = validateVersion(version2); |
| 29 | + if (t1Version !== null && t2Version !== null) { |
| 30 | + const t1VersionGt = gte(t1Version, t2Version); |
| 31 | + return t1VersionGt ? -1 : 1; |
| 32 | + } |
| 33 | + return 0; |
| 34 | +} |
| 35 | + |
| 36 | +const semverValidationOptions = { |
| 37 | + loose: true, |
| 38 | + includePrerelease: true |
| 39 | +}; |
| 40 | + |
| 41 | +/** |
| 42 | + * @param {string|null} version |
| 43 | + * @returns {string | null} |
| 44 | + */ |
| 45 | +function validateVersion(version) { |
| 46 | + return ( |
| 47 | + valid(version, semverValidationOptions) || |
| 48 | + valid(coerce(version), semverValidationOptions) || |
| 49 | + null |
| 50 | + ); |
| 51 | +} |
0 commit comments