1
0
Files
setup-deno/node_modules/semver/functions/coerce.js
T

61 lines
1.9 KiB
JavaScript
Raw Normal View History

2021-04-10 02:16:12 +02:00
const SemVer = require('../classes/semver')
const parse = require('./parse')
2024-09-13 17:15:41 +02:00
const { safeRe: re, t } = require('../internal/re')
2021-04-10 02:16:12 +02:00
const coerce = (version, options) => {
if (version instanceof SemVer) {
return version
}
if (typeof version === 'number') {
version = String(version)
}
if (typeof version !== 'string') {
return null
}
options = options || {}
let match = null
if (!options.rtl) {
2024-09-13 17:15:41 +02:00
match = version.match(options.includePrerelease ? re[t.COERCEFULL] : re[t.COERCE])
2021-04-10 02:16:12 +02:00
} else {
// Find the right-most coercible string that does not share
// a terminus with a more left-ward coercible string.
// Eg, '1.2.3.4' wants to coerce '2.3.4', not '3.4' or '4'
2024-09-13 17:15:41 +02:00
// With includePrerelease option set, '1.2.3.4-rc' wants to coerce '2.3.4-rc', not '2.3.4'
2021-04-10 02:16:12 +02:00
//
// Walk through the string checking with a /g regexp
// Manually set the index so as to pick up overlapping matches.
// Stop when we get a match that ends at the string end, since no
// coercible string can be more right-ward without the same terminus.
2024-09-13 17:15:41 +02:00
const coerceRtlRegex = options.includePrerelease ? re[t.COERCERTLFULL] : re[t.COERCERTL]
2021-04-10 02:16:12 +02:00
let next
2024-09-13 17:15:41 +02:00
while ((next = coerceRtlRegex.exec(version)) &&
2021-04-10 02:16:12 +02:00
(!match || match.index + match[0].length !== version.length)
) {
if (!match ||
next.index + next[0].length !== match.index + match[0].length) {
match = next
}
2024-09-13 17:15:41 +02:00
coerceRtlRegex.lastIndex = next.index + next[1].length + next[2].length
2021-04-10 02:16:12 +02:00
}
// leave it in a clean state
2024-09-13 17:15:41 +02:00
coerceRtlRegex.lastIndex = -1
2021-04-10 02:16:12 +02:00
}
2022-10-17 12:03:20 +02:00
if (match === null) {
2021-04-10 02:16:12 +02:00
return null
2022-10-17 12:03:20 +02:00
}
2021-04-10 02:16:12 +02:00
2024-09-13 17:15:41 +02:00
const major = match[2]
const minor = match[3] || '0'
const patch = match[4] || '0'
const prerelease = options.includePrerelease && match[5] ? `-${match[5]}` : ''
const build = options.includePrerelease && match[6] ? `+${match[6]}` : ''
return parse(`${major}.${minor}.${patch}${prerelease}${build}`, options)
2021-04-10 02:16:12 +02:00
}
module.exports = coerce