Skip to content

Commit

Permalink
Change option name to includePrerelease
Browse files Browse the repository at this point in the history
  • Loading branch information
madtisa committed Jan 4, 2024
1 parent 86535f0 commit 5f425d7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ 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 the `coerce` result will contain
If the `options.includePrerelease` flag is set, then the `coerce` result will contain
prerelease and build parts of a version. For example, `1.2.3.4-rc.1+rev.2`
will preserve prerelease `rc.1` and build `rev.2` in the result.

Expand Down
10 changes: 5 additions & 5 deletions functions/coerce.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ const coerce = (version, options) => {

let match = null
if (!options.rtl) {
match = version.match(options.full ? re[t.COERCEFULL] : re[t.COERCE])
match = version.match(options.includePrerelease ? 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'
// With includePrerelease option set, '1.2.3.4-pre.1.2' wants to coerce '2.3.4-pre.1.2', not '1.2'

Check failure on line 27 in functions/coerce.js

View workflow job for this annotation

GitHub Actions / Lint

This line has a length of 102. Maximum allowed is 100
//
// 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.
const coerceRtlRegex = options.full ? re[t.COERCERTLFULL] : re[t.COERCERTL]
const coerceRtlRegex = options.includePrerelease ? re[t.COERCERTLFULL] : re[t.COERCERTL]
let next
while ((next = coerceRtlRegex.exec(version)) &&
(!match || match.index + match[0].length !== version.length)
Expand All @@ -52,8 +52,8 @@ const coerce = (version, 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]}` : ''
const prerelease = options.includePrerelease && match[5] ? `-${match[5]}` : ''
const build = options.includePrerelease && match[6] ? `+${match[6]}` : ''

return parse(`${major}.${minor}.${patch}${prerelease}${build}`, options)
}
Expand Down
50 changes: 25 additions & 25 deletions test/functions/coerce.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,33 +111,33 @@ test('coerce tests', (t) => {
['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-rc.5', '1.0.0-rc.5', { includePrerelease: true }, true],
['1.2-rc.5', '1.2.0-rc.5', { includePrerelease: true }, true],
['1.2.3-rc.5', '1.2.3-rc.5', { includePrerelease: true }, true],
['1.2.3-rc.5/a', '1.2.3-rc.5', { includePrerelease: true }, true],
['1.2.3.4-rc.5', '1.2.3', { includePrerelease: true }, true],
['1.2.3.4+rev.6', '1.2.3', { includePrerelease: 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+rev.6', '1.0.0+rev.6', { includePrerelease: true }, true],
['1.2+rev.6', '1.2.0+rev.6', { includePrerelease: true }, true],
['1.2.3+rev.6', '1.2.3+rev.6', { includePrerelease: true }, true],
['1.2.3+rev.6/a', '1.2.3+rev.6', { includePrerelease: true }, true],
['1.2.3.4-rc.5', '1.2.3', { includePrerelease: true }, true],
['1.2.3.4+rev.6', '1.2.3', { includePrerelease: 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-rc.5+rev.6', '1.0.0-rc.5+rev.6', { includePrerelease: true }, true],
['1.2-rc.5+rev.6', '1.2.0-rc.5+rev.6', { includePrerelease: true }, true],
['1.2.3-rc.5+rev.6', '1.2.3-rc.5+rev.6', { includePrerelease: true }, true],
['1.2.3-rc.5+rev.6/a', '1.2.3-rc.5+rev.6', { includePrerelease: 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],
['1.2-rc.5+rev.6', '1.2.0-rc.5+rev.6', { rtl: true, includePrerelease: true }, true],
['1.2.3-rc.5+rev.6', '1.2.3-rc.5+rev.6', { rtl: true, includePrerelease: true }, true],
['1.2.3.4-rc.5+rev.6', '2.3.4-rc.5+rev.6', { rtl: true, includePrerelease: true }, true],
['1.2.3.4-rc.5', '2.3.4-rc.5', { rtl: true, includePrerelease: true }, true],
['1.2.3.4+rev.6', '2.3.4+rev.6', { rtl: true, includePrerelease: true }, true],
['1.2.3.4-rc.5+rev.6/7', '7.0.0', { rtl: true, includePrerelease: true }, true],
['1.2.3.4-rc/7.5+rev.6', '7.5.0+rev.6', { rtl: true, includePrerelease: true }, true],
['1.2.3.4/7-rc.5+rev.6', '7.0.0-rc.5+rev.6', { rtl: true, includePrerelease: true }, true],
]
coerceToValid.forEach(([input, expected, options, shouldParse]) => {
const coerceExpression = `coerce(${input}, ${JSON.stringify(options)})`
Expand All @@ -161,7 +161,7 @@ test('coerce tests', (t) => {
})

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('42.6.7-alpha+rev.1', { includePrerelease: true })), '42.6.7-alpha')
t.same(valid(coerce('v2')), '2.0.0')

t.end()
Expand Down

0 comments on commit 5f425d7

Please sign in to comment.