semver.js 32 KB

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