coerce.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 'use strict'
  2. const SemVer = require('../classes/semver')
  3. const parse = require('./parse')
  4. const { safeRe: re, t } = require('../internal/re')
  5. const coerce = (version, options) => {
  6. if (version instanceof SemVer) {
  7. return version
  8. }
  9. if (typeof version === 'number') {
  10. version = String(version)
  11. }
  12. if (typeof version !== 'string') {
  13. return null
  14. }
  15. options = options || {}
  16. let match = null
  17. if (!options.rtl) {
  18. match = version.match(options.includePrerelease ? re[t.COERCEFULL] : re[t.COERCE])
  19. } else {
  20. // Find the right-most coercible string that does not share
  21. // a terminus with a more left-ward coercible string.
  22. // Eg, '1.2.3.4' wants to coerce '2.3.4', not '3.4' or '4'
  23. // With includePrerelease option set, '1.2.3.4-rc' wants to coerce '2.3.4-rc', not '2.3.4'
  24. //
  25. // Walk through the string checking with a /g regexp
  26. // Manually set the index so as to pick up overlapping matches.
  27. // Stop when we get a match that ends at the string end, since no
  28. // coercible string can be more right-ward without the same terminus.
  29. const coerceRtlRegex = options.includePrerelease ? re[t.COERCERTLFULL] : re[t.COERCERTL]
  30. let next
  31. while ((next = coerceRtlRegex.exec(version)) &&
  32. (!match || match.index + match[0].length !== version.length)
  33. ) {
  34. if (!match ||
  35. next.index + next[0].length !== match.index + match[0].length) {
  36. match = next
  37. }
  38. coerceRtlRegex.lastIndex = next.index + next[1].length + next[2].length
  39. }
  40. // leave it in a clean state
  41. coerceRtlRegex.lastIndex = -1
  42. }
  43. if (match === null) {
  44. return null
  45. }
  46. const major = match[2]
  47. const minor = match[3] || '0'
  48. const patch = match[4] || '0'
  49. const prerelease = options.includePrerelease && match[5] ? `-${match[5]}` : ''
  50. const build = options.includePrerelease && match[6] ? `+${match[6]}` : ''
  51. return parse(`${major}.${minor}.${patch}${prerelease}${build}`, options)
  52. }
  53. module.exports = coerce