Skip to content

Commit

Permalink
feat: preserve pre-release and build parts of a version on coerce (#592)
Browse files Browse the repository at this point in the history
Introduces the new coerce option `full`, if set, allowing to preserve
pre-release and build parts of a version.
  • Loading branch information
madtisa committed Dec 21, 2023
1 parent 816c7b2 commit 7c9a156
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 9 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,10 @@ tuple. For example, `1.2.3.4` will return `2.3.4` in rtl mode, not
`4.0.0`. `1.2.3/4` will return `4.0.0`, because the `4` is not a part of
any other overlapping SemVer tuple.

If the `options.full` flag is set, then `coerce` will include prerelease
and build into resulted version. For example, `1.2.3.4-pre.1+build.2`
will return `2.3.4-pre.1+build.2`.

### Clean

* `clean(version)`: Clean a string to be a valid semver if possible
Expand Down
20 changes: 14 additions & 6 deletions functions/coerce.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,42 @@ const coerce = (version, options) => {

let match = null
if (!options.rtl) {
match = version.match(re[t.COERCE])
match = version.match(options.full ? re[t.COERCEFULL] : re[t.COERCE])
} 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'
// With full option set, '1.2.3.4-pre.1.2' wants to coerce '2.3.4-pre.1.2', not '1.2'
//
// Walk through the string checking with a /g regexp
// Manually set the index so as to pick up overlapping matches.
// 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.
const coerceRtlRegex = options.full ? re[t.COERCERTLFULL] : re[t.COERCERTL]
let next
while ((next = re[t.COERCERTL].exec(version)) &&
while ((next = coerceRtlRegex.exec(version)) &&
(!match || match.index + match[0].length !== version.length)
) {
if (!match ||
next.index + next[0].length !== match.index + match[0].length) {
match = next
}
re[t.COERCERTL].lastIndex = next.index + next[1].length + next[2].length
coerceRtlRegex.lastIndex = next.index + next[1].length + next[2].length
}
// leave it in a clean state
re[t.COERCERTL].lastIndex = -1
coerceRtlRegex.lastIndex = -1
}

if (match === null) {
return null
}

return parse(`${match[2]}.${match[3] || '0'}.${match[4] || '0'}`, options)
const major = match[2]
const minor = match[3] || '0'
const patch = match[4] || '0'
const prerelease = options.full && match[5] ? `-${match[5]}` : ''
const build = options.full && match[6] ? `+${match[6]}` : ''

return parse(`${major}.${minor}.${patch}${prerelease}${build}`, options)
}
module.exports = coerce
8 changes: 8 additions & 0 deletions internal/re.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,15 @@ createToken('COERCE', `${'(^|[^\\d])' +
`(?:\\.(\\d{1,${MAX_SAFE_COMPONENT_LENGTH}}))?` +
`(?:\\.(\\d{1,${MAX_SAFE_COMPONENT_LENGTH}}))?` +
`(?:$|[^\\d])`)
createToken('COERCEFULL', `${'(^|[^\\d])' +
'(\\d{1,'}${MAX_SAFE_COMPONENT_LENGTH}})` +
`(?:\\.(\\d{1,${MAX_SAFE_COMPONENT_LENGTH}}))?` +
`(?:\\.(\\d{1,${MAX_SAFE_COMPONENT_LENGTH}}))?` +
`(?:${src[t.PRERELEASE]})?` +
`(?:${src[t.BUILD]})?` +
`(?:$|[^\\d])`)
createToken('COERCERTL', src[t.COERCE], true)
createToken('COERCERTLFULL', src[t.COERCEFULL], true)

// Tilde ranges.
// Meaning is "reasonably at or greater than"
Expand Down
51 changes: 48 additions & 3 deletions test/functions/coerce.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,58 @@ test('coerce tests', (t) => {
['1.2.3/6', '6.0.0', { rtl: true }],
['1.2.3.4', '2.3.4', { rtl: true }],
['1.2.3.4xyz', '2.3.4', { rtl: true }],

['1-rc.5', '1.0.0-rc.5', { full: true }, true],
['1.2-rc.5', '1.2.0-rc.5', { full: true }, true],
['1.2.3-rc.5', '1.2.3-rc.5', { full: true }, true],
['1.2.3-rc.5/a', '1.2.3-rc.5', { full: true }, true],
['1.2.3.4-rc.5', '1.2.3', { full: true }, true],
['1.2.3.4+rev.6', '1.2.3', { full: true }, true],

['1+rev.6', '1.0.0+rev.6', { full: true }, true],
['1.2+rev.6', '1.2.0+rev.6', { full: true }, true],
['1.2.3+rev.6', '1.2.3+rev.6', { full: true }, true],
['1.2.3+rev.6/a', '1.2.3+rev.6', { full: true }, true],
['1.2.3.4-rc.5', '1.2.3', { full: true }, true],
['1.2.3.4+rev.6', '1.2.3', { full: true }, true],

['1-rc.5+rev.6', '1.0.0-rc.5+rev.6', { full: true }, true],
['1.2-rc.5+rev.6', '1.2.0-rc.5+rev.6', { full: true }, true],
['1.2.3-rc.5+rev.6', '1.2.3-rc.5+rev.6', { full: true }, true],
['1.2.3-rc.5+rev.6/a', '1.2.3-rc.5+rev.6', { full: true }, true],

['1.2-rc.5+rev.6', '1.2.0-rc.5+rev.6', { rtl: true, full: true }, true],
['1.2.3-rc.5+rev.6', '1.2.3-rc.5+rev.6', { rtl: true, full: true }, true],
['1.2.3.4-rc.5+rev.6', '2.3.4-rc.5+rev.6', { rtl: true, full: true }, true],
['1.2.3.4-rc.5', '2.3.4-rc.5', { rtl: true, full: true }, true],
['1.2.3.4+rev.6', '2.3.4+rev.6', { rtl: true, full: true }, true],
['1.2.3.4-rc.5+rev.6/7', '7.0.0', { rtl: true, full: true }, true],
['1.2.3.4-rc/7.5+rev.6', '7.5.0+rev.6', { rtl: true, full: true }, true],
['1.2.3.4/7-rc.5+rev.6', '7.0.0-rc.5+rev.6', { rtl: true, full: true }, true],
]
coerceToValid.forEach(([input, expected, options]) => {
const msg = `coerce(${input}) should become ${expected}`
t.same((coerce(input, options) || {}).version, expected, msg)
coerceToValid.forEach(([input, expected, options, shouldParse]) => {
const coerceExpression = `coerce(${input}, ${JSON.stringify(options)})`
const coercedVersion = coerce(input, options) || {}
let expectedText = expected

if (shouldParse) {
const expectedVersion = parse(expected)
expectedText = expectedVersion.version
t.equal(
expectedVersion.compare(coercedVersion),
0,
`${coerceExpression} should be equal to ${expectedVersion}`
)
}
t.same(
coercedVersion.version,
expectedText,
`${coerceExpression} should become ${expectedText}`
)
})

t.same(valid(coerce('42.6.7.9.3-alpha')), '42.6.7')
t.same(valid(coerce('42.6.7-alpha+rev.1', { full: true })), '42.6.7-alpha')
t.same(valid(coerce('v2')), '2.0.0')

t.end()
Expand Down

0 comments on commit 7c9a156

Please sign in to comment.