semver.js 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525
  1. exports = module.exports = SemVer
  2. var debug
  3. /* istanbul ignore next */
  4. if (typeof process === 'object' &&
  5. process.env &&
  6. process.env.NODE_DEBUG &&
  7. /\bsemver\b/i.test(process.env.NODE_DEBUG)) {
  8. debug = function () {
  9. var args = Array.prototype.slice.call(arguments, 0)
  10. args.unshift('SEMVER')
  11. console.log.apply(console, args)
  12. }
  13. } else {
  14. debug = function () {}
  15. }
  16. // Note: this is the semver.org version of the spec that it implements
  17. // Not necessarily the package version of this code.
  18. exports.SEMVER_SPEC_VERSION = '2.0.0'
  19. var MAX_LENGTH = 256
  20. var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER ||
  21. /* istanbul ignore next */ 9007199254740991
  22. // Max safe segment length for coercion.
  23. var MAX_SAFE_COMPONENT_LENGTH = 16
  24. var MAX_SAFE_BUILD_LENGTH = MAX_LENGTH - 6
  25. // The actual regexps go on exports.re
  26. var re = exports.re = []
  27. var safeRe = exports.safeRe = []
  28. var src = exports.src = []
  29. var R = 0
  30. var LETTERDASHNUMBER = '[a-zA-Z0-9-]'
  31. // Replace some greedy regex tokens to prevent regex dos issues. These regex are
  32. // used internally via the safeRe object since all inputs in this library get
  33. // normalized first to trim and collapse all extra whitespace. The original
  34. // regexes are exported for userland consumption and lower level usage. A
  35. // future breaking change could export the safer regex only with a note that
  36. // all input should have extra whitespace removed.
  37. var safeRegexReplacements = [
  38. ['\\s', 1],
  39. ['\\d', MAX_LENGTH],
  40. [LETTERDASHNUMBER, MAX_SAFE_BUILD_LENGTH],
  41. ]
  42. function makeSafeRe (value) {
  43. for (var i = 0; i < safeRegexReplacements.length; i++) {
  44. var token = safeRegexReplacements[i][0]
  45. var max = safeRegexReplacements[i][1]
  46. value = value
  47. .split(token + '*').join(token + '{0,' + max + '}')
  48. .split(token + '+').join(token + '{1,' + max + '}')
  49. }
  50. return value
  51. }
  52. // The following Regular Expressions can be used for tokenizing,
  53. // validating, and parsing SemVer version strings.
  54. // ## Numeric Identifier
  55. // A single `0`, or a non-zero digit followed by zero or more digits.
  56. var NUMERICIDENTIFIER = R++
  57. src[NUMERICIDENTIFIER] = '0|[1-9]\\d*'
  58. var NUMERICIDENTIFIERLOOSE = R++
  59. src[NUMERICIDENTIFIERLOOSE] = '\\d+'
  60. // ## Non-numeric Identifier
  61. // Zero or more digits, followed by a letter or hyphen, and then zero or
  62. // more letters, digits, or hyphens.
  63. var NONNUMERICIDENTIFIER = R++
  64. src[NONNUMERICIDENTIFIER] = '\\d*[a-zA-Z-]' + LETTERDASHNUMBER + '*'
  65. // ## Main Version
  66. // Three dot-separated numeric identifiers.
  67. var MAINVERSION = R++
  68. src[MAINVERSION] = '(' + src[NUMERICIDENTIFIER] + ')\\.' +
  69. '(' + src[NUMERICIDENTIFIER] + ')\\.' +
  70. '(' + src[NUMERICIDENTIFIER] + ')'
  71. var MAINVERSIONLOOSE = R++
  72. src[MAINVERSIONLOOSE] = '(' + src[NUMERICIDENTIFIERLOOSE] + ')\\.' +
  73. '(' + src[NUMERICIDENTIFIERLOOSE] + ')\\.' +
  74. '(' + src[NUMERICIDENTIFIERLOOSE] + ')'
  75. // ## Pre-release Version Identifier
  76. // A numeric identifier, or a non-numeric identifier.
  77. var PRERELEASEIDENTIFIER = R++
  78. src[PRERELEASEIDENTIFIER] = '(?:' + src[NUMERICIDENTIFIER] +
  79. '|' + src[NONNUMERICIDENTIFIER] + ')'
  80. var PRERELEASEIDENTIFIERLOOSE = R++
  81. src[PRERELEASEIDENTIFIERLOOSE] = '(?:' + src[NUMERICIDENTIFIERLOOSE] +
  82. '|' + src[NONNUMERICIDENTIFIER] + ')'
  83. // ## Pre-release Version
  84. // Hyphen, followed by one or more dot-separated pre-release version
  85. // identifiers.
  86. var PRERELEASE = R++
  87. src[PRERELEASE] = '(?:-(' + src[PRERELEASEIDENTIFIER] +
  88. '(?:\\.' + src[PRERELEASEIDENTIFIER] + ')*))'
  89. var PRERELEASELOOSE = R++
  90. src[PRERELEASELOOSE] = '(?:-?(' + src[PRERELEASEIDENTIFIERLOOSE] +
  91. '(?:\\.' + src[PRERELEASEIDENTIFIERLOOSE] + ')*))'
  92. // ## Build Metadata Identifier
  93. // Any combination of digits, letters, or hyphens.
  94. var BUILDIDENTIFIER = R++
  95. src[BUILDIDENTIFIER] = LETTERDASHNUMBER + '+'
  96. // ## Build Metadata
  97. // Plus sign, followed by one or more period-separated build metadata
  98. // identifiers.
  99. var BUILD = R++
  100. src[BUILD] = '(?:\\+(' + src[BUILDIDENTIFIER] +
  101. '(?:\\.' + src[BUILDIDENTIFIER] + ')*))'
  102. // ## Full Version String
  103. // A main version, followed optionally by a pre-release version and
  104. // build metadata.
  105. // Note that the only major, minor, patch, and pre-release sections of
  106. // the version string are capturing groups. The build metadata is not a
  107. // capturing group, because it should not ever be used in version
  108. // comparison.
  109. var FULL = R++
  110. var FULLPLAIN = 'v?' + src[MAINVERSION] +
  111. src[PRERELEASE] + '?' +
  112. src[BUILD] + '?'
  113. src[FULL] = '^' + FULLPLAIN + '$'
  114. // like full, but allows v1.2.3 and =1.2.3, which people do sometimes.
  115. // also, 1.0.0alpha1 (prerelease without the hyphen) which is pretty
  116. // common in the npm registry.
  117. var LOOSEPLAIN = '[v=\\s]*' + src[MAINVERSIONLOOSE] +
  118. src[PRERELEASELOOSE] + '?' +
  119. src[BUILD] + '?'
  120. var LOOSE = R++
  121. src[LOOSE] = '^' + LOOSEPLAIN + '$'
  122. var GTLT = R++
  123. src[GTLT] = '((?:<|>)?=?)'
  124. // Something like "2.*" or "1.2.x".
  125. // Note that "x.x" is a valid xRange identifer, meaning "any version"
  126. // Only the first item is strictly required.
  127. var XRANGEIDENTIFIERLOOSE = R++
  128. src[XRANGEIDENTIFIERLOOSE] = src[NUMERICIDENTIFIERLOOSE] + '|x|X|\\*'
  129. var XRANGEIDENTIFIER = R++
  130. src[XRANGEIDENTIFIER] = src[NUMERICIDENTIFIER] + '|x|X|\\*'
  131. var XRANGEPLAIN = R++
  132. src[XRANGEPLAIN] = '[v=\\s]*(' + src[XRANGEIDENTIFIER] + ')' +
  133. '(?:\\.(' + src[XRANGEIDENTIFIER] + ')' +
  134. '(?:\\.(' + src[XRANGEIDENTIFIER] + ')' +
  135. '(?:' + src[PRERELEASE] + ')?' +
  136. src[BUILD] + '?' +
  137. ')?)?'
  138. var XRANGEPLAINLOOSE = R++
  139. src[XRANGEPLAINLOOSE] = '[v=\\s]*(' + src[XRANGEIDENTIFIERLOOSE] + ')' +
  140. '(?:\\.(' + src[XRANGEIDENTIFIERLOOSE] + ')' +
  141. '(?:\\.(' + src[XRANGEIDENTIFIERLOOSE] + ')' +
  142. '(?:' + src[PRERELEASELOOSE] + ')?' +
  143. src[BUILD] + '?' +
  144. ')?)?'
  145. var XRANGE = R++
  146. src[XRANGE] = '^' + src[GTLT] + '\\s*' + src[XRANGEPLAIN] + '$'
  147. var XRANGELOOSE = R++
  148. src[XRANGELOOSE] = '^' + src[GTLT] + '\\s*' + src[XRANGEPLAINLOOSE] + '$'
  149. // Coercion.
  150. // Extract anything that could conceivably be a part of a valid semver
  151. var COERCE = R++
  152. src[COERCE] = '(?:^|[^\\d])' +
  153. '(\\d{1,' + MAX_SAFE_COMPONENT_LENGTH + '})' +
  154. '(?:\\.(\\d{1,' + MAX_SAFE_COMPONENT_LENGTH + '}))?' +
  155. '(?:\\.(\\d{1,' + MAX_SAFE_COMPONENT_LENGTH + '}))?' +
  156. '(?:$|[^\\d])'
  157. // Tilde ranges.
  158. // Meaning is "reasonably at or greater than"
  159. var LONETILDE = R++
  160. src[LONETILDE] = '(?:~>?)'
  161. var TILDETRIM = R++
  162. src[TILDETRIM] = '(\\s*)' + src[LONETILDE] + '\\s+'
  163. re[TILDETRIM] = new RegExp(src[TILDETRIM], 'g')
  164. safeRe[TILDETRIM] = new RegExp(makeSafeRe(src[TILDETRIM]), 'g')
  165. var tildeTrimReplace = '$1~'
  166. var TILDE = R++
  167. src[TILDE] = '^' + src[LONETILDE] + src[XRANGEPLAIN] + '$'
  168. var TILDELOOSE = R++
  169. src[TILDELOOSE] = '^' + src[LONETILDE] + src[XRANGEPLAINLOOSE] + '$'
  170. // Caret ranges.
  171. // Meaning is "at least and backwards compatible with"
  172. var LONECARET = R++
  173. src[LONECARET] = '(?:\\^)'
  174. var CARETTRIM = R++
  175. src[CARETTRIM] = '(\\s*)' + src[LONECARET] + '\\s+'
  176. re[CARETTRIM] = new RegExp(src[CARETTRIM], 'g')
  177. safeRe[CARETTRIM] = new RegExp(makeSafeRe(src[CARETTRIM]), 'g')
  178. var caretTrimReplace = '$1^'
  179. var CARET = R++
  180. src[CARET] = '^' + src[LONECARET] + src[XRANGEPLAIN] + '$'
  181. var CARETLOOSE = R++
  182. src[CARETLOOSE] = '^' + src[LONECARET] + src[XRANGEPLAINLOOSE] + '$'
  183. // A simple gt/lt/eq thing, or just "" to indicate "any version"
  184. var COMPARATORLOOSE = R++
  185. src[COMPARATORLOOSE] = '^' + src[GTLT] + '\\s*(' + LOOSEPLAIN + ')$|^$'
  186. var COMPARATOR = R++
  187. src[COMPARATOR] = '^' + src[GTLT] + '\\s*(' + FULLPLAIN + ')$|^$'
  188. // An expression to strip any whitespace between the gtlt and the thing
  189. // it modifies, so that `> 1.2.3` ==> `>1.2.3`
  190. var COMPARATORTRIM = R++
  191. src[COMPARATORTRIM] = '(\\s*)' + src[GTLT] +
  192. '\\s*(' + LOOSEPLAIN + '|' + src[XRANGEPLAIN] + ')'
  193. // this one has to use the /g flag
  194. re[COMPARATORTRIM] = new RegExp(src[COMPARATORTRIM], 'g')
  195. safeRe[COMPARATORTRIM] = new RegExp(makeSafeRe(src[COMPARATORTRIM]), 'g')
  196. var comparatorTrimReplace = '$1$2$3'
  197. // Something like `1.2.3 - 1.2.4`
  198. // Note that these all use the loose form, because they'll be
  199. // checked against either the strict or loose comparator form
  200. // later.
  201. var HYPHENRANGE = R++
  202. src[HYPHENRANGE] = '^\\s*(' + src[XRANGEPLAIN] + ')' +
  203. '\\s+-\\s+' +
  204. '(' + src[XRANGEPLAIN] + ')' +
  205. '\\s*$'
  206. var HYPHENRANGELOOSE = R++
  207. src[HYPHENRANGELOOSE] = '^\\s*(' + src[XRANGEPLAINLOOSE] + ')' +
  208. '\\s+-\\s+' +
  209. '(' + src[XRANGEPLAINLOOSE] + ')' +
  210. '\\s*$'
  211. // Star ranges basically just allow anything at all.
  212. var STAR = R++
  213. src[STAR] = '(<|>)?=?\\s*\\*'
  214. // Compile to actual regexp objects.
  215. // All are flag-free, unless they were created above with a flag.
  216. for (var i = 0; i < R; i++) {
  217. debug(i, src[i])
  218. if (!re[i]) {
  219. re[i] = new RegExp(src[i])
  220. // Replace all greedy whitespace to prevent regex dos issues. These regex are
  221. // used internally via the safeRe object since all inputs in this library get
  222. // normalized first to trim and collapse all extra whitespace. The original
  223. // regexes are exported for userland consumption and lower level usage. A
  224. // future breaking change could export the safer regex only with a note that
  225. // all input should have extra whitespace removed.
  226. safeRe[i] = new RegExp(makeSafeRe(src[i]))
  227. }
  228. }
  229. exports.parse = parse
  230. function parse (version, options) {
  231. if (!options || typeof options !== 'object') {
  232. options = {
  233. loose: !!options,
  234. includePrerelease: false
  235. }
  236. }
  237. if (version instanceof SemVer) {
  238. return version
  239. }
  240. if (typeof version !== 'string') {
  241. return null
  242. }
  243. if (version.length > MAX_LENGTH) {
  244. return null
  245. }
  246. var r = options.loose ? safeRe[LOOSE] : safeRe[FULL]
  247. if (!r.test(version)) {
  248. return null
  249. }
  250. try {
  251. return new SemVer(version, options)
  252. } catch (er) {
  253. return null
  254. }
  255. }
  256. exports.valid = valid
  257. function valid (version, options) {
  258. var v = parse(version, options)
  259. return v ? v.version : null
  260. }
  261. exports.clean = clean
  262. function clean (version, options) {
  263. var s = parse(version.trim().replace(/^[=v]+/, ''), options)
  264. return s ? s.version : null
  265. }
  266. exports.SemVer = SemVer
  267. function SemVer (version, options) {
  268. if (!options || typeof options !== 'object') {
  269. options = {
  270. loose: !!options,
  271. includePrerelease: false
  272. }
  273. }
  274. if (version instanceof SemVer) {
  275. if (version.loose === options.loose) {
  276. return version
  277. } else {
  278. version = version.version
  279. }
  280. } else if (typeof version !== 'string') {
  281. throw new TypeError('Invalid Version: ' + version)
  282. }
  283. if (version.length > MAX_LENGTH) {
  284. throw new TypeError('version is longer than ' + MAX_LENGTH + ' characters')
  285. }
  286. if (!(this instanceof SemVer)) {
  287. return new SemVer(version, options)
  288. }
  289. debug('SemVer', version, options)
  290. this.options = options
  291. this.loose = !!options.loose
  292. var m = version.trim().match(options.loose ? safeRe[LOOSE] : safeRe[FULL])
  293. if (!m) {
  294. throw new TypeError('Invalid Version: ' + version)
  295. }
  296. this.raw = version
  297. // these are actually numbers
  298. this.major = +m[1]
  299. this.minor = +m[2]
  300. this.patch = +m[3]
  301. if (this.major > MAX_SAFE_INTEGER || this.major < 0) {
  302. throw new TypeError('Invalid major version')
  303. }
  304. if (this.minor > MAX_SAFE_INTEGER || this.minor < 0) {
  305. throw new TypeError('Invalid minor version')
  306. }
  307. if (this.patch > MAX_SAFE_INTEGER || this.patch < 0) {
  308. throw new TypeError('Invalid patch version')
  309. }
  310. // numberify any prerelease numeric ids
  311. if (!m[4]) {
  312. this.prerelease = []
  313. } else {
  314. this.prerelease = m[4].split('.').map(function (id) {
  315. if (/^[0-9]+$/.test(id)) {
  316. var num = +id
  317. if (num >= 0 && num < MAX_SAFE_INTEGER) {
  318. return num
  319. }
  320. }
  321. return id
  322. })
  323. }
  324. this.build = m[5] ? m[5].split('.') : []
  325. this.format()
  326. }
  327. SemVer.prototype.format = function () {
  328. this.version = this.major + '.' + this.minor + '.' + this.patch
  329. if (this.prerelease.length) {
  330. this.version += '-' + this.prerelease.join('.')
  331. }
  332. return this.version
  333. }
  334. SemVer.prototype.toString = function () {
  335. return this.version
  336. }
  337. SemVer.prototype.compare = function (other) {
  338. debug('SemVer.compare', this.version, this.options, other)
  339. if (!(other instanceof SemVer)) {
  340. other = new SemVer(other, this.options)
  341. }
  342. return this.compareMain(other) || this.comparePre(other)
  343. }
  344. SemVer.prototype.compareMain = function (other) {
  345. if (!(other instanceof SemVer)) {
  346. other = new SemVer(other, this.options)
  347. }
  348. return compareIdentifiers(this.major, other.major) ||
  349. compareIdentifiers(this.minor, other.minor) ||
  350. compareIdentifiers(this.patch, other.patch)
  351. }
  352. SemVer.prototype.comparePre = function (other) {
  353. if (!(other instanceof SemVer)) {
  354. other = new SemVer(other, this.options)
  355. }
  356. // NOT having a prerelease is > having one
  357. if (this.prerelease.length && !other.prerelease.length) {
  358. return -1
  359. } else if (!this.prerelease.length && other.prerelease.length) {
  360. return 1
  361. } else if (!this.prerelease.length && !other.prerelease.length) {
  362. return 0
  363. }
  364. var i = 0
  365. do {
  366. var a = this.prerelease[i]
  367. var b = other.prerelease[i]
  368. debug('prerelease compare', i, a, b)
  369. if (a === undefined && b === undefined) {
  370. return 0
  371. } else if (b === undefined) {
  372. return 1
  373. } else if (a === undefined) {
  374. return -1
  375. } else if (a === b) {
  376. continue
  377. } else {
  378. return compareIdentifiers(a, b)
  379. }
  380. } while (++i)
  381. }
  382. // preminor will bump the version up to the next minor release, and immediately
  383. // down to pre-release. premajor and prepatch work the same way.
  384. SemVer.prototype.inc = function (release, identifier) {
  385. switch (release) {
  386. case 'premajor':
  387. this.prerelease.length = 0
  388. this.patch = 0
  389. this.minor = 0
  390. this.major++
  391. this.inc('pre', identifier)
  392. break
  393. case 'preminor':
  394. this.prerelease.length = 0
  395. this.patch = 0
  396. this.minor++
  397. this.inc('pre', identifier)
  398. break
  399. case 'prepatch':
  400. // If this is already a prerelease, it will bump to the next version
  401. // drop any prereleases that might already exist, since they are not
  402. // relevant at this point.
  403. this.prerelease.length = 0
  404. this.inc('patch', identifier)
  405. this.inc('pre', identifier)
  406. break
  407. // If the input is a non-prerelease version, this acts the same as
  408. // prepatch.
  409. case 'prerelease':
  410. if (this.prerelease.length === 0) {
  411. this.inc('patch', identifier)
  412. }
  413. this.inc('pre', identifier)
  414. break
  415. case 'major':
  416. // If this is a pre-major version, bump up to the same major version.
  417. // Otherwise increment major.
  418. // 1.0.0-5 bumps to 1.0.0
  419. // 1.1.0 bumps to 2.0.0
  420. if (this.minor !== 0 ||
  421. this.patch !== 0 ||
  422. this.prerelease.length === 0) {
  423. this.major++
  424. }
  425. this.minor = 0
  426. this.patch = 0
  427. this.prerelease = []
  428. break
  429. case 'minor':
  430. // If this is a pre-minor version, bump up to the same minor version.
  431. // Otherwise increment minor.
  432. // 1.2.0-5 bumps to 1.2.0
  433. // 1.2.1 bumps to 1.3.0
  434. if (this.patch !== 0 || this.prerelease.length === 0) {
  435. this.minor++
  436. }
  437. this.patch = 0
  438. this.prerelease = []
  439. break
  440. case 'patch':
  441. // If this is not a pre-release version, it will increment the patch.
  442. // If it is a pre-release it will bump up to the same patch version.
  443. // 1.2.0-5 patches to 1.2.0
  444. // 1.2.0 patches to 1.2.1
  445. if (this.prerelease.length === 0) {
  446. this.patch++
  447. }
  448. this.prerelease = []
  449. break
  450. // This probably shouldn't be used publicly.
  451. // 1.0.0 "pre" would become 1.0.0-0 which is the wrong direction.
  452. case 'pre':
  453. if (this.prerelease.length === 0) {
  454. this.prerelease = [0]
  455. } else {
  456. var i = this.prerelease.length
  457. while (--i >= 0) {
  458. if (typeof this.prerelease[i] === 'number') {
  459. this.prerelease[i]++
  460. i = -2
  461. }
  462. }
  463. if (i === -1) {
  464. // didn't increment anything
  465. this.prerelease.push(0)
  466. }
  467. }
  468. if (identifier) {
  469. // 1.2.0-beta.1 bumps to 1.2.0-beta.2,
  470. // 1.2.0-beta.fooblz or 1.2.0-beta bumps to 1.2.0-beta.0
  471. if (this.prerelease[0] === identifier) {
  472. if (isNaN(this.prerelease[1])) {
  473. this.prerelease = [identifier, 0]
  474. }
  475. } else {
  476. this.prerelease = [identifier, 0]
  477. }
  478. }
  479. break
  480. default:
  481. throw new Error('invalid increment argument: ' + release)
  482. }
  483. this.format()
  484. this.raw = this.version
  485. return this
  486. }
  487. exports.inc = inc
  488. function inc (version, release, loose, identifier) {
  489. if (typeof (loose) === 'string') {
  490. identifier = loose
  491. loose = undefined
  492. }
  493. try {
  494. return new SemVer(version, loose).inc(release, identifier).version
  495. } catch (er) {
  496. return null
  497. }
  498. }
  499. exports.diff = diff
  500. function diff (version1, version2) {
  501. if (eq(version1, version2)) {
  502. return null
  503. } else {
  504. var v1 = parse(version1)
  505. var v2 = parse(version2)
  506. var prefix = ''
  507. if (v1.prerelease.length || v2.prerelease.length) {
  508. prefix = 'pre'
  509. var defaultResult = 'prerelease'
  510. }
  511. for (var key in v1) {
  512. if (key === 'major' || key === 'minor' || key === 'patch') {
  513. if (v1[key] !== v2[key]) {
  514. return prefix + key
  515. }
  516. }
  517. }
  518. return defaultResult // may be undefined
  519. }
  520. }
  521. exports.compareIdentifiers = compareIdentifiers
  522. var numeric = /^[0-9]+$/
  523. function compareIdentifiers (a, b) {
  524. var anum = numeric.test(a)
  525. var bnum = numeric.test(b)
  526. if (anum && bnum) {
  527. a = +a
  528. b = +b
  529. }
  530. return a === b ? 0
  531. : (anum && !bnum) ? -1
  532. : (bnum && !anum) ? 1
  533. : a < b ? -1
  534. : 1
  535. }
  536. exports.rcompareIdentifiers = rcompareIdentifiers
  537. function rcompareIdentifiers (a, b) {
  538. return compareIdentifiers(b, a)
  539. }
  540. exports.major = major
  541. function major (a, loose) {
  542. return new SemVer(a, loose).major
  543. }
  544. exports.minor = minor
  545. function minor (a, loose) {
  546. return new SemVer(a, loose).minor
  547. }
  548. exports.patch = patch
  549. function patch (a, loose) {
  550. return new SemVer(a, loose).patch
  551. }
  552. exports.compare = compare
  553. function compare (a, b, loose) {
  554. return new SemVer(a, loose).compare(new SemVer(b, loose))
  555. }
  556. exports.compareLoose = compareLoose
  557. function compareLoose (a, b) {
  558. return compare(a, b, true)
  559. }
  560. exports.rcompare = rcompare
  561. function rcompare (a, b, loose) {
  562. return compare(b, a, loose)
  563. }
  564. exports.sort = sort
  565. function sort (list, loose) {
  566. return list.sort(function (a, b) {
  567. return exports.compare(a, b, loose)
  568. })
  569. }
  570. exports.rsort = rsort
  571. function rsort (list, loose) {
  572. return list.sort(function (a, b) {
  573. return exports.rcompare(a, b, loose)
  574. })
  575. }
  576. exports.gt = gt
  577. function gt (a, b, loose) {
  578. return compare(a, b, loose) > 0
  579. }
  580. exports.lt = lt
  581. function lt (a, b, loose) {
  582. return compare(a, b, loose) < 0
  583. }
  584. exports.eq = eq
  585. function eq (a, b, loose) {
  586. return compare(a, b, loose) === 0
  587. }
  588. exports.neq = neq
  589. function neq (a, b, loose) {
  590. return compare(a, b, loose) !== 0
  591. }
  592. exports.gte = gte
  593. function gte (a, b, loose) {
  594. return compare(a, b, loose) >= 0
  595. }
  596. exports.lte = lte
  597. function lte (a, b, loose) {
  598. return compare(a, b, loose) <= 0
  599. }
  600. exports.cmp = cmp
  601. function cmp (a, op, b, loose) {
  602. switch (op) {
  603. case '===':
  604. if (typeof a === 'object')
  605. a = a.version
  606. if (typeof b === 'object')
  607. b = b.version
  608. return a === b
  609. case '!==':
  610. if (typeof a === 'object')
  611. a = a.version
  612. if (typeof b === 'object')
  613. b = b.version
  614. return a !== b
  615. case '':
  616. case '=':
  617. case '==':
  618. return eq(a, b, loose)
  619. case '!=':
  620. return neq(a, b, loose)
  621. case '>':
  622. return gt(a, b, loose)
  623. case '>=':
  624. return gte(a, b, loose)
  625. case '<':
  626. return lt(a, b, loose)
  627. case '<=':
  628. return lte(a, b, loose)
  629. default:
  630. throw new TypeError('Invalid operator: ' + op)
  631. }
  632. }
  633. exports.Comparator = Comparator
  634. function Comparator (comp, options) {
  635. if (!options || typeof options !== 'object') {
  636. options = {
  637. loose: !!options,
  638. includePrerelease: false
  639. }
  640. }
  641. if (comp instanceof Comparator) {
  642. if (comp.loose === !!options.loose) {
  643. return comp
  644. } else {
  645. comp = comp.value
  646. }
  647. }
  648. if (!(this instanceof Comparator)) {
  649. return new Comparator(comp, options)
  650. }
  651. comp = comp.trim().split(/\s+/).join(' ')
  652. debug('comparator', comp, options)
  653. this.options = options
  654. this.loose = !!options.loose
  655. this.parse(comp)
  656. if (this.semver === ANY) {
  657. this.value = ''
  658. } else {
  659. this.value = this.operator + this.semver.version
  660. }
  661. debug('comp', this)
  662. }
  663. var ANY = {}
  664. Comparator.prototype.parse = function (comp) {
  665. var r = this.options.loose ? safeRe[COMPARATORLOOSE] : safeRe[COMPARATOR]
  666. var m = comp.match(r)
  667. if (!m) {
  668. throw new TypeError('Invalid comparator: ' + comp)
  669. }
  670. this.operator = m[1]
  671. if (this.operator === '=') {
  672. this.operator = ''
  673. }
  674. // if it literally is just '>' or '' then allow anything.
  675. if (!m[2]) {
  676. this.semver = ANY
  677. } else {
  678. this.semver = new SemVer(m[2], this.options.loose)
  679. }
  680. }
  681. Comparator.prototype.toString = function () {
  682. return this.value
  683. }
  684. Comparator.prototype.test = function (version) {
  685. debug('Comparator.test', version, this.options.loose)
  686. if (this.semver === ANY) {
  687. return true
  688. }
  689. if (typeof version === 'string') {
  690. version = new SemVer(version, this.options)
  691. }
  692. return cmp(version, this.operator, this.semver, this.options)
  693. }
  694. Comparator.prototype.intersects = function (comp, options) {
  695. if (!(comp instanceof Comparator)) {
  696. throw new TypeError('a Comparator is required')
  697. }
  698. if (!options || typeof options !== 'object') {
  699. options = {
  700. loose: !!options,
  701. includePrerelease: false
  702. }
  703. }
  704. var rangeTmp
  705. if (this.operator === '') {
  706. rangeTmp = new Range(comp.value, options)
  707. return satisfies(this.value, rangeTmp, options)
  708. } else if (comp.operator === '') {
  709. rangeTmp = new Range(this.value, options)
  710. return satisfies(comp.semver, rangeTmp, options)
  711. }
  712. var sameDirectionIncreasing =
  713. (this.operator === '>=' || this.operator === '>') &&
  714. (comp.operator === '>=' || comp.operator === '>')
  715. var sameDirectionDecreasing =
  716. (this.operator === '<=' || this.operator === '<') &&
  717. (comp.operator === '<=' || comp.operator === '<')
  718. var sameSemVer = this.semver.version === comp.semver.version
  719. var differentDirectionsInclusive =
  720. (this.operator === '>=' || this.operator === '<=') &&
  721. (comp.operator === '>=' || comp.operator === '<=')
  722. var oppositeDirectionsLessThan =
  723. cmp(this.semver, '<', comp.semver, options) &&
  724. ((this.operator === '>=' || this.operator === '>') &&
  725. (comp.operator === '<=' || comp.operator === '<'))
  726. var oppositeDirectionsGreaterThan =
  727. cmp(this.semver, '>', comp.semver, options) &&
  728. ((this.operator === '<=' || this.operator === '<') &&
  729. (comp.operator === '>=' || comp.operator === '>'))
  730. return sameDirectionIncreasing || sameDirectionDecreasing ||
  731. (sameSemVer && differentDirectionsInclusive) ||
  732. oppositeDirectionsLessThan || oppositeDirectionsGreaterThan
  733. }
  734. exports.Range = Range
  735. function Range (range, options) {
  736. if (!options || typeof options !== 'object') {
  737. options = {
  738. loose: !!options,
  739. includePrerelease: false
  740. }
  741. }
  742. if (range instanceof Range) {
  743. if (range.loose === !!options.loose &&
  744. range.includePrerelease === !!options.includePrerelease) {
  745. return range
  746. } else {
  747. return new Range(range.raw, options)
  748. }
  749. }
  750. if (range instanceof Comparator) {
  751. return new Range(range.value, options)
  752. }
  753. if (!(this instanceof Range)) {
  754. return new Range(range, options)
  755. }
  756. this.options = options
  757. this.loose = !!options.loose
  758. this.includePrerelease = !!options.includePrerelease
  759. // First reduce all whitespace as much as possible so we do not have to rely
  760. // on potentially slow regexes like \s*. This is then stored and used for
  761. // future error messages as well.
  762. this.raw = range
  763. .trim()
  764. .split(/\s+/)
  765. .join(' ')
  766. // First, split based on boolean or ||
  767. this.set = this.raw.split('||').map(function (range) {
  768. return this.parseRange(range.trim())
  769. }, this).filter(function (c) {
  770. // throw out any that are not relevant for whatever reason
  771. return c.length
  772. })
  773. if (!this.set.length) {
  774. throw new TypeError('Invalid SemVer Range: ' + this.raw)
  775. }
  776. this.format()
  777. }
  778. Range.prototype.format = function () {
  779. this.range = this.set.map(function (comps) {
  780. return comps.join(' ').trim()
  781. }).join('||').trim()
  782. return this.range
  783. }
  784. Range.prototype.toString = function () {
  785. return this.range
  786. }
  787. Range.prototype.parseRange = function (range) {
  788. var loose = this.options.loose
  789. // `1.2.3 - 1.2.4` => `>=1.2.3 <=1.2.4`
  790. var hr = loose ? safeRe[HYPHENRANGELOOSE] : safeRe[HYPHENRANGE]
  791. range = range.replace(hr, hyphenReplace)
  792. debug('hyphen replace', range)
  793. // `> 1.2.3 < 1.2.5` => `>1.2.3 <1.2.5`
  794. range = range.replace(safeRe[COMPARATORTRIM], comparatorTrimReplace)
  795. debug('comparator trim', range, safeRe[COMPARATORTRIM])
  796. // `~ 1.2.3` => `~1.2.3`
  797. range = range.replace(safeRe[TILDETRIM], tildeTrimReplace)
  798. // `^ 1.2.3` => `^1.2.3`
  799. range = range.replace(safeRe[CARETTRIM], caretTrimReplace)
  800. // At this point, the range is completely trimmed and
  801. // ready to be split into comparators.
  802. var compRe = loose ? safeRe[COMPARATORLOOSE] : safeRe[COMPARATOR]
  803. var set = range.split(' ').map(function (comp) {
  804. return parseComparator(comp, this.options)
  805. }, this).join(' ').split(/\s+/)
  806. if (this.options.loose) {
  807. // in loose mode, throw out any that are not valid comparators
  808. set = set.filter(function (comp) {
  809. return !!comp.match(compRe)
  810. })
  811. }
  812. set = set.map(function (comp) {
  813. return new Comparator(comp, this.options)
  814. }, this)
  815. return set
  816. }
  817. Range.prototype.intersects = function (range, options) {
  818. if (!(range instanceof Range)) {
  819. throw new TypeError('a Range is required')
  820. }
  821. return this.set.some(function (thisComparators) {
  822. return thisComparators.every(function (thisComparator) {
  823. return range.set.some(function (rangeComparators) {
  824. return rangeComparators.every(function (rangeComparator) {
  825. return thisComparator.intersects(rangeComparator, options)
  826. })
  827. })
  828. })
  829. })
  830. }
  831. // Mostly just for testing and legacy API reasons
  832. exports.toComparators = toComparators
  833. function toComparators (range, options) {
  834. return new Range(range, options).set.map(function (comp) {
  835. return comp.map(function (c) {
  836. return c.value
  837. }).join(' ').trim().split(' ')
  838. })
  839. }
  840. // comprised of xranges, tildes, stars, and gtlt's at this point.
  841. // already replaced the hyphen ranges
  842. // turn into a set of JUST comparators.
  843. function parseComparator (comp, options) {
  844. debug('comp', comp, options)
  845. comp = replaceCarets(comp, options)
  846. debug('caret', comp)
  847. comp = replaceTildes(comp, options)
  848. debug('tildes', comp)
  849. comp = replaceXRanges(comp, options)
  850. debug('xrange', comp)
  851. comp = replaceStars(comp, options)
  852. debug('stars', comp)
  853. return comp
  854. }
  855. function isX (id) {
  856. return !id || id.toLowerCase() === 'x' || id === '*'
  857. }
  858. // ~, ~> --> * (any, kinda silly)
  859. // ~2, ~2.x, ~2.x.x, ~>2, ~>2.x ~>2.x.x --> >=2.0.0 <3.0.0
  860. // ~2.0, ~2.0.x, ~>2.0, ~>2.0.x --> >=2.0.0 <2.1.0
  861. // ~1.2, ~1.2.x, ~>1.2, ~>1.2.x --> >=1.2.0 <1.3.0
  862. // ~1.2.3, ~>1.2.3 --> >=1.2.3 <1.3.0
  863. // ~1.2.0, ~>1.2.0 --> >=1.2.0 <1.3.0
  864. function replaceTildes (comp, options) {
  865. return comp.trim().split(/\s+/).map(function (comp) {
  866. return replaceTilde(comp, options)
  867. }).join(' ')
  868. }
  869. function replaceTilde (comp, options) {
  870. var r = options.loose ? safeRe[TILDELOOSE] : safeRe[TILDE]
  871. return comp.replace(r, function (_, M, m, p, pr) {
  872. debug('tilde', comp, _, M, m, p, pr)
  873. var ret
  874. if (isX(M)) {
  875. ret = ''
  876. } else if (isX(m)) {
  877. ret = '>=' + M + '.0.0 <' + (+M + 1) + '.0.0'
  878. } else if (isX(p)) {
  879. // ~1.2 == >=1.2.0 <1.3.0
  880. ret = '>=' + M + '.' + m + '.0 <' + M + '.' + (+m + 1) + '.0'
  881. } else if (pr) {
  882. debug('replaceTilde pr', pr)
  883. ret = '>=' + M + '.' + m + '.' + p + '-' + pr +
  884. ' <' + M + '.' + (+m + 1) + '.0'
  885. } else {
  886. // ~1.2.3 == >=1.2.3 <1.3.0
  887. ret = '>=' + M + '.' + m + '.' + p +
  888. ' <' + M + '.' + (+m + 1) + '.0'
  889. }
  890. debug('tilde return', ret)
  891. return ret
  892. })
  893. }
  894. // ^ --> * (any, kinda silly)
  895. // ^2, ^2.x, ^2.x.x --> >=2.0.0 <3.0.0
  896. // ^2.0, ^2.0.x --> >=2.0.0 <3.0.0
  897. // ^1.2, ^1.2.x --> >=1.2.0 <2.0.0
  898. // ^1.2.3 --> >=1.2.3 <2.0.0
  899. // ^1.2.0 --> >=1.2.0 <2.0.0
  900. function replaceCarets (comp, options) {
  901. return comp.trim().split(/\s+/).map(function (comp) {
  902. return replaceCaret(comp, options)
  903. }).join(' ')
  904. }
  905. function replaceCaret (comp, options) {
  906. debug('caret', comp, options)
  907. var r = options.loose ? safeRe[CARETLOOSE] : safeRe[CARET]
  908. return comp.replace(r, function (_, M, m, p, pr) {
  909. debug('caret', comp, _, M, m, p, pr)
  910. var ret
  911. if (isX(M)) {
  912. ret = ''
  913. } else if (isX(m)) {
  914. ret = '>=' + M + '.0.0 <' + (+M + 1) + '.0.0'
  915. } else if (isX(p)) {
  916. if (M === '0') {
  917. ret = '>=' + M + '.' + m + '.0 <' + M + '.' + (+m + 1) + '.0'
  918. } else {
  919. ret = '>=' + M + '.' + m + '.0 <' + (+M + 1) + '.0.0'
  920. }
  921. } else if (pr) {
  922. debug('replaceCaret pr', pr)
  923. if (M === '0') {
  924. if (m === '0') {
  925. ret = '>=' + M + '.' + m + '.' + p + '-' + pr +
  926. ' <' + M + '.' + m + '.' + (+p + 1)
  927. } else {
  928. ret = '>=' + M + '.' + m + '.' + p + '-' + pr +
  929. ' <' + M + '.' + (+m + 1) + '.0'
  930. }
  931. } else {
  932. ret = '>=' + M + '.' + m + '.' + p + '-' + pr +
  933. ' <' + (+M + 1) + '.0.0'
  934. }
  935. } else {
  936. debug('no pr')
  937. if (M === '0') {
  938. if (m === '0') {
  939. ret = '>=' + M + '.' + m + '.' + p +
  940. ' <' + M + '.' + m + '.' + (+p + 1)
  941. } else {
  942. ret = '>=' + M + '.' + m + '.' + p +
  943. ' <' + M + '.' + (+m + 1) + '.0'
  944. }
  945. } else {
  946. ret = '>=' + M + '.' + m + '.' + p +
  947. ' <' + (+M + 1) + '.0.0'
  948. }
  949. }
  950. debug('caret return', ret)
  951. return ret
  952. })
  953. }
  954. function replaceXRanges (comp, options) {
  955. debug('replaceXRanges', comp, options)
  956. return comp.split(/\s+/).map(function (comp) {
  957. return replaceXRange(comp, options)
  958. }).join(' ')
  959. }
  960. function replaceXRange (comp, options) {
  961. comp = comp.trim()
  962. var r = options.loose ? safeRe[XRANGELOOSE] : safeRe[XRANGE]
  963. return comp.replace(r, function (ret, gtlt, M, m, p, pr) {
  964. debug('xRange', comp, ret, gtlt, M, m, p, pr)
  965. var xM = isX(M)
  966. var xm = xM || isX(m)
  967. var xp = xm || isX(p)
  968. var anyX = xp
  969. if (gtlt === '=' && anyX) {
  970. gtlt = ''
  971. }
  972. if (xM) {
  973. if (gtlt === '>' || gtlt === '<') {
  974. // nothing is allowed
  975. ret = '<0.0.0'
  976. } else {
  977. // nothing is forbidden
  978. ret = '*'
  979. }
  980. } else if (gtlt && anyX) {
  981. // we know patch is an x, because we have any x at all.
  982. // replace X with 0
  983. if (xm) {
  984. m = 0
  985. }
  986. p = 0
  987. if (gtlt === '>') {
  988. // >1 => >=2.0.0
  989. // >1.2 => >=1.3.0
  990. // >1.2.3 => >= 1.2.4
  991. gtlt = '>='
  992. if (xm) {
  993. M = +M + 1
  994. m = 0
  995. p = 0
  996. } else {
  997. m = +m + 1
  998. p = 0
  999. }
  1000. } else if (gtlt === '<=') {
  1001. // <=0.7.x is actually <0.8.0, since any 0.7.x should
  1002. // pass. Similarly, <=7.x is actually <8.0.0, etc.
  1003. gtlt = '<'
  1004. if (xm) {
  1005. M = +M + 1
  1006. } else {
  1007. m = +m + 1
  1008. }
  1009. }
  1010. ret = gtlt + M + '.' + m + '.' + p
  1011. } else if (xm) {
  1012. ret = '>=' + M + '.0.0 <' + (+M + 1) + '.0.0'
  1013. } else if (xp) {
  1014. ret = '>=' + M + '.' + m + '.0 <' + M + '.' + (+m + 1) + '.0'
  1015. }
  1016. debug('xRange return', ret)
  1017. return ret
  1018. })
  1019. }
  1020. // Because * is AND-ed with everything else in the comparator,
  1021. // and '' means "any version", just remove the *s entirely.
  1022. function replaceStars (comp, options) {
  1023. debug('replaceStars', comp, options)
  1024. // Looseness is ignored here. star is always as loose as it gets!
  1025. return comp.trim().replace(safeRe[STAR], '')
  1026. }
  1027. // This function is passed to string.replace(safeRe[HYPHENRANGE])
  1028. // M, m, patch, prerelease, build
  1029. // 1.2 - 3.4.5 => >=1.2.0 <=3.4.5
  1030. // 1.2.3 - 3.4 => >=1.2.0 <3.5.0 Any 3.4.x will do
  1031. // 1.2 - 3.4 => >=1.2.0 <3.5.0
  1032. function hyphenReplace ($0,
  1033. from, fM, fm, fp, fpr, fb,
  1034. to, tM, tm, tp, tpr, tb) {
  1035. if (isX(fM)) {
  1036. from = ''
  1037. } else if (isX(fm)) {
  1038. from = '>=' + fM + '.0.0'
  1039. } else if (isX(fp)) {
  1040. from = '>=' + fM + '.' + fm + '.0'
  1041. } else {
  1042. from = '>=' + from
  1043. }
  1044. if (isX(tM)) {
  1045. to = ''
  1046. } else if (isX(tm)) {
  1047. to = '<' + (+tM + 1) + '.0.0'
  1048. } else if (isX(tp)) {
  1049. to = '<' + tM + '.' + (+tm + 1) + '.0'
  1050. } else if (tpr) {
  1051. to = '<=' + tM + '.' + tm + '.' + tp + '-' + tpr
  1052. } else {
  1053. to = '<=' + to
  1054. }
  1055. return (from + ' ' + to).trim()
  1056. }
  1057. // if ANY of the sets match ALL of its comparators, then pass
  1058. Range.prototype.test = function (version) {
  1059. if (!version) {
  1060. return false
  1061. }
  1062. if (typeof version === 'string') {
  1063. version = new SemVer(version, this.options)
  1064. }
  1065. for (var i = 0; i < this.set.length; i++) {
  1066. if (testSet(this.set[i], version, this.options)) {
  1067. return true
  1068. }
  1069. }
  1070. return false
  1071. }
  1072. function testSet (set, version, options) {
  1073. for (var i = 0; i < set.length; i++) {
  1074. if (!set[i].test(version)) {
  1075. return false
  1076. }
  1077. }
  1078. if (version.prerelease.length && !options.includePrerelease) {
  1079. // Find the set of versions that are allowed to have prereleases
  1080. // For example, ^1.2.3-pr.1 desugars to >=1.2.3-pr.1 <2.0.0
  1081. // That should allow `1.2.3-pr.2` to pass.
  1082. // However, `1.2.4-alpha.notready` should NOT be allowed,
  1083. // even though it's within the range set by the comparators.
  1084. for (i = 0; i < set.length; i++) {
  1085. debug(set[i].semver)
  1086. if (set[i].semver === ANY) {
  1087. continue
  1088. }
  1089. if (set[i].semver.prerelease.length > 0) {
  1090. var allowed = set[i].semver
  1091. if (allowed.major === version.major &&
  1092. allowed.minor === version.minor &&
  1093. allowed.patch === version.patch) {
  1094. return true
  1095. }
  1096. }
  1097. }
  1098. // Version has a -pre, but it's not one of the ones we like.
  1099. return false
  1100. }
  1101. return true
  1102. }
  1103. exports.satisfies = satisfies
  1104. function satisfies (version, range, options) {
  1105. try {
  1106. range = new Range(range, options)
  1107. } catch (er) {
  1108. return false
  1109. }
  1110. return range.test(version)
  1111. }
  1112. exports.maxSatisfying = maxSatisfying
  1113. function maxSatisfying (versions, range, options) {
  1114. var max = null
  1115. var maxSV = null
  1116. try {
  1117. var rangeObj = new Range(range, options)
  1118. } catch (er) {
  1119. return null
  1120. }
  1121. versions.forEach(function (v) {
  1122. if (rangeObj.test(v)) {
  1123. // satisfies(v, range, options)
  1124. if (!max || maxSV.compare(v) === -1) {
  1125. // compare(max, v, true)
  1126. max = v
  1127. maxSV = new SemVer(max, options)
  1128. }
  1129. }
  1130. })
  1131. return max
  1132. }
  1133. exports.minSatisfying = minSatisfying
  1134. function minSatisfying (versions, range, options) {
  1135. var min = null
  1136. var minSV = null
  1137. try {
  1138. var rangeObj = new Range(range, options)
  1139. } catch (er) {
  1140. return null
  1141. }
  1142. versions.forEach(function (v) {
  1143. if (rangeObj.test(v)) {
  1144. // satisfies(v, range, options)
  1145. if (!min || minSV.compare(v) === 1) {
  1146. // compare(min, v, true)
  1147. min = v
  1148. minSV = new SemVer(min, options)
  1149. }
  1150. }
  1151. })
  1152. return min
  1153. }
  1154. exports.minVersion = minVersion
  1155. function minVersion (range, loose) {
  1156. range = new Range(range, loose)
  1157. var minver = new SemVer('0.0.0')
  1158. if (range.test(minver)) {
  1159. return minver
  1160. }
  1161. minver = new SemVer('0.0.0-0')
  1162. if (range.test(minver)) {
  1163. return minver
  1164. }
  1165. minver = null
  1166. for (var i = 0; i < range.set.length; ++i) {
  1167. var comparators = range.set[i]
  1168. comparators.forEach(function (comparator) {
  1169. // Clone to avoid manipulating the comparator's semver object.
  1170. var compver = new SemVer(comparator.semver.version)
  1171. switch (comparator.operator) {
  1172. case '>':
  1173. if (compver.prerelease.length === 0) {
  1174. compver.patch++
  1175. } else {
  1176. compver.prerelease.push(0)
  1177. }
  1178. compver.raw = compver.format()
  1179. /* fallthrough */
  1180. case '':
  1181. case '>=':
  1182. if (!minver || gt(minver, compver)) {
  1183. minver = compver
  1184. }
  1185. break
  1186. case '<':
  1187. case '<=':
  1188. /* Ignore maximum versions */
  1189. break
  1190. /* istanbul ignore next */
  1191. default:
  1192. throw new Error('Unexpected operation: ' + comparator.operator)
  1193. }
  1194. })
  1195. }
  1196. if (minver && range.test(minver)) {
  1197. return minver
  1198. }
  1199. return null
  1200. }
  1201. exports.validRange = validRange
  1202. function validRange (range, options) {
  1203. try {
  1204. // Return '*' instead of '' so that truthiness works.
  1205. // This will throw if it's invalid anyway
  1206. return new Range(range, options).range || '*'
  1207. } catch (er) {
  1208. return null
  1209. }
  1210. }
  1211. // Determine if version is less than all the versions possible in the range
  1212. exports.ltr = ltr
  1213. function ltr (version, range, options) {
  1214. return outside(version, range, '<', options)
  1215. }
  1216. // Determine if version is greater than all the versions possible in the range.
  1217. exports.gtr = gtr
  1218. function gtr (version, range, options) {
  1219. return outside(version, range, '>', options)
  1220. }
  1221. exports.outside = outside
  1222. function outside (version, range, hilo, options) {
  1223. version = new SemVer(version, options)
  1224. range = new Range(range, options)
  1225. var gtfn, ltefn, ltfn, comp, ecomp
  1226. switch (hilo) {
  1227. case '>':
  1228. gtfn = gt
  1229. ltefn = lte
  1230. ltfn = lt
  1231. comp = '>'
  1232. ecomp = '>='
  1233. break
  1234. case '<':
  1235. gtfn = lt
  1236. ltefn = gte
  1237. ltfn = gt
  1238. comp = '<'
  1239. ecomp = '<='
  1240. break
  1241. default:
  1242. throw new TypeError('Must provide a hilo val of "<" or ">"')
  1243. }
  1244. // If it satisifes the range it is not outside
  1245. if (satisfies(version, range, options)) {
  1246. return false
  1247. }
  1248. // From now on, variable terms are as if we're in "gtr" mode.
  1249. // but note that everything is flipped for the "ltr" function.
  1250. for (var i = 0; i < range.set.length; ++i) {
  1251. var comparators = range.set[i]
  1252. var high = null
  1253. var low = null
  1254. comparators.forEach(function (comparator) {
  1255. if (comparator.semver === ANY) {
  1256. comparator = new Comparator('>=0.0.0')
  1257. }
  1258. high = high || comparator
  1259. low = low || comparator
  1260. if (gtfn(comparator.semver, high.semver, options)) {
  1261. high = comparator
  1262. } else if (ltfn(comparator.semver, low.semver, options)) {
  1263. low = comparator
  1264. }
  1265. })
  1266. // If the edge version comparator has a operator then our version
  1267. // isn't outside it
  1268. if (high.operator === comp || high.operator === ecomp) {
  1269. return false
  1270. }
  1271. // If the lowest version comparator has an operator and our version
  1272. // is less than it then it isn't higher than the range
  1273. if ((!low.operator || low.operator === comp) &&
  1274. ltefn(version, low.semver)) {
  1275. return false
  1276. } else if (low.operator === ecomp && ltfn(version, low.semver)) {
  1277. return false
  1278. }
  1279. }
  1280. return true
  1281. }
  1282. exports.prerelease = prerelease
  1283. function prerelease (version, options) {
  1284. var parsed = parse(version, options)
  1285. return (parsed && parsed.prerelease.length) ? parsed.prerelease : null
  1286. }
  1287. exports.intersects = intersects
  1288. function intersects (r1, r2, options) {
  1289. r1 = new Range(r1, options)
  1290. r2 = new Range(r2, options)
  1291. return r1.intersects(r2)
  1292. }
  1293. exports.coerce = coerce
  1294. function coerce (version) {
  1295. if (version instanceof SemVer) {
  1296. return version
  1297. }
  1298. if (typeof version !== 'string') {
  1299. return null
  1300. }
  1301. var match = version.match(safeRe[COERCE])
  1302. if (match == null) {
  1303. return null
  1304. }
  1305. return parse(match[1] +
  1306. '.' + (match[2] || '0') +
  1307. '.' + (match[3] || '0'))
  1308. }