semver.js 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352
  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, options) {
  194. if (!options || typeof options !== 'object')
  195. options = { loose: !!options, includePrerelease: false }
  196. if (version instanceof SemVer)
  197. return version;
  198. if (typeof version !== 'string')
  199. return null;
  200. if (version.length > MAX_LENGTH)
  201. return null;
  202. var r = options.loose ? re[LOOSE] : re[FULL];
  203. if (!r.test(version))
  204. return null;
  205. try {
  206. return new SemVer(version, options);
  207. } catch (er) {
  208. return null;
  209. }
  210. }
  211. exports.valid = valid;
  212. function valid(version, options) {
  213. var v = parse(version, options);
  214. return v ? v.version : null;
  215. }
  216. exports.clean = clean;
  217. function clean(version, options) {
  218. var s = parse(version.trim().replace(/^[=v]+/, ''), options);
  219. return s ? s.version : null;
  220. }
  221. exports.SemVer = SemVer;
  222. function SemVer(version, options) {
  223. if (!options || typeof options !== 'object')
  224. options = { loose: !!options, includePrerelease: false }
  225. if (version instanceof SemVer) {
  226. if (version.loose === options.loose)
  227. return version;
  228. else
  229. version = version.version;
  230. } else if (typeof version !== 'string') {
  231. throw new TypeError('Invalid Version: ' + version);
  232. }
  233. if (version.length > MAX_LENGTH)
  234. throw new TypeError('version is longer than ' + MAX_LENGTH + ' characters')
  235. if (!(this instanceof SemVer))
  236. return new SemVer(version, options);
  237. debug('SemVer', version, options);
  238. this.options = options;
  239. this.loose = !!options.loose;
  240. var m = version.trim().match(options.loose ? re[LOOSE] : re[FULL]);
  241. if (!m)
  242. throw new TypeError('Invalid Version: ' + version);
  243. this.raw = version;
  244. // these are actually numbers
  245. this.major = +m[1];
  246. this.minor = +m[2];
  247. this.patch = +m[3];
  248. if (this.major > MAX_SAFE_INTEGER || this.major < 0)
  249. throw new TypeError('Invalid major version')
  250. if (this.minor > MAX_SAFE_INTEGER || this.minor < 0)
  251. throw new TypeError('Invalid minor version')
  252. if (this.patch > MAX_SAFE_INTEGER || this.patch < 0)
  253. throw new TypeError('Invalid patch version')
  254. // numberify any prerelease numeric ids
  255. if (!m[4])
  256. this.prerelease = [];
  257. else
  258. this.prerelease = m[4].split('.').map(function(id) {
  259. if (/^[0-9]+$/.test(id)) {
  260. var num = +id;
  261. if (num >= 0 && num < MAX_SAFE_INTEGER)
  262. return num;
  263. }
  264. return id;
  265. });
  266. this.build = m[5] ? m[5].split('.') : [];
  267. this.format();
  268. }
  269. SemVer.prototype.format = function() {
  270. this.version = this.major + '.' + this.minor + '.' + this.patch;
  271. if (this.prerelease.length)
  272. this.version += '-' + this.prerelease.join('.');
  273. return this.version;
  274. };
  275. SemVer.prototype.toString = function() {
  276. return this.version;
  277. };
  278. SemVer.prototype.compare = function(other) {
  279. debug('SemVer.compare', this.version, this.options, other);
  280. if (!(other instanceof SemVer))
  281. other = new SemVer(other, this.options);
  282. return this.compareMain(other) || this.comparePre(other);
  283. };
  284. SemVer.prototype.compareMain = function(other) {
  285. if (!(other instanceof SemVer))
  286. other = new SemVer(other, this.options);
  287. return compareIdentifiers(this.major, other.major) ||
  288. compareIdentifiers(this.minor, other.minor) ||
  289. compareIdentifiers(this.patch, other.patch);
  290. };
  291. SemVer.prototype.comparePre = function(other) {
  292. if (!(other instanceof SemVer))
  293. other = new SemVer(other, this.options);
  294. // NOT having a prerelease is > having one
  295. if (this.prerelease.length && !other.prerelease.length)
  296. return -1;
  297. else if (!this.prerelease.length && other.prerelease.length)
  298. return 1;
  299. else if (!this.prerelease.length && !other.prerelease.length)
  300. return 0;
  301. var i = 0;
  302. do {
  303. var a = this.prerelease[i];
  304. var b = other.prerelease[i];
  305. debug('prerelease compare', i, a, b);
  306. if (a === undefined && b === undefined)
  307. return 0;
  308. else if (b === undefined)
  309. return 1;
  310. else if (a === undefined)
  311. return -1;
  312. else if (a === b)
  313. continue;
  314. else
  315. return compareIdentifiers(a, b);
  316. } while (++i);
  317. };
  318. // preminor will bump the version up to the next minor release, and immediately
  319. // down to pre-release. premajor and prepatch work the same way.
  320. SemVer.prototype.inc = function(release, identifier) {
  321. switch (release) {
  322. case 'premajor':
  323. this.prerelease.length = 0;
  324. this.patch = 0;
  325. this.minor = 0;
  326. this.major++;
  327. this.inc('pre', identifier);
  328. break;
  329. case 'preminor':
  330. this.prerelease.length = 0;
  331. this.patch = 0;
  332. this.minor++;
  333. this.inc('pre', identifier);
  334. break;
  335. case 'prepatch':
  336. // If this is already a prerelease, it will bump to the next version
  337. // drop any prereleases that might already exist, since they are not
  338. // relevant at this point.
  339. this.prerelease.length = 0;
  340. this.inc('patch', identifier);
  341. this.inc('pre', identifier);
  342. break;
  343. // If the input is a non-prerelease version, this acts the same as
  344. // prepatch.
  345. case 'prerelease':
  346. if (this.prerelease.length === 0)
  347. this.inc('patch', identifier);
  348. this.inc('pre', identifier);
  349. break;
  350. case 'major':
  351. // If this is a pre-major version, bump up to the same major version.
  352. // Otherwise increment major.
  353. // 1.0.0-5 bumps to 1.0.0
  354. // 1.1.0 bumps to 2.0.0
  355. if (this.minor !== 0 || this.patch !== 0 || this.prerelease.length === 0)
  356. this.major++;
  357. this.minor = 0;
  358. this.patch = 0;
  359. this.prerelease = [];
  360. break;
  361. case 'minor':
  362. // If this is a pre-minor version, bump up to the same minor version.
  363. // Otherwise increment minor.
  364. // 1.2.0-5 bumps to 1.2.0
  365. // 1.2.1 bumps to 1.3.0
  366. if (this.patch !== 0 || this.prerelease.length === 0)
  367. this.minor++;
  368. this.patch = 0;
  369. this.prerelease = [];
  370. break;
  371. case 'patch':
  372. // If this is not a pre-release version, it will increment the patch.
  373. // If it is a pre-release it will bump up to the same patch version.
  374. // 1.2.0-5 patches to 1.2.0
  375. // 1.2.0 patches to 1.2.1
  376. if (this.prerelease.length === 0)
  377. this.patch++;
  378. this.prerelease = [];
  379. break;
  380. // This probably shouldn't be used publicly.
  381. // 1.0.0 "pre" would become 1.0.0-0 which is the wrong direction.
  382. case 'pre':
  383. if (this.prerelease.length === 0)
  384. this.prerelease = [0];
  385. else {
  386. var i = this.prerelease.length;
  387. while (--i >= 0) {
  388. if (typeof this.prerelease[i] === 'number') {
  389. this.prerelease[i]++;
  390. i = -2;
  391. }
  392. }
  393. if (i === -1) // didn't increment anything
  394. this.prerelease.push(0);
  395. }
  396. if (identifier) {
  397. // 1.2.0-beta.1 bumps to 1.2.0-beta.2,
  398. // 1.2.0-beta.fooblz or 1.2.0-beta bumps to 1.2.0-beta.0
  399. if (this.prerelease[0] === identifier) {
  400. if (isNaN(this.prerelease[1]))
  401. this.prerelease = [identifier, 0];
  402. } else
  403. this.prerelease = [identifier, 0];
  404. }
  405. break;
  406. default:
  407. throw new Error('invalid increment argument: ' + release);
  408. }
  409. this.format();
  410. this.raw = this.version;
  411. return this;
  412. };
  413. exports.inc = inc;
  414. function inc(version, release, loose, identifier) {
  415. if (typeof(loose) === 'string') {
  416. identifier = loose;
  417. loose = undefined;
  418. }
  419. try {
  420. return new SemVer(version, loose).inc(release, identifier).version;
  421. } catch (er) {
  422. return null;
  423. }
  424. }
  425. exports.diff = diff;
  426. function diff(version1, version2) {
  427. if (eq(version1, version2)) {
  428. return null;
  429. } else {
  430. var v1 = parse(version1);
  431. var v2 = parse(version2);
  432. if (v1.prerelease.length || v2.prerelease.length) {
  433. for (var key in v1) {
  434. if (key === 'major' || key === 'minor' || key === 'patch') {
  435. if (v1[key] !== v2[key]) {
  436. return 'pre'+key;
  437. }
  438. }
  439. }
  440. return 'prerelease';
  441. }
  442. for (var key in v1) {
  443. if (key === 'major' || key === 'minor' || key === 'patch') {
  444. if (v1[key] !== v2[key]) {
  445. return key;
  446. }
  447. }
  448. }
  449. }
  450. }
  451. exports.compareIdentifiers = compareIdentifiers;
  452. var numeric = /^[0-9]+$/;
  453. function compareIdentifiers(a, b) {
  454. var anum = numeric.test(a);
  455. var bnum = numeric.test(b);
  456. if (anum && bnum) {
  457. a = +a;
  458. b = +b;
  459. }
  460. return (anum && !bnum) ? -1 :
  461. (bnum && !anum) ? 1 :
  462. a < b ? -1 :
  463. a > b ? 1 :
  464. 0;
  465. }
  466. exports.rcompareIdentifiers = rcompareIdentifiers;
  467. function rcompareIdentifiers(a, b) {
  468. return compareIdentifiers(b, a);
  469. }
  470. exports.major = major;
  471. function major(a, loose) {
  472. return new SemVer(a, loose).major;
  473. }
  474. exports.minor = minor;
  475. function minor(a, loose) {
  476. return new SemVer(a, loose).minor;
  477. }
  478. exports.patch = patch;
  479. function patch(a, loose) {
  480. return new SemVer(a, loose).patch;
  481. }
  482. exports.compare = compare;
  483. function compare(a, b, loose) {
  484. return new SemVer(a, loose).compare(new SemVer(b, loose));
  485. }
  486. exports.compareLoose = compareLoose;
  487. function compareLoose(a, b) {
  488. return compare(a, b, true);
  489. }
  490. exports.rcompare = rcompare;
  491. function rcompare(a, b, loose) {
  492. return compare(b, a, loose);
  493. }
  494. exports.sort = sort;
  495. function sort(list, loose) {
  496. return list.sort(function(a, b) {
  497. return exports.compare(a, b, loose);
  498. });
  499. }
  500. exports.rsort = rsort;
  501. function rsort(list, loose) {
  502. return list.sort(function(a, b) {
  503. return exports.rcompare(a, b, loose);
  504. });
  505. }
  506. exports.gt = gt;
  507. function gt(a, b, loose) {
  508. return compare(a, b, loose) > 0;
  509. }
  510. exports.lt = lt;
  511. function lt(a, b, loose) {
  512. return compare(a, b, loose) < 0;
  513. }
  514. exports.eq = eq;
  515. function eq(a, b, loose) {
  516. return compare(a, b, loose) === 0;
  517. }
  518. exports.neq = neq;
  519. function neq(a, b, loose) {
  520. return compare(a, b, loose) !== 0;
  521. }
  522. exports.gte = gte;
  523. function gte(a, b, loose) {
  524. return compare(a, b, loose) >= 0;
  525. }
  526. exports.lte = lte;
  527. function lte(a, b, loose) {
  528. return compare(a, b, loose) <= 0;
  529. }
  530. exports.cmp = cmp;
  531. function cmp(a, op, b, loose) {
  532. var ret;
  533. switch (op) {
  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 '!==':
  540. if (typeof a === 'object') a = a.version;
  541. if (typeof b === 'object') b = b.version;
  542. ret = a !== b;
  543. break;
  544. case '': case '=': case '==': ret = eq(a, b, loose); break;
  545. case '!=': ret = neq(a, b, loose); break;
  546. case '>': ret = gt(a, b, loose); break;
  547. case '>=': ret = gte(a, b, loose); break;
  548. case '<': ret = lt(a, b, loose); break;
  549. case '<=': ret = lte(a, b, loose); break;
  550. default: throw new TypeError('Invalid operator: ' + op);
  551. }
  552. return ret;
  553. }
  554. exports.Comparator = Comparator;
  555. function Comparator(comp, options) {
  556. if (!options || typeof options !== 'object')
  557. options = { loose: !!options, includePrerelease: false }
  558. if (comp instanceof Comparator) {
  559. if (comp.loose === !!options.loose)
  560. return comp;
  561. else
  562. comp = comp.value;
  563. }
  564. if (!(this instanceof Comparator))
  565. return new Comparator(comp, options);
  566. debug('comparator', comp, options);
  567. this.options = options;
  568. this.loose = !!options.loose;
  569. this.parse(comp);
  570. if (this.semver === ANY)
  571. this.value = '';
  572. else
  573. this.value = this.operator + this.semver.version;
  574. debug('comp', this);
  575. }
  576. var ANY = {};
  577. Comparator.prototype.parse = function(comp) {
  578. var r = this.options.loose ? re[COMPARATORLOOSE] : re[COMPARATOR];
  579. var m = comp.match(r);
  580. if (!m)
  581. throw new TypeError('Invalid comparator: ' + comp);
  582. this.operator = m[1];
  583. if (this.operator === '=')
  584. this.operator = '';
  585. // if it literally is just '>' or '' then allow anything.
  586. if (!m[2])
  587. this.semver = ANY;
  588. else
  589. this.semver = new SemVer(m[2], this.options.loose);
  590. };
  591. Comparator.prototype.toString = function() {
  592. return this.value;
  593. };
  594. Comparator.prototype.test = function(version) {
  595. debug('Comparator.test', version, this.options.loose);
  596. if (this.semver === ANY)
  597. return true;
  598. if (typeof version === 'string')
  599. version = new SemVer(version, this.options);
  600. return cmp(version, this.operator, this.semver, this.options);
  601. };
  602. Comparator.prototype.intersects = function(comp, options) {
  603. if (!(comp instanceof Comparator)) {
  604. throw new TypeError('a Comparator is required');
  605. }
  606. if (!options || typeof options !== 'object')
  607. options = { loose: !!options, includePrerelease: false }
  608. var rangeTmp;
  609. if (this.operator === '') {
  610. rangeTmp = new Range(comp.value, options);
  611. return satisfies(this.value, rangeTmp, options);
  612. } else if (comp.operator === '') {
  613. rangeTmp = new Range(this.value, options);
  614. return satisfies(comp.semver, rangeTmp, options);
  615. }
  616. var sameDirectionIncreasing =
  617. (this.operator === '>=' || this.operator === '>') &&
  618. (comp.operator === '>=' || comp.operator === '>');
  619. var sameDirectionDecreasing =
  620. (this.operator === '<=' || this.operator === '<') &&
  621. (comp.operator === '<=' || comp.operator === '<');
  622. var sameSemVer = this.semver.version === comp.semver.version;
  623. var differentDirectionsInclusive =
  624. (this.operator === '>=' || this.operator === '<=') &&
  625. (comp.operator === '>=' || comp.operator === '<=');
  626. var oppositeDirectionsLessThan =
  627. cmp(this.semver, '<', comp.semver, options) &&
  628. ((this.operator === '>=' || this.operator === '>') &&
  629. (comp.operator === '<=' || comp.operator === '<'));
  630. var oppositeDirectionsGreaterThan =
  631. cmp(this.semver, '>', comp.semver, options) &&
  632. ((this.operator === '<=' || this.operator === '<') &&
  633. (comp.operator === '>=' || comp.operator === '>'));
  634. return sameDirectionIncreasing || sameDirectionDecreasing ||
  635. (sameSemVer && differentDirectionsInclusive) ||
  636. oppositeDirectionsLessThan || oppositeDirectionsGreaterThan;
  637. };
  638. exports.Range = Range;
  639. function Range(range, options) {
  640. if (!options || typeof options !== 'object')
  641. options = { loose: !!options, includePrerelease: false }
  642. if (range instanceof Range) {
  643. if (range.loose === !!options.loose &&
  644. range.includePrerelease === !!options.includePrerelease) {
  645. return range;
  646. } else {
  647. return new Range(range.raw, options);
  648. }
  649. }
  650. if (range instanceof Comparator) {
  651. return new Range(range.value, options);
  652. }
  653. if (!(this instanceof Range))
  654. return new Range(range, options);
  655. this.options = options;
  656. this.loose = !!options.loose;
  657. this.includePrerelease = !!options.includePrerelease
  658. // First, split based on boolean or ||
  659. this.raw = range;
  660. this.set = range.split(/\s*\|\|\s*/).map(function(range) {
  661. return this.parseRange(range.trim());
  662. }, this).filter(function(c) {
  663. // throw out any that are not relevant for whatever reason
  664. return c.length;
  665. });
  666. if (!this.set.length) {
  667. throw new TypeError('Invalid SemVer Range: ' + range);
  668. }
  669. this.format();
  670. }
  671. Range.prototype.format = function() {
  672. this.range = this.set.map(function(comps) {
  673. return comps.join(' ').trim();
  674. }).join('||').trim();
  675. return this.range;
  676. };
  677. Range.prototype.toString = function() {
  678. return this.range;
  679. };
  680. Range.prototype.parseRange = function(range) {
  681. var loose = this.options.loose;
  682. range = range.trim();
  683. // `1.2.3 - 1.2.4` => `>=1.2.3 <=1.2.4`
  684. var hr = loose ? re[HYPHENRANGELOOSE] : re[HYPHENRANGE];
  685. range = range.replace(hr, hyphenReplace);
  686. debug('hyphen replace', range);
  687. // `> 1.2.3 < 1.2.5` => `>1.2.3 <1.2.5`
  688. range = range.replace(re[COMPARATORTRIM], comparatorTrimReplace);
  689. debug('comparator trim', range, re[COMPARATORTRIM]);
  690. // `~ 1.2.3` => `~1.2.3`
  691. range = range.replace(re[TILDETRIM], tildeTrimReplace);
  692. // `^ 1.2.3` => `^1.2.3`
  693. range = range.replace(re[CARETTRIM], caretTrimReplace);
  694. // normalize spaces
  695. range = range.split(/\s+/).join(' ');
  696. // At this point, the range is completely trimmed and
  697. // ready to be split into comparators.
  698. var compRe = loose ? re[COMPARATORLOOSE] : re[COMPARATOR];
  699. var set = range.split(' ').map(function(comp) {
  700. return parseComparator(comp, this.options);
  701. }, this).join(' ').split(/\s+/);
  702. if (this.options.loose) {
  703. // in loose mode, throw out any that are not valid comparators
  704. set = set.filter(function(comp) {
  705. return !!comp.match(compRe);
  706. });
  707. }
  708. set = set.map(function(comp) {
  709. return new Comparator(comp, this.options);
  710. }, this);
  711. return set;
  712. };
  713. Range.prototype.intersects = function(range, options) {
  714. if (!(range instanceof Range)) {
  715. throw new TypeError('a Range is required');
  716. }
  717. return this.set.some(function(thisComparators) {
  718. return thisComparators.every(function(thisComparator) {
  719. return range.set.some(function(rangeComparators) {
  720. return rangeComparators.every(function(rangeComparator) {
  721. return thisComparator.intersects(rangeComparator, options);
  722. });
  723. });
  724. });
  725. });
  726. };
  727. // Mostly just for testing and legacy API reasons
  728. exports.toComparators = toComparators;
  729. function toComparators(range, options) {
  730. return new Range(range, options).set.map(function(comp) {
  731. return comp.map(function(c) {
  732. return c.value;
  733. }).join(' ').trim().split(' ');
  734. });
  735. }
  736. // comprised of xranges, tildes, stars, and gtlt's at this point.
  737. // already replaced the hyphen ranges
  738. // turn into a set of JUST comparators.
  739. function parseComparator(comp, options) {
  740. debug('comp', comp, options);
  741. comp = replaceCarets(comp, options);
  742. debug('caret', comp);
  743. comp = replaceTildes(comp, options);
  744. debug('tildes', comp);
  745. comp = replaceXRanges(comp, options);
  746. debug('xrange', comp);
  747. comp = replaceStars(comp, options);
  748. debug('stars', comp);
  749. return comp;
  750. }
  751. function isX(id) {
  752. return !id || id.toLowerCase() === 'x' || id === '*';
  753. }
  754. // ~, ~> --> * (any, kinda silly)
  755. // ~2, ~2.x, ~2.x.x, ~>2, ~>2.x ~>2.x.x --> >=2.0.0 <3.0.0
  756. // ~2.0, ~2.0.x, ~>2.0, ~>2.0.x --> >=2.0.0 <2.1.0
  757. // ~1.2, ~1.2.x, ~>1.2, ~>1.2.x --> >=1.2.0 <1.3.0
  758. // ~1.2.3, ~>1.2.3 --> >=1.2.3 <1.3.0
  759. // ~1.2.0, ~>1.2.0 --> >=1.2.0 <1.3.0
  760. function replaceTildes(comp, options) {
  761. return comp.trim().split(/\s+/).map(function(comp) {
  762. return replaceTilde(comp, options);
  763. }).join(' ');
  764. }
  765. function replaceTilde(comp, options) {
  766. if (!options || typeof options !== 'object')
  767. options = { loose: !!options, includePrerelease: false }
  768. var r = options.loose ? re[TILDELOOSE] : re[TILDE];
  769. return comp.replace(r, function(_, M, m, p, pr) {
  770. debug('tilde', comp, _, M, m, p, pr);
  771. var ret;
  772. if (isX(M))
  773. ret = '';
  774. else if (isX(m))
  775. ret = '>=' + M + '.0.0 <' + (+M + 1) + '.0.0';
  776. else if (isX(p))
  777. // ~1.2 == >=1.2.0 <1.3.0
  778. ret = '>=' + M + '.' + m + '.0 <' + M + '.' + (+m + 1) + '.0';
  779. else if (pr) {
  780. debug('replaceTilde pr', pr);
  781. if (pr.charAt(0) !== '-')
  782. pr = '-' + pr;
  783. ret = '>=' + M + '.' + m + '.' + p + pr +
  784. ' <' + M + '.' + (+m + 1) + '.0';
  785. } else
  786. // ~1.2.3 == >=1.2.3 <1.3.0
  787. ret = '>=' + M + '.' + m + '.' + p +
  788. ' <' + M + '.' + (+m + 1) + '.0';
  789. debug('tilde return', ret);
  790. return ret;
  791. });
  792. }
  793. // ^ --> * (any, kinda silly)
  794. // ^2, ^2.x, ^2.x.x --> >=2.0.0 <3.0.0
  795. // ^2.0, ^2.0.x --> >=2.0.0 <3.0.0
  796. // ^1.2, ^1.2.x --> >=1.2.0 <2.0.0
  797. // ^1.2.3 --> >=1.2.3 <2.0.0
  798. // ^1.2.0 --> >=1.2.0 <2.0.0
  799. function replaceCarets(comp, options) {
  800. return comp.trim().split(/\s+/).map(function(comp) {
  801. return replaceCaret(comp, options);
  802. }).join(' ');
  803. }
  804. function replaceCaret(comp, options) {
  805. debug('caret', comp, options);
  806. if (!options || typeof options !== 'object')
  807. options = { loose: !!options, includePrerelease: false }
  808. var r = options.loose ? re[CARETLOOSE] : re[CARET];
  809. return comp.replace(r, function(_, M, m, p, pr) {
  810. debug('caret', comp, _, M, m, p, pr);
  811. var ret;
  812. if (isX(M))
  813. ret = '';
  814. else if (isX(m))
  815. ret = '>=' + M + '.0.0 <' + (+M + 1) + '.0.0';
  816. else if (isX(p)) {
  817. if (M === '0')
  818. ret = '>=' + M + '.' + m + '.0 <' + M + '.' + (+m + 1) + '.0';
  819. else
  820. ret = '>=' + M + '.' + m + '.0 <' + (+M + 1) + '.0.0';
  821. } else if (pr) {
  822. debug('replaceCaret pr', pr);
  823. if (pr.charAt(0) !== '-')
  824. pr = '-' + pr;
  825. if (M === '0') {
  826. if (m === '0')
  827. ret = '>=' + M + '.' + m + '.' + p + pr +
  828. ' <' + M + '.' + m + '.' + (+p + 1);
  829. else
  830. ret = '>=' + M + '.' + m + '.' + p + pr +
  831. ' <' + M + '.' + (+m + 1) + '.0';
  832. } else
  833. ret = '>=' + M + '.' + m + '.' + p + pr +
  834. ' <' + (+M + 1) + '.0.0';
  835. } else {
  836. debug('no pr');
  837. if (M === '0') {
  838. if (m === '0')
  839. ret = '>=' + M + '.' + m + '.' + p +
  840. ' <' + M + '.' + m + '.' + (+p + 1);
  841. else
  842. ret = '>=' + M + '.' + m + '.' + p +
  843. ' <' + M + '.' + (+m + 1) + '.0';
  844. } else
  845. ret = '>=' + M + '.' + m + '.' + p +
  846. ' <' + (+M + 1) + '.0.0';
  847. }
  848. debug('caret return', ret);
  849. return ret;
  850. });
  851. }
  852. function replaceXRanges(comp, options) {
  853. debug('replaceXRanges', comp, options);
  854. return comp.split(/\s+/).map(function(comp) {
  855. return replaceXRange(comp, options);
  856. }).join(' ');
  857. }
  858. function replaceXRange(comp, options) {
  859. comp = comp.trim();
  860. if (!options || typeof options !== 'object')
  861. options = { loose: !!options, includePrerelease: false }
  862. var r = options.loose ? re[XRANGELOOSE] : re[XRANGE];
  863. return comp.replace(r, function(ret, gtlt, M, m, p, pr) {
  864. debug('xRange', comp, ret, gtlt, M, m, p, pr);
  865. var xM = isX(M);
  866. var xm = xM || isX(m);
  867. var xp = xm || isX(p);
  868. var anyX = xp;
  869. if (gtlt === '=' && anyX)
  870. gtlt = '';
  871. if (xM) {
  872. if (gtlt === '>' || gtlt === '<') {
  873. // nothing is allowed
  874. ret = '<0.0.0';
  875. } else {
  876. // nothing is forbidden
  877. ret = '*';
  878. }
  879. } else if (gtlt && anyX) {
  880. // replace X with 0
  881. if (xm)
  882. m = 0;
  883. if (xp)
  884. p = 0;
  885. if (gtlt === '>') {
  886. // >1 => >=2.0.0
  887. // >1.2 => >=1.3.0
  888. // >1.2.3 => >= 1.2.4
  889. gtlt = '>=';
  890. if (xm) {
  891. M = +M + 1;
  892. m = 0;
  893. p = 0;
  894. } else if (xp) {
  895. m = +m + 1;
  896. p = 0;
  897. }
  898. } else if (gtlt === '<=') {
  899. // <=0.7.x is actually <0.8.0, since any 0.7.x should
  900. // pass. Similarly, <=7.x is actually <8.0.0, etc.
  901. gtlt = '<';
  902. if (xm)
  903. M = +M + 1;
  904. else
  905. m = +m + 1;
  906. }
  907. ret = gtlt + M + '.' + m + '.' + p;
  908. } else if (xm) {
  909. ret = '>=' + M + '.0.0 <' + (+M + 1) + '.0.0';
  910. } else if (xp) {
  911. ret = '>=' + M + '.' + m + '.0 <' + M + '.' + (+m + 1) + '.0';
  912. }
  913. debug('xRange return', ret);
  914. return ret;
  915. });
  916. }
  917. // Because * is AND-ed with everything else in the comparator,
  918. // and '' means "any version", just remove the *s entirely.
  919. function replaceStars(comp, options) {
  920. debug('replaceStars', comp, options);
  921. // Looseness is ignored here. star is always as loose as it gets!
  922. return comp.trim().replace(re[STAR], '');
  923. }
  924. // This function is passed to string.replace(re[HYPHENRANGE])
  925. // M, m, patch, prerelease, build
  926. // 1.2 - 3.4.5 => >=1.2.0 <=3.4.5
  927. // 1.2.3 - 3.4 => >=1.2.0 <3.5.0 Any 3.4.x will do
  928. // 1.2 - 3.4 => >=1.2.0 <3.5.0
  929. function hyphenReplace($0,
  930. from, fM, fm, fp, fpr, fb,
  931. to, tM, tm, tp, tpr, tb) {
  932. if (isX(fM))
  933. from = '';
  934. else if (isX(fm))
  935. from = '>=' + fM + '.0.0';
  936. else if (isX(fp))
  937. from = '>=' + fM + '.' + fm + '.0';
  938. else
  939. from = '>=' + from;
  940. if (isX(tM))
  941. to = '';
  942. else if (isX(tm))
  943. to = '<' + (+tM + 1) + '.0.0';
  944. else if (isX(tp))
  945. to = '<' + tM + '.' + (+tm + 1) + '.0';
  946. else if (tpr)
  947. to = '<=' + tM + '.' + tm + '.' + tp + '-' + tpr;
  948. else
  949. to = '<=' + to;
  950. return (from + ' ' + to).trim();
  951. }
  952. // if ANY of the sets match ALL of its comparators, then pass
  953. Range.prototype.test = function(version) {
  954. if (!version)
  955. return false;
  956. if (typeof version === 'string')
  957. version = new SemVer(version, this.options);
  958. for (var i = 0; i < this.set.length; i++) {
  959. if (testSet(this.set[i], version, this.options))
  960. return true;
  961. }
  962. return false;
  963. };
  964. function testSet(set, version, options) {
  965. for (var i = 0; i < set.length; i++) {
  966. if (!set[i].test(version))
  967. return false;
  968. }
  969. if (!options)
  970. options = {}
  971. if (version.prerelease.length && !options.includePrerelease) {
  972. // Find the set of versions that are allowed to have prereleases
  973. // For example, ^1.2.3-pr.1 desugars to >=1.2.3-pr.1 <2.0.0
  974. // That should allow `1.2.3-pr.2` to pass.
  975. // However, `1.2.4-alpha.notready` should NOT be allowed,
  976. // even though it's within the range set by the comparators.
  977. for (var i = 0; i < set.length; i++) {
  978. debug(set[i].semver);
  979. if (set[i].semver === ANY)
  980. continue;
  981. if (set[i].semver.prerelease.length > 0) {
  982. var allowed = set[i].semver;
  983. if (allowed.major === version.major &&
  984. allowed.minor === version.minor &&
  985. allowed.patch === version.patch)
  986. return true;
  987. }
  988. }
  989. // Version has a -pre, but it's not one of the ones we like.
  990. return false;
  991. }
  992. return true;
  993. }
  994. exports.satisfies = satisfies;
  995. function satisfies(version, range, options) {
  996. try {
  997. range = new Range(range, options);
  998. } catch (er) {
  999. return false;
  1000. }
  1001. return range.test(version);
  1002. }
  1003. exports.maxSatisfying = maxSatisfying;
  1004. function maxSatisfying(versions, range, options) {
  1005. var max = null;
  1006. var maxSV = null;
  1007. try {
  1008. var rangeObj = new Range(range, options);
  1009. } catch (er) {
  1010. return null;
  1011. }
  1012. versions.forEach(function (v) {
  1013. if (rangeObj.test(v)) { // satisfies(v, range, options)
  1014. if (!max || maxSV.compare(v) === -1) { // compare(max, v, true)
  1015. max = v;
  1016. maxSV = new SemVer(max, options);
  1017. }
  1018. }
  1019. })
  1020. return max;
  1021. }
  1022. exports.minSatisfying = minSatisfying;
  1023. function minSatisfying(versions, range, options) {
  1024. var min = null;
  1025. var minSV = null;
  1026. try {
  1027. var rangeObj = new Range(range, options);
  1028. } catch (er) {
  1029. return null;
  1030. }
  1031. versions.forEach(function (v) {
  1032. if (rangeObj.test(v)) { // satisfies(v, range, options)
  1033. if (!min || minSV.compare(v) === 1) { // compare(min, v, true)
  1034. min = v;
  1035. minSV = new SemVer(min, options);
  1036. }
  1037. }
  1038. })
  1039. return min;
  1040. }
  1041. exports.validRange = validRange;
  1042. function validRange(range, options) {
  1043. try {
  1044. // Return '*' instead of '' so that truthiness works.
  1045. // This will throw if it's invalid anyway
  1046. return new Range(range, options).range || '*';
  1047. } catch (er) {
  1048. return null;
  1049. }
  1050. }
  1051. // Determine if version is less than all the versions possible in the range
  1052. exports.ltr = ltr;
  1053. function ltr(version, range, options) {
  1054. return outside(version, range, '<', options);
  1055. }
  1056. // Determine if version is greater than all the versions possible in the range.
  1057. exports.gtr = gtr;
  1058. function gtr(version, range, options) {
  1059. return outside(version, range, '>', options);
  1060. }
  1061. exports.outside = outside;
  1062. function outside(version, range, hilo, options) {
  1063. version = new SemVer(version, options);
  1064. range = new Range(range, options);
  1065. var gtfn, ltefn, ltfn, comp, ecomp;
  1066. switch (hilo) {
  1067. case '>':
  1068. gtfn = gt;
  1069. ltefn = lte;
  1070. ltfn = lt;
  1071. comp = '>';
  1072. ecomp = '>=';
  1073. break;
  1074. case '<':
  1075. gtfn = lt;
  1076. ltefn = gte;
  1077. ltfn = gt;
  1078. comp = '<';
  1079. ecomp = '<=';
  1080. break;
  1081. default:
  1082. throw new TypeError('Must provide a hilo val of "<" or ">"');
  1083. }
  1084. // If it satisifes the range it is not outside
  1085. if (satisfies(version, range, options)) {
  1086. return false;
  1087. }
  1088. // From now on, variable terms are as if we're in "gtr" mode.
  1089. // but note that everything is flipped for the "ltr" function.
  1090. for (var i = 0; i < range.set.length; ++i) {
  1091. var comparators = range.set[i];
  1092. var high = null;
  1093. var low = null;
  1094. comparators.forEach(function(comparator) {
  1095. if (comparator.semver === ANY) {
  1096. comparator = new Comparator('>=0.0.0')
  1097. }
  1098. high = high || comparator;
  1099. low = low || comparator;
  1100. if (gtfn(comparator.semver, high.semver, options)) {
  1101. high = comparator;
  1102. } else if (ltfn(comparator.semver, low.semver, options)) {
  1103. low = comparator;
  1104. }
  1105. });
  1106. // If the edge version comparator has a operator then our version
  1107. // isn't outside it
  1108. if (high.operator === comp || high.operator === ecomp) {
  1109. return false;
  1110. }
  1111. // If the lowest version comparator has an operator and our version
  1112. // is less than it then it isn't higher than the range
  1113. if ((!low.operator || low.operator === comp) &&
  1114. ltefn(version, low.semver)) {
  1115. return false;
  1116. } else if (low.operator === ecomp && ltfn(version, low.semver)) {
  1117. return false;
  1118. }
  1119. }
  1120. return true;
  1121. }
  1122. exports.prerelease = prerelease;
  1123. function prerelease(version, options) {
  1124. var parsed = parse(version, options);
  1125. return (parsed && parsed.prerelease.length) ? parsed.prerelease : null;
  1126. }
  1127. exports.intersects = intersects;
  1128. function intersects(r1, r2, options) {
  1129. r1 = new Range(r1, options)
  1130. r2 = new Range(r2, options)
  1131. return r1.intersects(r2)
  1132. }
  1133. exports.coerce = coerce;
  1134. function coerce(version) {
  1135. if (version instanceof SemVer)
  1136. return version;
  1137. if (typeof version !== 'string')
  1138. return null;
  1139. var match = version.match(re[COERCE]);
  1140. if (match == null)
  1141. return null;
  1142. return parse((match[1] || '0') + '.' + (match[2] || '0') + '.' + (match[3] || '0'));
  1143. }