VersionRange.js.flow 9.7 KB

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