semver.js 32 KB

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