semver.js 32 KB

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