semver.js 36KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325
  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. }