semver.js 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  1. exports = module.exports = SemVer;
  2. // The debug function is excluded entirely from the minified version.
  3. /* nomin */ var debug;
  4. /* nomin */ if (typeof process === 'object' &&
  5. /* nomin */ process.env &&
  6. /* nomin */ process.env.NODE_DEBUG &&
  7. /* nomin */ /\bsemver\b/i.test(process.env.NODE_DEBUG))
  8. /* nomin */ debug = function() {
  9. /* nomin */ var args = Array.prototype.slice.call(arguments, 0);
  10. /* nomin */ args.unshift('SEMVER');
  11. /* nomin */ console.log.apply(console, args);
  12. /* nomin */ };
  13. /* nomin */ else
  14. /* nomin */ debug = function() {};
  15. // Note: this is the semver.org version of the spec that it implements
  16. // Not necessarily the package version of this code.
  17. exports.SEMVER_SPEC_VERSION = '2.0.0';
  18. var MAX_LENGTH = 256;
  19. var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991;
  20. // Max safe segment length for coercion.
  21. var MAX_SAFE_COMPONENT_LENGTH = 16;
  22. // The actual regexps go on exports.re
  23. var re = exports.re = [];
  24. var src = exports.src = [];
  25. var R = 0;
  26. // The following Regular Expressions can be used for tokenizing,
  27. // validating, and parsing SemVer version strings.
  28. // ## Numeric Identifier
  29. // A single `0`, or a non-zero digit followed by zero or more digits.
  30. var NUMERICIDENTIFIER = R++;
  31. src[NUMERICIDENTIFIER] = '0|[1-9]\\d*';
  32. var NUMERICIDENTIFIERLOOSE = R++;
  33. src[NUMERICIDENTIFIERLOOSE] = '[0-9]+';
  34. // ## Non-numeric Identifier
  35. // Zero or more digits, followed by a letter or hyphen, and then zero or
  36. // more letters, digits, or hyphens.
  37. var NONNUMERICIDENTIFIER = R++;
  38. src[NONNUMERICIDENTIFIER] = '\\d*[a-zA-Z-][a-zA-Z0-9-]*';
  39. // ## Main Version
  40. // Three dot-separated numeric identifiers.
  41. var MAINVERSION = R++;
  42. src[MAINVERSION] = '(' + src[NUMERICIDENTIFIER] + ')\\.' +
  43. '(' + src[NUMERICIDENTIFIER] + ')\\.' +
  44. '(' + src[NUMERICIDENTIFIER] + ')';
  45. var MAINVERSIONLOOSE = R++;
  46. src[MAINVERSIONLOOSE] = '(' + src[NUMERICIDENTIFIERLOOSE] + ')\\.' +
  47. '(' + src[NUMERICIDENTIFIERLOOSE] + ')\\.' +
  48. '(' + src[NUMERICIDENTIFIERLOOSE] + ')';
  49. // ## Pre-release Version Identifier
  50. // A numeric identifier, or a non-numeric identifier.
  51. var PRERELEASEIDENTIFIER = R++;
  52. src[PRERELEASEIDENTIFIER] = '(?:' + src[NUMERICIDENTIFIER] +
  53. '|' + src[NONNUMERICIDENTIFIER] + ')';
  54. var PRERELEASEIDENTIFIERLOOSE = R++;
  55. src[PRERELEASEIDENTIFIERLOOSE] = '(?:' + src[NUMERICIDENTIFIERLOOSE] +
  56. '|' + src[NONNUMERICIDENTIFIER] + ')';
  57. // ## Pre-release Version
  58. // Hyphen, followed by one or more dot-separated pre-release version
  59. // identifiers.
  60. var PRERELEASE = R++;
  61. src[PRERELEASE] = '(?:-(' + src[PRERELEASEIDENTIFIER] +
  62. '(?:\\.' + src[PRERELEASEIDENTIFIER] + ')*))';
  63. var PRERELEASELOOSE = R++;
  64. src[PRERELEASELOOSE] = '(?:-?(' + src[PRERELEASEIDENTIFIERLOOSE] +
  65. '(?:\\.' + src[PRERELEASEIDENTIFIERLOOSE] + ')*))';
  66. // ## Build Metadata Identifier
  67. // Any combination of digits, letters, or hyphens.
  68. var BUILDIDENTIFIER = R++;
  69. src[BUILDIDENTIFIER] = '[0-9A-Za-z-]+';
  70. // ## Build Metadata
  71. // Plus sign, followed by one or more period-separated build metadata
  72. // identifiers.
  73. var BUILD = R++;
  74. src[BUILD] = '(?:\\+(' + src[BUILDIDENTIFIER] +
  75. '(?:\\.' + src[BUILDIDENTIFIER] + ')*))';
  76. // ## Full Version String
  77. // A main version, followed optionally by a pre-release version and
  78. // build metadata.
  79. // Note that the only major, minor, patch, and pre-release sections of
  80. // the version string are capturing groups. The build metadata is not a
  81. // capturing group, because it should not ever be used in version
  82. // comparison.
  83. var FULL = R++;
  84. var FULLPLAIN = 'v?' + src[MAINVERSION] +
  85. src[PRERELEASE] + '?' +
  86. src[BUILD] + '?';
  87. src[FULL] = '^' + FULLPLAIN + '$';
  88. // like full, but allows v1.2.3 and =1.2.3, which people do sometimes.
  89. // also, 1.0.0alpha1 (prerelease without the hyphen) which is pretty
  90. // common in the npm registry.
  91. var LOOSEPLAIN = '[v=\\s]*' + src[MAINVERSIONLOOSE] +
  92. src[PRERELEASELOOSE] + '?' +
  93. src[BUILD] + '?';
  94. var LOOSE = R++;
  95. src[LOOSE] = '^' + LOOSEPLAIN + '$';
  96. var GTLT = R++;
  97. src[GTLT] = '((?:<|>)?=?)';
  98. // Something like "2.*" or "1.2.x".
  99. // Note that "x.x" is a valid xRange identifer, meaning "any version"
  100. // Only the first item is strictly required.
  101. var XRANGEIDENTIFIERLOOSE = R++;
  102. src[XRANGEIDENTIFIERLOOSE] = src[NUMERICIDENTIFIERLOOSE] + '|x|X|\\*';
  103. var XRANGEIDENTIFIER = R++;
  104. src[XRANGEIDENTIFIER] = src[NUMERICIDENTIFIER] + '|x|X|\\*';
  105. var XRANGEPLAIN = R++;
  106. src[XRANGEPLAIN] = '[v=\\s]*(' + src[XRANGEIDENTIFIER] + ')' +
  107. '(?:\\.(' + src[XRANGEIDENTIFIER] + ')' +
  108. '(?:\\.(' + src[XRANGEIDENTIFIER] + ')' +
  109. '(?:' + src[PRERELEASE] + ')?' +
  110. src[BUILD] + '?' +
  111. ')?)?';
  112. var XRANGEPLAINLOOSE = R++;
  113. src[XRANGEPLAINLOOSE] = '[v=\\s]*(' + src[XRANGEIDENTIFIERLOOSE] + ')' +
  114. '(?:\\.(' + src[XRANGEIDENTIFIERLOOSE] + ')' +
  115. '(?:\\.(' + src[XRANGEIDENTIFIERLOOSE] + ')' +
  116. '(?:' + src[PRERELEASELOOSE] + ')?' +
  117. src[BUILD] + '?' +
  118. ')?)?';
  119. var XRANGE = R++;
  120. src[XRANGE] = '^' + src[GTLT] + '\\s*' + src[XRANGEPLAIN] + '$';
  121. var XRANGELOOSE = R++;
  122. src[XRANGELOOSE] = '^' + src[GTLT] + '\\s*' + src[XRANGEPLAINLOOSE] + '$';
  123. // Coercion.
  124. // Extract anything that could conceivably be a part of a valid semver
  125. var COERCE = R++;
  126. src[COERCE] = '(?:^|[^\\d])' +
  127. '(\\d{1,' + MAX_SAFE_COMPONENT_LENGTH + '})' +
  128. '(?:\\.(\\d{1,' + MAX_SAFE_COMPONENT_LENGTH + '}))?' +
  129. '(?:\\.(\\d{1,' + MAX_SAFE_COMPONENT_LENGTH + '}))?' +
  130. '(?:$|[^\\d])';
  131. // Tilde ranges.
  132. // Meaning is "reasonably at or greater than"
  133. var LONETILDE = R++;
  134. src[LONETILDE] = '(?:~>?)';
  135. var TILDETRIM = R++;
  136. src[TILDETRIM] = '(\\s*)' + src[LONETILDE] + '\\s+';
  137. re[TILDETRIM] = new RegExp(src[TILDETRIM], 'g');
  138. var tildeTrimReplace = '$1~';
  139. var TILDE = R++;
  140. src[TILDE] = '^' + src[LONETILDE] + src[XRANGEPLAIN] + '$';
  141. var TILDELOOSE = R++;
  142. src[TILDELOOSE] = '^' + src[LONETILDE] + src[XRANGEPLAINLOOSE] + '$';
  143. // Caret ranges.
  144. // Meaning is "at least and backwards compatible with"
  145. var LONECARET = R++;
  146. src[LONECARET] = '(?:\\^)';
  147. var CARETTRIM = R++;
  148. src[CARETTRIM] = '(\\s*)' + src[LONECARET] + '\\s+';
  149. re[CARETTRIM] = new RegExp(src[CARETTRIM], 'g');
  150. var caretTrimReplace = '$1^';
  151. var CARET = R++;
  152. src[CARET] = '^' + src[LONECARET] + src[XRANGEPLAIN] + '$';
  153. var CARETLOOSE = R++;
  154. src[CARETLOOSE] = '^' + src[LONECARET] + src[XRANGEPLAINLOOSE] + '$';
  155. // A simple gt/lt/eq thing, or just "" to indicate "any version"
  156. var COMPARATORLOOSE = R++;
  157. src[COMPARATORLOOSE] = '^' + src[GTLT] + '\\s*(' + LOOSEPLAIN + ')$|^$';
  158. var COMPARATOR = R++;
  159. src[COMPARATOR] = '^' + src[GTLT] + '\\s*(' + FULLPLAIN + ')$|^$';
  160. // An expression to strip any whitespace between the gtlt and the thing
  161. // it modifies, so that `> 1.2.3` ==> `>1.2.3`
  162. var COMPARATORTRIM = R++;
  163. src[COMPARATORTRIM] = '(\\s*)' + src[GTLT] +
  164. '\\s*(' + LOOSEPLAIN + '|' + src[XRANGEPLAIN] + ')';
  165. // this one has to use the /g flag
  166. re[COMPARATORTRIM] = new RegExp(src[COMPARATORTRIM], 'g');
  167. var comparatorTrimReplace = '$1$2$3';
  168. // Something like `1.2.3 - 1.2.4`
  169. // Note that these all use the loose form, because they'll be
  170. // checked against either the strict or loose comparator form
  171. // later.
  172. var HYPHENRANGE = R++;
  173. src[HYPHENRANGE] = '^\\s*(' + src[XRANGEPLAIN] + ')' +
  174. '\\s+-\\s+' +
  175. '(' + src[XRANGEPLAIN] + ')' +
  176. '\\s*$';
  177. var HYPHENRANGELOOSE = R++;
  178. src[HYPHENRANGELOOSE] = '^\\s*(' + src[XRANGEPLAINLOOSE] + ')' +
  179. '\\s+-\\s+' +
  180. '(' + src[XRANGEPLAINLOOSE] + ')' +
  181. '\\s*$';
  182. // Star ranges basically just allow anything at all.
  183. var STAR = R++;
  184. src[STAR] = '(<|>)?=?\\s*\\*';
  185. // Compile to actual regexp objects.
  186. // All are flag-free, unless they were created above with a flag.
  187. for (var i = 0; i < R; i++) {
  188. debug(i, src[i]);
  189. if (!re[i])
  190. re[i] = new RegExp(src[i]);
  191. }
  192. exports.parse = parse;
  193. function parse(version, loose) {
  194. if (version instanceof SemVer)
  195. return version;
  196. if (typeof version !== 'string')
  197. return null;
  198. if (version.length > MAX_LENGTH)
  199. return null;
  200. var r = loose ? re[LOOSE] : re[FULL];
  201. if (!r.test(version))
  202. return null;
  203. try {
  204. return new SemVer(version, loose);
  205. } catch (er) {
  206. return null;
  207. }
  208. }
  209. exports.valid = valid;
  210. function valid(version, loose) {
  211. var v = parse(version, loose);
  212. return v ? v.version : null;
  213. }
  214. exports.clean = clean;
  215. function clean(version, loose) {
  216. var s = parse(version.trim().replace(/^[=v]+/, ''), loose);
  217. return s ? s.version : null;
  218. }
  219. exports.SemVer = SemVer;
  220. function SemVer(version, loose) {
  221. if (version instanceof SemVer) {
  222. if (version.loose === loose)
  223. return version;
  224. else
  225. version = version.version;
  226. } else if (typeof version !== 'string') {
  227. throw new TypeError('Invalid Version: ' + version);
  228. }
  229. if (version.length > MAX_LENGTH)
  230. throw new TypeError('version is longer than ' + MAX_LENGTH + ' characters')
  231. if (!(this instanceof SemVer))
  232. return new SemVer(version, loose);
  233. debug('SemVer', version, loose);
  234. this.loose = loose;
  235. var m = version.trim().match(loose ? re[LOOSE] : re[FULL]);
  236. if (!m)
  237. throw new TypeError('Invalid Version: ' + version);
  238. this.raw = version;
  239. // these are actually numbers
  240. this.major = +m[1];
  241. this.minor = +m[2];
  242. this.patch = +m[3];
  243. if (this.major > MAX_SAFE_INTEGER || this.major < 0)
  244. throw new TypeError('Invalid major version')
  245. if (this.minor > MAX_SAFE_INTEGER || this.minor < 0)
  246. throw new TypeError('Invalid minor version')
  247. if (this.patch > MAX_SAFE_INTEGER || this.patch < 0)
  248. throw new TypeError('Invalid patch version')
  249. // numberify any prerelease numeric ids
  250. if (!m[4])
  251. this.prerelease = [];
  252. else
  253. this.prerelease = m[4].split('.').map(function(id) {
  254. if (/^[0-9]+$/.test(id)) {
  255. var num = +id;
  256. if (num >= 0 && num < MAX_SAFE_INTEGER)
  257. return num;
  258. }
  259. return id;
  260. });
  261. this.build = m[5] ? m[5].split('.') : [];
  262. this.format();
  263. }
  264. SemVer.prototype.format = function() {
  265. this.version = this.major + '.' + this.minor + '.' + this.patch;
  266. if (this.prerelease.length)
  267. this.version += '-' + this.prerelease.join('.');
  268. return this.version;
  269. };
  270. SemVer.prototype.toString = function() {
  271. return this.version;
  272. };
  273. SemVer.prototype.compare = function(other) {
  274. debug('SemVer.compare', this.version, this.loose, other);
  275. if (!(other instanceof SemVer))
  276. other = new SemVer(other, this.loose);
  277. return this.compareMain(other) || this.comparePre(other);
  278. };
  279. SemVer.prototype.compareMain = function(other) {
  280. if (!(other instanceof SemVer))
  281. other = new SemVer(other, this.loose);
  282. return compareIdentifiers(this.major, other.major) ||
  283. compareIdentifiers(this.minor, other.minor) ||
  284. compareIdentifiers(this.patch, other.patch);
  285. };
  286. SemVer.prototype.comparePre = function(other) {
  287. if (!(other instanceof SemVer))
  288. other = new SemVer(other, this.loose);
  289. // NOT having a prerelease is > having one
  290. if (this.prerelease.length && !other.prerelease.length)
  291. return -1;
  292. else if (!this.prerelease.length && other.prerelease.length)
  293. return 1;
  294. else if (!this.prerelease.length && !other.prerelease.length)
  295. return 0;
  296. var i = 0;
  297. do {
  298. var a = this.prerelease[i];
  299. var b = other.prerelease[i];
  300. debug('prerelease compare', i, a, b);
  301. if (a === undefined && b === undefined)
  302. return 0;
  303. else if (b === undefined)
  304. return 1;
  305. else if (a === undefined)
  306. return -1;
  307. else if (a === b)
  308. continue;
  309. else
  310. return compareIdentifiers(a, b);
  311. } while (++i);
  312. };
  313. // preminor will bump the version up to the next minor release, and immediately
  314. // down to pre-release. premajor and prepatch work the same way.
  315. SemVer.prototype.inc = function(release, identifier) {
  316. switch (release) {
  317. case 'premajor':
  318. this.prerelease.length = 0;
  319. this.patch = 0;
  320. this.minor = 0;
  321. this.major++;
  322. this.inc('pre', identifier);
  323. break;
  324. case 'preminor':
  325. this.prerelease.length = 0;
  326. this.patch = 0;
  327. this.minor++;
  328. this.inc('pre', identifier);
  329. break;
  330. case 'prepatch':
  331. // If this is already a prerelease, it will bump to the next version
  332. // drop any prereleases that might already exist, since they are not
  333. // relevant at this point.
  334. this.prerelease.length = 0;
  335. this.inc('patch', identifier);
  336. this.inc('pre', identifier);
  337. break;
  338. // If the input is a non-prerelease version, this acts the same as
  339. // prepatch.
  340. case 'prerelease':
  341. if (this.prerelease.length === 0)
  342. this.inc('patch', identifier);
  343. this.inc('pre', identifier);
  344. break;
  345. case 'major':
  346. // If this is a pre-major version, bump up to the same major version.
  347. // Otherwise increment major.
  348. // 1.0.0-5 bumps to 1.0.0
  349. // 1.1.0 bumps to 2.0.0
  350. if (this.minor !== 0 || this.patch !== 0 || this.prerelease.length === 0)
  351. this.major++;
  352. this.minor = 0;
  353. this.patch = 0;
  354. this.prerelease = [];
  355. break;
  356. case 'minor':
  357. // If this is a pre-minor version, bump up to the same minor version.
  358. // Otherwise increment minor.
  359. // 1.2.0-5 bumps to 1.2.0
  360. // 1.2.1 bumps to 1.3.0
  361. if (this.patch !== 0 || this.prerelease.length === 0)
  362. this.minor++;
  363. this.patch = 0;
  364. this.prerelease = [];
  365. break;
  366. case 'patch':
  367. // If this is not a pre-release version, it will increment the patch.
  368. // If it is a pre-release it will bump up to the same patch version.
  369. // 1.2.0-5 patches to 1.2.0
  370. // 1.2.0 patches to 1.2.1
  371. if (this.prerelease.length === 0)
  372. this.patch++;
  373. this.prerelease = [];
  374. break;
  375. // This probably shouldn't be used publicly.
  376. // 1.0.0 "pre" would become 1.0.0-0 which is the wrong direction.
  377. case 'pre':
  378. if (this.prerelease.length === 0)
  379. this.prerelease = [0];
  380. else {
  381. var i = this.prerelease.length;
  382. while (--i >= 0) {
  383. if (typeof this.prerelease[i] === 'number') {
  384. this.prerelease[i]++;
  385. i = -2;
  386. }
  387. }
  388. if (i === -1) // didn't increment anything
  389. this.prerelease.push(0);
  390. }
  391. if (identifier) {
  392. // 1.2.0-beta.1 bumps to 1.2.0-beta.2,
  393. // 1.2.0-beta.fooblz or 1.2.0-beta bumps to 1.2.0-beta.0
  394. if (this.prerelease[0] === identifier) {
  395. if (isNaN(this.prerelease[1]))
  396. this.prerelease = [identifier, 0];
  397. } else
  398. this.prerelease = [identifier, 0];
  399. }
  400. break;
  401. default:
  402. throw new Error('invalid increment argument: ' + release);
  403. }
  404. this.format();
  405. this.raw = this.version;
  406. return this;
  407. };
  408. exports.inc = inc;
  409. function inc(version, release, loose, identifier) {
  410. if (typeof(loose) === 'string') {
  411. identifier = loose;
  412. loose = undefined;
  413. }
  414. try {
  415. return new SemVer(version, loose).inc(release, identifier).version;
  416. } catch (er) {
  417. return null;
  418. }
  419. }
  420. exports.diff = diff;
  421. function diff(version1, version2) {
  422. if (eq(version1, version2)) {
  423. return null;
  424. } else {
  425. var v1 = parse(version1);
  426. var v2 = parse(version2);
  427. if (v1.prerelease.length || v2.prerelease.length) {
  428. for (var key in v1) {
  429. if (key === 'major' || key === 'minor' || key === 'patch') {
  430. if (v1[key] !== v2[key]) {
  431. return 'pre'+key;
  432. }
  433. }
  434. }
  435. return 'prerelease';
  436. }
  437. for (var key in v1) {
  438. if (key === 'major' || key === 'minor' || key === 'patch') {
  439. if (v1[key] !== v2[key]) {
  440. return key;
  441. }
  442. }
  443. }
  444. }
  445. }
  446. exports.compareIdentifiers = compareIdentifiers;
  447. var numeric = /^[0-9]+$/;
  448. function compareIdentifiers(a, b) {
  449. var anum = numeric.test(a);
  450. var bnum = numeric.test(b);
  451. if (anum && bnum) {
  452. a = +a;
  453. b = +b;
  454. }
  455. return (anum && !bnum) ? -1 :
  456. (bnum && !anum) ? 1 :
  457. a < b ? -1 :
  458. a > b ? 1 :
  459. 0;
  460. }
  461. exports.rcompareIdentifiers = rcompareIdentifiers;
  462. function rcompareIdentifiers(a, b) {
  463. return compareIdentifiers(b, a);
  464. }
  465. exports.major = major;
  466. function major(a, loose) {
  467. return new SemVer(a, loose).major;
  468. }
  469. exports.minor = minor;
  470. function minor(a, loose) {
  471. return new SemVer(a, loose).minor;
  472. }
  473. exports.patch = patch;
  474. function patch(a, loose) {
  475. return new SemVer(a, loose).patch;
  476. }
  477. exports.compare = compare;
  478. function compare(a, b, loose) {
  479. return new SemVer(a, loose).compare(new SemVer(b, loose));
  480. }
  481. exports.compareLoose = compareLoose;
  482. function compareLoose(a, b) {
  483. return compare(a, b, true);
  484. }
  485. exports.rcompare = rcompare;
  486. function rcompare(a, b, loose) {
  487. return compare(b, a, loose);
  488. }
  489. exports.sort = sort;
  490. function sort(list, loose) {
  491. return list.sort(function(a, b) {
  492. return exports.compare(a, b, loose);
  493. });
  494. }
  495. exports.rsort = rsort;
  496. function rsort(list, loose) {
  497. return list.sort(function(a, b) {
  498. return exports.rcompare(a, b, loose);
  499. });
  500. }
  501. exports.gt = gt;
  502. function gt(a, b, loose) {
  503. return compare(a, b, loose) > 0;
  504. }
  505. exports.lt = lt;
  506. function lt(a, b, loose) {
  507. return compare(a, b, loose) < 0;
  508. }
  509. exports.eq = eq;
  510. function eq(a, b, loose) {
  511. return compare(a, b, loose) === 0;
  512. }
  513. exports.neq = neq;
  514. function neq(a, b, loose) {
  515. return compare(a, b, loose) !== 0;
  516. }
  517. exports.gte = gte;
  518. function gte(a, b, loose) {
  519. return compare(a, b, loose) >= 0;
  520. }
  521. exports.lte = lte;
  522. function lte(a, b, loose) {
  523. return compare(a, b, loose) <= 0;
  524. }
  525. exports.cmp = cmp;
  526. function cmp(a, op, b, loose) {
  527. var ret;
  528. switch (op) {
  529. case '===':
  530. if (typeof a === 'object') a = a.version;
  531. if (typeof b === 'object') b = b.version;
  532. ret = a === b;
  533. break;
  534. case '!==':
  535. if (typeof a === 'object') a = a.version;
  536. if (typeof b === 'object') b = b.version;
  537. ret = a !== b;
  538. break;
  539. case '': case '=': case '==': ret = eq(a, b, loose); break;
  540. case '!=': ret = neq(a, b, loose); break;
  541. case '>': ret = gt(a, b, loose); break;
  542. case '>=': ret = gte(a, b, loose); break;
  543. case '<': ret = lt(a, b, loose); break;
  544. case '<=': ret = lte(a, b, loose); break;
  545. default: throw new TypeError('Invalid operator: ' + op);
  546. }
  547. return ret;
  548. }
  549. exports.Comparator = Comparator;
  550. function Comparator(comp, loose) {
  551. if (comp instanceof Comparator) {
  552. if (comp.loose === loose)
  553. return comp;
  554. else
  555. comp = comp.value;
  556. }
  557. if (!(this instanceof Comparator))
  558. return new Comparator(comp, loose);
  559. debug('comparator', comp, loose);
  560. this.loose = loose;
  561. this.parse(comp);
  562. if (this.semver === ANY)
  563. this.value = '';
  564. else
  565. this.value = this.operator + this.semver.version;
  566. debug('comp', this);
  567. }
  568. var ANY = {};
  569. Comparator.prototype.parse = function(comp) {
  570. var r = this.loose ? re[COMPARATORLOOSE] : re[COMPARATOR];
  571. var m = comp.match(r);
  572. if (!m)
  573. throw new TypeError('Invalid comparator: ' + comp);
  574. this.operator = m[1];
  575. if (this.operator === '=')
  576. this.operator = '';
  577. // if it literally is just '>' or '' then allow anything.
  578. if (!m[2])
  579. this.semver = ANY;
  580. else
  581. this.semver = new SemVer(m[2], this.loose);
  582. };
  583. Comparator.prototype.toString = function() {
  584. return this.value;
  585. };
  586. Comparator.prototype.test = function(version) {
  587. debug('Comparator.test', version, this.loose);
  588. if (this.semver === ANY)
  589. return true;
  590. if (typeof version === 'string')
  591. version = new SemVer(version, this.loose);
  592. return cmp(version, this.operator, this.semver, this.loose);
  593. };
  594. Comparator.prototype.intersects = function(comp, loose) {
  595. if (!(comp instanceof Comparator)) {
  596. throw new TypeError('a Comparator is required');
  597. }
  598. var rangeTmp;
  599. if (this.operator === '') {
  600. rangeTmp = new Range(comp.value, loose);
  601. return satisfies(this.value, rangeTmp, loose);
  602. } else if (comp.operator === '') {
  603. rangeTmp = new Range(this.value, loose);
  604. return satisfies(comp.semver, rangeTmp, loose);
  605. }
  606. var sameDirectionIncreasing =
  607. (this.operator === '>=' || this.operator === '>') &&
  608. (comp.operator === '>=' || comp.operator === '>');
  609. var sameDirectionDecreasing =
  610. (this.operator === '<=' || this.operator === '<') &&
  611. (comp.operator === '<=' || comp.operator === '<');
  612. var sameSemVer = this.semver.version === comp.semver.version;
  613. var differentDirectionsInclusive =
  614. (this.operator === '>=' || this.operator === '<=') &&
  615. (comp.operator === '>=' || comp.operator === '<=');
  616. var oppositeDirectionsLessThan =
  617. cmp(this.semver, '<', comp.semver, loose) &&
  618. ((this.operator === '>=' || this.operator === '>') &&
  619. (comp.operator === '<=' || comp.operator === '<'));
  620. var oppositeDirectionsGreaterThan =
  621. cmp(this.semver, '>', comp.semver, loose) &&
  622. ((this.operator === '<=' || this.operator === '<') &&
  623. (comp.operator === '>=' || comp.operator === '>'));
  624. return sameDirectionIncreasing || sameDirectionDecreasing ||
  625. (sameSemVer && differentDirectionsInclusive) ||
  626. oppositeDirectionsLessThan || oppositeDirectionsGreaterThan;
  627. };
  628. exports.Range = Range;
  629. function Range(range, loose) {
  630. if (range instanceof Range) {
  631. if (range.loose === loose) {
  632. return range;
  633. } else {
  634. return new Range(range.raw, loose);
  635. }
  636. }
  637. if (range instanceof Comparator) {
  638. return new Range(range.value, loose);
  639. }
  640. if (!(this instanceof Range))
  641. return new Range(range, loose);
  642. this.loose = loose;
  643. // First, split based on boolean or ||
  644. this.raw = range;
  645. this.set = range.split(/\s*\|\|\s*/).map(function(range) {
  646. return this.parseRange(range.trim());
  647. }, this).filter(function(c) {
  648. // throw out any that are not relevant for whatever reason
  649. return c.length;
  650. });
  651. if (!this.set.length) {
  652. throw new TypeError('Invalid SemVer Range: ' + range);
  653. }
  654. this.format();
  655. }
  656. Range.prototype.format = function() {
  657. this.range = this.set.map(function(comps) {
  658. return comps.join(' ').trim();
  659. }).join('||').trim();
  660. return this.range;
  661. };
  662. Range.prototype.toString = function() {
  663. return this.range;
  664. };
  665. Range.prototype.parseRange = function(range) {
  666. var loose = this.loose;
  667. range = range.trim();
  668. debug('range', range, loose);
  669. // `1.2.3 - 1.2.4` => `>=1.2.3 <=1.2.4`
  670. var hr = loose ? re[HYPHENRANGELOOSE] : re[HYPHENRANGE];
  671. range = range.replace(hr, hyphenReplace);
  672. debug('hyphen replace', range);
  673. // `> 1.2.3 < 1.2.5` => `>1.2.3 <1.2.5`
  674. range = range.replace(re[COMPARATORTRIM], comparatorTrimReplace);
  675. debug('comparator trim', range, re[COMPARATORTRIM]);
  676. // `~ 1.2.3` => `~1.2.3`
  677. range = range.replace(re[TILDETRIM], tildeTrimReplace);
  678. // `^ 1.2.3` => `^1.2.3`
  679. range = range.replace(re[CARETTRIM], caretTrimReplace);
  680. // normalize spaces
  681. range = range.split(/\s+/).join(' ');
  682. // At this point, the range is completely trimmed and
  683. // ready to be split into comparators.
  684. var compRe = loose ? re[COMPARATORLOOSE] : re[COMPARATOR];
  685. var set = range.split(' ').map(function(comp) {
  686. return parseComparator(comp, loose);
  687. }).join(' ').split(/\s+/);
  688. if (this.loose) {
  689. // in loose mode, throw out any that are not valid comparators
  690. set = set.filter(function(comp) {
  691. return !!comp.match(compRe);
  692. });
  693. }
  694. set = set.map(function(comp) {
  695. return new Comparator(comp, loose);
  696. });
  697. return set;
  698. };
  699. Range.prototype.intersects = function(range, loose) {
  700. if (!(range instanceof Range)) {
  701. throw new TypeError('a Range is required');
  702. }
  703. return this.set.some(function(thisComparators) {
  704. return thisComparators.every(function(thisComparator) {
  705. return range.set.some(function(rangeComparators) {
  706. return rangeComparators.every(function(rangeComparator) {
  707. return thisComparator.intersects(rangeComparator, loose);
  708. });
  709. });
  710. });
  711. });
  712. };
  713. // Mostly just for testing and legacy API reasons
  714. exports.toComparators = toComparators;
  715. function toComparators(range, loose) {
  716. return new Range(range, loose).set.map(function(comp) {
  717. return comp.map(function(c) {
  718. return c.value;
  719. }).join(' ').trim().split(' ');
  720. });
  721. }
  722. // comprised of xranges, tildes, stars, and gtlt's at this point.
  723. // already replaced the hyphen ranges
  724. // turn into a set of JUST comparators.
  725. function parseComparator(comp, loose) {
  726. debug('comp', comp);
  727. comp = replaceCarets(comp, loose);
  728. debug('caret', comp);
  729. comp = replaceTildes(comp, loose);
  730. debug('tildes', comp);
  731. comp = replaceXRanges(comp, loose);
  732. debug('xrange', comp);
  733. comp = replaceStars(comp, loose);
  734. debug('stars', comp);
  735. return comp;
  736. }
  737. function isX(id) {
  738. return !id || id.toLowerCase() === 'x' || id === '*';
  739. }
  740. // ~, ~> --> * (any, kinda silly)
  741. // ~2, ~2.x, ~2.x.x, ~>2, ~>2.x ~>2.x.x --> >=2.0.0 <3.0.0
  742. // ~2.0, ~2.0.x, ~>2.0, ~>2.0.x --> >=2.0.0 <2.1.0
  743. // ~1.2, ~1.2.x, ~>1.2, ~>1.2.x --> >=1.2.0 <1.3.0
  744. // ~1.2.3, ~>1.2.3 --> >=1.2.3 <1.3.0
  745. // ~1.2.0, ~>1.2.0 --> >=1.2.0 <1.3.0
  746. function replaceTildes(comp, loose) {
  747. return comp.trim().split(/\s+/).map(function(comp) {
  748. return replaceTilde(comp, loose);
  749. }).join(' ');
  750. }
  751. function replaceTilde(comp, loose) {
  752. var r = loose ? re[TILDELOOSE] : re[TILDE];
  753. return comp.replace(r, function(_, M, m, p, pr) {
  754. debug('tilde', comp, _, M, m, p, pr);
  755. var ret;
  756. if (isX(M))
  757. ret = '';
  758. else if (isX(m))
  759. ret = '>=' + M + '.0.0 <' + (+M + 1) + '.0.0';
  760. else if (isX(p))
  761. // ~1.2 == >=1.2.0 <1.3.0
  762. ret = '>=' + M + '.' + m + '.0 <' + M + '.' + (+m + 1) + '.0';
  763. else if (pr) {
  764. debug('replaceTilde pr', pr);
  765. if (pr.charAt(0) !== '-')
  766. pr = '-' + pr;
  767. ret = '>=' + M + '.' + m + '.' + p + pr +
  768. ' <' + M + '.' + (+m + 1) + '.0';
  769. } else
  770. // ~1.2.3 == >=1.2.3 <1.3.0
  771. ret = '>=' + M + '.' + m + '.' + p +
  772. ' <' + M + '.' + (+m + 1) + '.0';
  773. debug('tilde return', ret);
  774. return ret;
  775. });
  776. }
  777. // ^ --> * (any, kinda silly)
  778. // ^2, ^2.x, ^2.x.x --> >=2.0.0 <3.0.0
  779. // ^2.0, ^2.0.x --> >=2.0.0 <3.0.0
  780. // ^1.2, ^1.2.x --> >=1.2.0 <2.0.0
  781. // ^1.2.3 --> >=1.2.3 <2.0.0
  782. // ^1.2.0 --> >=1.2.0 <2.0.0
  783. function replaceCarets(comp, loose) {
  784. return comp.trim().split(/\s+/).map(function(comp) {
  785. return replaceCaret(comp, loose);
  786. }).join(' ');
  787. }
  788. function replaceCaret(comp, loose) {
  789. debug('caret', comp, loose);
  790. var r = loose ? re[CARETLOOSE] : re[CARET];
  791. return comp.replace(r, function(_, M, m, p, pr) {
  792. debug('caret', comp, _, M, m, p, pr);
  793. var ret;
  794. if (isX(M))
  795. ret = '';
  796. else if (isX(m))
  797. ret = '>=' + M + '.0.0 <' + (+M + 1) + '.0.0';
  798. else if (isX(p)) {
  799. if (M === '0')
  800. ret = '>=' + M + '.' + m + '.0 <' + M + '.' + (+m + 1) + '.0';
  801. else
  802. ret = '>=' + M + '.' + m + '.0 <' + (+M + 1) + '.0.0';
  803. } else if (pr) {
  804. debug('replaceCaret pr', pr);
  805. if (pr.charAt(0) !== '-')
  806. pr = '-' + pr;
  807. if (M === '0') {
  808. if (m === '0')
  809. ret = '>=' + M + '.' + m + '.' + p + pr +
  810. ' <' + M + '.' + m + '.' + (+p + 1);
  811. else
  812. ret = '>=' + M + '.' + m + '.' + p + pr +
  813. ' <' + M + '.' + (+m + 1) + '.0';
  814. } else
  815. ret = '>=' + M + '.' + m + '.' + p + pr +
  816. ' <' + (+M + 1) + '.0.0';
  817. } else {
  818. debug('no pr');
  819. if (M === '0') {
  820. if (m === '0')
  821. ret = '>=' + M + '.' + m + '.' + p +
  822. ' <' + M + '.' + m + '.' + (+p + 1);
  823. else
  824. ret = '>=' + M + '.' + m + '.' + p +
  825. ' <' + M + '.' + (+m + 1) + '.0';
  826. } else
  827. ret = '>=' + M + '.' + m + '.' + p +
  828. ' <' + (+M + 1) + '.0.0';
  829. }
  830. debug('caret return', ret);
  831. return ret;
  832. });
  833. }
  834. function replaceXRanges(comp, loose) {
  835. debug('replaceXRanges', comp, loose);
  836. return comp.split(/\s+/).map(function(comp) {
  837. return replaceXRange(comp, loose);
  838. }).join(' ');
  839. }
  840. function replaceXRange(comp, loose) {
  841. comp = comp.trim();
  842. var r = loose ? re[XRANGELOOSE] : re[XRANGE];
  843. return comp.replace(r, function(ret, gtlt, M, m, p, pr) {
  844. debug('xRange', comp, ret, gtlt, M, m, p, pr);
  845. var xM = isX(M);
  846. var xm = xM || isX(m);
  847. var xp = xm || isX(p);
  848. var anyX = xp;
  849. if (gtlt === '=' && anyX)
  850. gtlt = '';
  851. if (xM) {
  852. if (gtlt === '>' || gtlt === '<') {
  853. // nothing is allowed
  854. ret = '<0.0.0';
  855. } else {
  856. // nothing is forbidden
  857. ret = '*';
  858. }
  859. } else if (gtlt && anyX) {
  860. // replace X with 0
  861. if (xm)
  862. m = 0;
  863. if (xp)
  864. p = 0;
  865. if (gtlt === '>') {
  866. // >1 => >=2.0.0
  867. // >1.2 => >=1.3.0
  868. // >1.2.3 => >= 1.2.4
  869. gtlt = '>=';
  870. if (xm) {
  871. M = +M + 1;
  872. m = 0;
  873. p = 0;
  874. } else if (xp) {
  875. m = +m + 1;
  876. p = 0;
  877. }
  878. } else if (gtlt === '<=') {
  879. // <=0.7.x is actually <0.8.0, since any 0.7.x should
  880. // pass. Similarly, <=7.x is actually <8.0.0, etc.
  881. gtlt = '<';
  882. if (xm)
  883. M = +M + 1;
  884. else
  885. m = +m + 1;
  886. }
  887. ret = gtlt + M + '.' + m + '.' + p;
  888. } else if (xm) {
  889. ret = '>=' + M + '.0.0 <' + (+M + 1) + '.0.0';
  890. } else if (xp) {
  891. ret = '>=' + M + '.' + m + '.0 <' + M + '.' + (+m + 1) + '.0';
  892. }
  893. debug('xRange return', ret);
  894. return ret;
  895. });
  896. }
  897. // Because * is AND-ed with everything else in the comparator,
  898. // and '' means "any version", just remove the *s entirely.
  899. function replaceStars(comp, loose) {
  900. debug('replaceStars', comp, loose);
  901. // Looseness is ignored here. star is always as loose as it gets!
  902. return comp.trim().replace(re[STAR], '');
  903. }
  904. // This function is passed to string.replace(re[HYPHENRANGE])
  905. // M, m, patch, prerelease, build
  906. // 1.2 - 3.4.5 => >=1.2.0 <=3.4.5
  907. // 1.2.3 - 3.4 => >=1.2.0 <3.5.0 Any 3.4.x will do
  908. // 1.2 - 3.4 => >=1.2.0 <3.5.0
  909. function hyphenReplace($0,
  910. from, fM, fm, fp, fpr, fb,
  911. to, tM, tm, tp, tpr, tb) {
  912. if (isX(fM))
  913. from = '';
  914. else if (isX(fm))
  915. from = '>=' + fM + '.0.0';
  916. else if (isX(fp))
  917. from = '>=' + fM + '.' + fm + '.0';
  918. else
  919. from = '>=' + from;
  920. if (isX(tM))
  921. to = '';
  922. else if (isX(tm))
  923. to = '<' + (+tM + 1) + '.0.0';
  924. else if (isX(tp))
  925. to = '<' + tM + '.' + (+tm + 1) + '.0';
  926. else if (tpr)
  927. to = '<=' + tM + '.' + tm + '.' + tp + '-' + tpr;
  928. else
  929. to = '<=' + to;
  930. return (from + ' ' + to).trim();
  931. }
  932. // if ANY of the sets match ALL of its comparators, then pass
  933. Range.prototype.test = function(version) {
  934. if (!version)
  935. return false;
  936. if (typeof version === 'string')
  937. version = new SemVer(version, this.loose);
  938. for (var i = 0; i < this.set.length; i++) {
  939. if (testSet(this.set[i], version))
  940. return true;
  941. }
  942. return false;
  943. };
  944. function testSet(set, version) {
  945. for (var i = 0; i < set.length; i++) {
  946. if (!set[i].test(version))
  947. return false;
  948. }
  949. if (version.prerelease.length) {
  950. // Find the set of versions that are allowed to have prereleases
  951. // For example, ^1.2.3-pr.1 desugars to >=1.2.3-pr.1 <2.0.0
  952. // That should allow `1.2.3-pr.2` to pass.
  953. // However, `1.2.4-alpha.notready` should NOT be allowed,
  954. // even though it's within the range set by the comparators.
  955. for (var i = 0; i < set.length; i++) {
  956. debug(set[i].semver);
  957. if (set[i].semver === ANY)
  958. continue;
  959. if (set[i].semver.prerelease.length > 0) {
  960. var allowed = set[i].semver;
  961. if (allowed.major === version.major &&
  962. allowed.minor === version.minor &&
  963. allowed.patch === version.patch)
  964. return true;
  965. }
  966. }
  967. // Version has a -pre, but it's not one of the ones we like.
  968. return false;
  969. }
  970. return true;
  971. }
  972. exports.satisfies = satisfies;
  973. function satisfies(version, range, loose) {
  974. try {
  975. range = new Range(range, loose);
  976. } catch (er) {
  977. return false;
  978. }
  979. return range.test(version);
  980. }
  981. exports.maxSatisfying = maxSatisfying;
  982. function maxSatisfying(versions, range, loose) {
  983. var max = null;
  984. var maxSV = null;
  985. try {
  986. var rangeObj = new Range(range, loose);
  987. } catch (er) {
  988. return null;
  989. }
  990. versions.forEach(function (v) {
  991. if (rangeObj.test(v)) { // satisfies(v, range, loose)
  992. if (!max || maxSV.compare(v) === -1) { // compare(max, v, true)
  993. max = v;
  994. maxSV = new SemVer(max, loose);
  995. }
  996. }
  997. })
  998. return max;
  999. }
  1000. exports.minSatisfying = minSatisfying;
  1001. function minSatisfying(versions, range, loose) {
  1002. var min = null;
  1003. var minSV = null;
  1004. try {
  1005. var rangeObj = new Range(range, loose);
  1006. } catch (er) {
  1007. return null;
  1008. }
  1009. versions.forEach(function (v) {
  1010. if (rangeObj.test(v)) { // satisfies(v, range, loose)
  1011. if (!min || minSV.compare(v) === 1) { // compare(min, v, true)
  1012. min = v;
  1013. minSV = new SemVer(min, loose);
  1014. }
  1015. }
  1016. })
  1017. return min;
  1018. }
  1019. exports.validRange = validRange;
  1020. function validRange(range, loose) {
  1021. try {
  1022. // Return '*' instead of '' so that truthiness works.
  1023. // This will throw if it's invalid anyway
  1024. return new Range(range, loose).range || '*';
  1025. } catch (er) {
  1026. return null;
  1027. }
  1028. }
  1029. // Determine if version is less than all the versions possible in the range
  1030. exports.ltr = ltr;
  1031. function ltr(version, range, loose) {
  1032. return outside(version, range, '<', loose);
  1033. }
  1034. // Determine if version is greater than all the versions possible in the range.
  1035. exports.gtr = gtr;
  1036. function gtr(version, range, loose) {
  1037. return outside(version, range, '>', loose);
  1038. }
  1039. exports.outside = outside;
  1040. function outside(version, range, hilo, loose) {
  1041. version = new SemVer(version, loose);
  1042. range = new Range(range, loose);
  1043. var gtfn, ltefn, ltfn, comp, ecomp;
  1044. switch (hilo) {
  1045. case '>':
  1046. gtfn = gt;
  1047. ltefn = lte;
  1048. ltfn = lt;
  1049. comp = '>';
  1050. ecomp = '>=';
  1051. break;
  1052. case '<':
  1053. gtfn = lt;
  1054. ltefn = gte;
  1055. ltfn = gt;
  1056. comp = '<';
  1057. ecomp = '<=';
  1058. break;
  1059. default:
  1060. throw new TypeError('Must provide a hilo val of "<" or ">"');
  1061. }
  1062. // If it satisifes the range it is not outside
  1063. if (satisfies(version, range, loose)) {
  1064. return false;
  1065. }
  1066. // From now on, variable terms are as if we're in "gtr" mode.
  1067. // but note that everything is flipped for the "ltr" function.
  1068. for (var i = 0; i < range.set.length; ++i) {
  1069. var comparators = range.set[i];
  1070. var high = null;
  1071. var low = null;
  1072. comparators.forEach(function(comparator) {
  1073. if (comparator.semver === ANY) {
  1074. comparator = new Comparator('>=0.0.0')
  1075. }
  1076. high = high || comparator;
  1077. low = low || comparator;
  1078. if (gtfn(comparator.semver, high.semver, loose)) {
  1079. high = comparator;
  1080. } else if (ltfn(comparator.semver, low.semver, loose)) {
  1081. low = comparator;
  1082. }
  1083. });
  1084. // If the edge version comparator has a operator then our version
  1085. // isn't outside it
  1086. if (high.operator === comp || high.operator === ecomp) {
  1087. return false;
  1088. }
  1089. // If the lowest version comparator has an operator and our version
  1090. // is less than it then it isn't higher than the range
  1091. if ((!low.operator || low.operator === comp) &&
  1092. ltefn(version, low.semver)) {
  1093. return false;
  1094. } else if (low.operator === ecomp && ltfn(version, low.semver)) {
  1095. return false;
  1096. }
  1097. }
  1098. return true;
  1099. }
  1100. exports.prerelease = prerelease;
  1101. function prerelease(version, loose) {
  1102. var parsed = parse(version, loose);
  1103. return (parsed && parsed.prerelease.length) ? parsed.prerelease : null;
  1104. }
  1105. exports.intersects = intersects;
  1106. function intersects(r1, r2, loose) {
  1107. r1 = new Range(r1, loose)
  1108. r2 = new Range(r2, loose)
  1109. return r1.intersects(r2)
  1110. }
  1111. exports.coerce = coerce;
  1112. function coerce(version) {
  1113. if (version instanceof SemVer)
  1114. return version;
  1115. if (typeof version !== 'string')
  1116. return null;
  1117. var match = version.match(re[COERCE]);
  1118. if (match == null)
  1119. return null;
  1120. return parse((match[1] || '0') + '.' + (match[2] || '0') + '.' + (match[3] || '0'));
  1121. }