source: trip-planner-front/node_modules/semver/functions/coerce.js@ 59329aa

Last change on this file since 59329aa was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

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