| 6849 | |
| 6850 | exports.coerce = coerce |
| 6851 | function coerce (version, options) { |
| 6852 | if (version instanceof SemVer) { |
| 6853 | return version |
| 6854 | } |
| 6855 | |
| 6856 | if (typeof version === 'number') { |
| 6857 | version = String(version) |
| 6858 | } |
| 6859 | |
| 6860 | if (typeof version !== 'string') { |
| 6861 | return null |
| 6862 | } |
| 6863 | |
| 6864 | options = options || {} |
| 6865 | |
| 6866 | var match = null |
| 6867 | if (!options.rtl) { |
| 6868 | match = version.match(safeRe[t.COERCE]) |
| 6869 | } else { |
| 6870 | // Find the right-most coercible string that does not share |
| 6871 | // a terminus with a more left-ward coercible string. |
| 6872 | // Eg, '1.2.3.4' wants to coerce '2.3.4', not '3.4' or '4' |
| 6873 | // |
| 6874 | // Walk through the string checking with a /g regexp |
| 6875 | // Manually set the index so as to pick up overlapping matches. |
| 6876 | // Stop when we get a match that ends at the string end, since no |
| 6877 | // coercible string can be more right-ward without the same terminus. |
| 6878 | var next |
| 6879 | while ((next = safeRe[t.COERCERTL].exec(version)) && |
| 6880 | (!match || match.index + match[0].length !== version.length) |
| 6881 | ) { |
| 6882 | if (!match || |
| 6883 | next.index + next[0].length !== match.index + match[0].length) { |
| 6884 | match = next |
| 6885 | } |
| 6886 | safeRe[t.COERCERTL].lastIndex = next.index + next[1].length + next[2].length |
| 6887 | } |
| 6888 | // leave it in a clean state |
| 6889 | safeRe[t.COERCERTL].lastIndex = -1 |
| 6890 | } |
| 6891 | |
| 6892 | if (match === null) { |
| 6893 | return null |
| 6894 | } |
| 6895 | |
| 6896 | return parse(match[2] + |
| 6897 | '.' + (match[3] || '0') + |
| 6898 | '.' + (match[4] || '0'), options) |
| 6899 | } |
| 6900 | |
| 6901 | |
| 6902 | /***/ }), |