VersionRange.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /**
  2. * Copyright (c) 2013-present, Facebook, Inc.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. */
  8. 'use strict';
  9. var invariant = require('./invariant');
  10. var componentRegex = /\./;
  11. var orRegex = /\|\|/;
  12. var rangeRegex = /\s+\-\s+/;
  13. var modifierRegex = /^(<=|<|=|>=|~>|~|>|)?\s*(.+)/;
  14. var numericRegex = /^(\d*)(.*)/;
  15. /**
  16. * Splits input `range` on "||" and returns true if any subrange matches
  17. * `version`.
  18. *
  19. * @param {string} range
  20. * @param {string} version
  21. * @returns {boolean}
  22. */
  23. function checkOrExpression(range, version) {
  24. var expressions = range.split(orRegex);
  25. if (expressions.length > 1) {
  26. return expressions.some(function (range) {
  27. return VersionRange.contains(range, version);
  28. });
  29. } else {
  30. range = expressions[0].trim();
  31. return checkRangeExpression(range, version);
  32. }
  33. }
  34. /**
  35. * Splits input `range` on " - " (the surrounding whitespace is required) and
  36. * returns true if version falls between the two operands.
  37. *
  38. * @param {string} range
  39. * @param {string} version
  40. * @returns {boolean}
  41. */
  42. function checkRangeExpression(range, version) {
  43. var expressions = range.split(rangeRegex);
  44. !(expressions.length > 0 && expressions.length <= 2) ? process.env.NODE_ENV !== 'production' ? invariant(false, 'the "-" operator expects exactly 2 operands') : invariant(false) : void 0;
  45. if (expressions.length === 1) {
  46. return checkSimpleExpression(expressions[0], version);
  47. } else {
  48. var startVersion = expressions[0],
  49. endVersion = expressions[1];
  50. !(isSimpleVersion(startVersion) && isSimpleVersion(endVersion)) ? process.env.NODE_ENV !== 'production' ? invariant(false, 'operands to the "-" operator must be simple (no modifiers)') : invariant(false) : void 0;
  51. return checkSimpleExpression('>=' + startVersion, version) && checkSimpleExpression('<=' + endVersion, version);
  52. }
  53. }
  54. /**
  55. * Checks if `range` matches `version`. `range` should be a "simple" range (ie.
  56. * not a compound range using the " - " or "||" operators).
  57. *
  58. * @param {string} range
  59. * @param {string} version
  60. * @returns {boolean}
  61. */
  62. function checkSimpleExpression(range, version) {
  63. range = range.trim();
  64. if (range === '') {
  65. return true;
  66. }
  67. var versionComponents = version.split(componentRegex);
  68. var _getModifierAndCompon = getModifierAndComponents(range),
  69. modifier = _getModifierAndCompon.modifier,
  70. rangeComponents = _getModifierAndCompon.rangeComponents;
  71. switch (modifier) {
  72. case '<':
  73. return checkLessThan(versionComponents, rangeComponents);
  74. case '<=':
  75. return checkLessThanOrEqual(versionComponents, rangeComponents);
  76. case '>=':
  77. return checkGreaterThanOrEqual(versionComponents, rangeComponents);
  78. case '>':
  79. return checkGreaterThan(versionComponents, rangeComponents);
  80. case '~':
  81. case '~>':
  82. return checkApproximateVersion(versionComponents, rangeComponents);
  83. default:
  84. return checkEqual(versionComponents, rangeComponents);
  85. }
  86. }
  87. /**
  88. * Checks whether `a` is less than `b`.
  89. *
  90. * @param {array<string>} a
  91. * @param {array<string>} b
  92. * @returns {boolean}
  93. */
  94. function checkLessThan(a, b) {
  95. return compareComponents(a, b) === -1;
  96. }
  97. /**
  98. * Checks whether `a` is less than or equal to `b`.
  99. *
  100. * @param {array<string>} a
  101. * @param {array<string>} b
  102. * @returns {boolean}
  103. */
  104. function checkLessThanOrEqual(a, b) {
  105. var result = compareComponents(a, b);
  106. return result === -1 || result === 0;
  107. }
  108. /**
  109. * Checks whether `a` is equal to `b`.
  110. *
  111. * @param {array<string>} a
  112. * @param {array<string>} b
  113. * @returns {boolean}
  114. */
  115. function checkEqual(a, b) {
  116. return compareComponents(a, b) === 0;
  117. }
  118. /**
  119. * Checks whether `a` is greater than or equal to `b`.
  120. *
  121. * @param {array<string>} a
  122. * @param {array<string>} b
  123. * @returns {boolean}
  124. */
  125. function checkGreaterThanOrEqual(a, b) {
  126. var result = compareComponents(a, b);
  127. return result === 1 || result === 0;
  128. }
  129. /**
  130. * Checks whether `a` is greater than `b`.
  131. *
  132. * @param {array<string>} a
  133. * @param {array<string>} b
  134. * @returns {boolean}
  135. */
  136. function checkGreaterThan(a, b) {
  137. return compareComponents(a, b) === 1;
  138. }
  139. /**
  140. * Checks whether `a` is "reasonably close" to `b` (as described in
  141. * https://www.npmjs.org/doc/misc/semver.html). For example, if `b` is "1.3.1"
  142. * then "reasonably close" is defined as ">= 1.3.1 and < 1.4".
  143. *
  144. * @param {array<string>} a
  145. * @param {array<string>} b
  146. * @returns {boolean}
  147. */
  148. function checkApproximateVersion(a, b) {
  149. var lowerBound = b.slice();
  150. var upperBound = b.slice();
  151. if (upperBound.length > 1) {
  152. upperBound.pop();
  153. }
  154. var lastIndex = upperBound.length - 1;
  155. var numeric = parseInt(upperBound[lastIndex], 10);
  156. if (isNumber(numeric)) {
  157. upperBound[lastIndex] = numeric + 1 + '';
  158. }
  159. return checkGreaterThanOrEqual(a, lowerBound) && checkLessThan(a, upperBound);
  160. }
  161. /**
  162. * Extracts the optional modifier (<, <=, =, >=, >, ~, ~>) and version
  163. * components from `range`.
  164. *
  165. * For example, given `range` ">= 1.2.3" returns an object with a `modifier` of
  166. * `">="` and `components` of `[1, 2, 3]`.
  167. *
  168. * @param {string} range
  169. * @returns {object}
  170. */
  171. function getModifierAndComponents(range) {
  172. var rangeComponents = range.split(componentRegex);
  173. var matches = rangeComponents[0].match(modifierRegex);
  174. !matches ? process.env.NODE_ENV !== 'production' ? invariant(false, 'expected regex to match but it did not') : invariant(false) : void 0;
  175. return {
  176. modifier: matches[1],
  177. rangeComponents: [matches[2]].concat(rangeComponents.slice(1))
  178. };
  179. }
  180. /**
  181. * Determines if `number` is a number.
  182. *
  183. * @param {mixed} number
  184. * @returns {boolean}
  185. */
  186. function isNumber(number) {
  187. return !isNaN(number) && isFinite(number);
  188. }
  189. /**
  190. * Tests whether `range` is a "simple" version number without any modifiers
  191. * (">", "~" etc).
  192. *
  193. * @param {string} range
  194. * @returns {boolean}
  195. */
  196. function isSimpleVersion(range) {
  197. return !getModifierAndComponents(range).modifier;
  198. }
  199. /**
  200. * Zero-pads array `array` until it is at least `length` long.
  201. *
  202. * @param {array} array
  203. * @param {number} length
  204. */
  205. function zeroPad(array, length) {
  206. for (var i = array.length; i < length; i++) {
  207. array[i] = '0';
  208. }
  209. }
  210. /**
  211. * Normalizes `a` and `b` in preparation for comparison by doing the following:
  212. *
  213. * - zero-pads `a` and `b`
  214. * - marks any "x", "X" or "*" component in `b` as equivalent by zero-ing it out
  215. * in both `a` and `b`
  216. * - marks any final "*" component in `b` as a greedy wildcard by zero-ing it
  217. * and all of its successors in `a`
  218. *
  219. * @param {array<string>} a
  220. * @param {array<string>} b
  221. * @returns {array<array<string>>}
  222. */
  223. function normalizeVersions(a, b) {
  224. a = a.slice();
  225. b = b.slice();
  226. zeroPad(a, b.length);
  227. // mark "x" and "*" components as equal
  228. for (var i = 0; i < b.length; i++) {
  229. var matches = b[i].match(/^[x*]$/i);
  230. if (matches) {
  231. b[i] = a[i] = '0';
  232. // final "*" greedily zeros all remaining components
  233. if (matches[0] === '*' && i === b.length - 1) {
  234. for (var j = i; j < a.length; j++) {
  235. a[j] = '0';
  236. }
  237. }
  238. }
  239. }
  240. zeroPad(b, a.length);
  241. return [a, b];
  242. }
  243. /**
  244. * Returns the numerical -- not the lexicographical -- ordering of `a` and `b`.
  245. *
  246. * For example, `10-alpha` is greater than `2-beta`.
  247. *
  248. * @param {string} a
  249. * @param {string} b
  250. * @returns {number} -1, 0 or 1 to indicate whether `a` is less than, equal to,
  251. * or greater than `b`, respectively
  252. */
  253. function compareNumeric(a, b) {
  254. var aPrefix = a.match(numericRegex)[1];
  255. var bPrefix = b.match(numericRegex)[1];
  256. var aNumeric = parseInt(aPrefix, 10);
  257. var bNumeric = parseInt(bPrefix, 10);
  258. if (isNumber(aNumeric) && isNumber(bNumeric) && aNumeric !== bNumeric) {
  259. return compare(aNumeric, bNumeric);
  260. } else {
  261. return compare(a, b);
  262. }
  263. }
  264. /**
  265. * Returns the ordering of `a` and `b`.
  266. *
  267. * @param {string|number} a
  268. * @param {string|number} b
  269. * @returns {number} -1, 0 or 1 to indicate whether `a` is less than, equal to,
  270. * or greater than `b`, respectively
  271. */
  272. function compare(a, b) {
  273. !(typeof a === typeof b) ? process.env.NODE_ENV !== 'production' ? invariant(false, '"a" and "b" must be of the same type') : invariant(false) : void 0;
  274. if (a > b) {
  275. return 1;
  276. } else if (a < b) {
  277. return -1;
  278. } else {
  279. return 0;
  280. }
  281. }
  282. /**
  283. * Compares arrays of version components.
  284. *
  285. * @param {array<string>} a
  286. * @param {array<string>} b
  287. * @returns {number} -1, 0 or 1 to indicate whether `a` is less than, equal to,
  288. * or greater than `b`, respectively
  289. */
  290. function compareComponents(a, b) {
  291. var _normalizeVersions = normalizeVersions(a, b),
  292. aNormalized = _normalizeVersions[0],
  293. bNormalized = _normalizeVersions[1];
  294. for (var i = 0; i < bNormalized.length; i++) {
  295. var result = compareNumeric(aNormalized[i], bNormalized[i]);
  296. if (result) {
  297. return result;
  298. }
  299. }
  300. return 0;
  301. }
  302. var VersionRange = {
  303. /**
  304. * Checks whether `version` satisfies the `range` specification.
  305. *
  306. * We support a subset of the expressions defined in
  307. * https://www.npmjs.org/doc/misc/semver.html:
  308. *
  309. * version Must match version exactly
  310. * =version Same as just version
  311. * >version Must be greater than version
  312. * >=version Must be greater than or equal to version
  313. * <version Must be less than version
  314. * <=version Must be less than or equal to version
  315. * ~version Must be at least version, but less than the next significant
  316. * revision above version:
  317. * "~1.2.3" is equivalent to ">= 1.2.3 and < 1.3"
  318. * ~>version Equivalent to ~version
  319. * 1.2.x Must match "1.2.x", where "x" is a wildcard that matches
  320. * anything
  321. * 1.2.* Similar to "1.2.x", but "*" in the trailing position is a
  322. * "greedy" wildcard, so will match any number of additional
  323. * components:
  324. * "1.2.*" will match "1.2.1", "1.2.1.1", "1.2.1.1.1" etc
  325. * * Any version
  326. * "" (Empty string) Same as *
  327. * v1 - v2 Equivalent to ">= v1 and <= v2"
  328. * r1 || r2 Passes if either r1 or r2 are satisfied
  329. *
  330. * @param {string} range
  331. * @param {string} version
  332. * @returns {boolean}
  333. */
  334. contains: function contains(range, version) {
  335. return checkOrExpression(range.trim(), version.trim());
  336. }
  337. };
  338. module.exports = VersionRange;