5d9e1f680a30a9d792715152bf64152510c655e93d735869623d7ad69b2a1945eeaeee0a6b6058da83ff69e73fac0bd871296af6adecfc56dcf7aa6a73b6e2 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. semver(1) -- The semantic versioner for npm
  2. ===========================================
  3. ## Install
  4. ```bash
  5. npm install semver
  6. ````
  7. ## Usage
  8. As a node module:
  9. ```js
  10. const semver = require('semver')
  11. semver.valid('1.2.3') // '1.2.3'
  12. semver.valid('a.b.c') // null
  13. semver.clean(' =v1.2.3 ') // '1.2.3'
  14. semver.satisfies('1.2.3', '1.x || >=2.5.0 || 5.0.0 - 7.2.3') // true
  15. semver.gt('1.2.3', '9.8.7') // false
  16. semver.lt('1.2.3', '9.8.7') // true
  17. semver.minVersion('>=1.0.0') // '1.0.0'
  18. semver.valid(semver.coerce('v2')) // '2.0.0'
  19. semver.valid(semver.coerce('42.6.7.9.3-alpha')) // '42.6.7'
  20. ```
  21. You can also just load the module for the function that you care about if
  22. you'd like to minimize your footprint.
  23. ```js
  24. // load the whole API at once in a single object
  25. const semver = require('semver')
  26. // or just load the bits you need
  27. // all of them listed here, just pick and choose what you want
  28. // classes
  29. const SemVer = require('semver/classes/semver')
  30. const Comparator = require('semver/classes/comparator')
  31. const Range = require('semver/classes/range')
  32. // functions for working with versions
  33. const semverParse = require('semver/functions/parse')
  34. const semverValid = require('semver/functions/valid')
  35. const semverClean = require('semver/functions/clean')
  36. const semverInc = require('semver/functions/inc')
  37. const semverDiff = require('semver/functions/diff')
  38. const semverMajor = require('semver/functions/major')
  39. const semverMinor = require('semver/functions/minor')
  40. const semverPatch = require('semver/functions/patch')
  41. const semverPrerelease = require('semver/functions/prerelease')
  42. const semverCompare = require('semver/functions/compare')
  43. const semverRcompare = require('semver/functions/rcompare')
  44. const semverCompareLoose = require('semver/functions/compare-loose')
  45. const semverCompareBuild = require('semver/functions/compare-build')
  46. const semverSort = require('semver/functions/sort')
  47. const semverRsort = require('semver/functions/rsort')
  48. // low-level comparators between versions
  49. const semverGt = require('semver/functions/gt')
  50. const semverLt = require('semver/functions/lt')
  51. const semverEq = require('semver/functions/eq')
  52. const semverNeq = require('semver/functions/neq')
  53. const semverGte = require('semver/functions/gte')
  54. const semverLte = require('semver/functions/lte')
  55. const semverCmp = require('semver/functions/cmp')
  56. const semverCoerce = require('semver/functions/coerce')
  57. // working with ranges
  58. const semverSatisfies = require('semver/functions/satisfies')
  59. const semverMaxSatisfying = require('semver/ranges/max-satisfying')
  60. const semverMinSatisfying = require('semver/ranges/min-satisfying')
  61. const semverToComparators = require('semver/ranges/to-comparators')
  62. const semverMinVersion = require('semver/ranges/min-version')
  63. const semverValidRange = require('semver/ranges/valid')
  64. const semverOutside = require('semver/ranges/outside')
  65. const semverGtr = require('semver/ranges/gtr')
  66. const semverLtr = require('semver/ranges/ltr')
  67. const semverIntersects = require('semver/ranges/intersects')
  68. const semverSimplifyRange = require('semver/ranges/simplify')
  69. const semverRangeSubset = require('semver/ranges/subset')
  70. ```
  71. As a command-line utility:
  72. ```
  73. $ semver -h
  74. A JavaScript implementation of the https://semver.org/ specification
  75. Copyright Isaac Z. Schlueter
  76. Usage: semver [options] <version> [<version> [...]]
  77. Prints valid versions sorted by SemVer precedence
  78. Options:
  79. -r --range <range>
  80. Print versions that match the specified range.
  81. -i --increment [<level>]
  82. Increment a version by the specified level. Level can
  83. be one of: major, minor, patch, premajor, preminor,
  84. prepatch, prerelease, or release. Default level is 'patch'.
  85. Only one version may be specified.
  86. --preid <identifier>
  87. Identifier to be used to prefix premajor, preminor,
  88. prepatch or prerelease version increments.
  89. -l --loose
  90. Interpret versions and ranges loosely
  91. -n <0|1>
  92. This is the base to be used for the prerelease identifier.
  93. -p --include-prerelease
  94. Always include prerelease versions in range matching
  95. -c --coerce
  96. Coerce a string into SemVer if possible
  97. (does not imply --loose)
  98. --rtl
  99. Coerce version strings right to left
  100. --ltr
  101. Coerce version strings left to right (default)
  102. Program exits successfully if any valid version satisfies
  103. all supplied ranges, and prints all satisfying versions.
  104. If no satisfying versions are found, then exits failure.
  105. Versions are printed in ascending order, so supplying
  106. multiple versions to the utility will just sort them.
  107. ```
  108. ## Versions
  109. A "version" is described by the `v2.0.0` specification found at
  110. <https://semver.org/>.
  111. A leading `"="` or `"v"` character is stripped off and ignored.
  112. Support for stripping a leading "v" is kept for compatibility with `v1.0.0` of the SemVer
  113. specification but should not be used anymore.
  114. ## Ranges
  115. A `version range` is a set of `comparators` that specify versions
  116. that satisfy the range.
  117. A `comparator` is composed of an `operator` and a `version`. The set
  118. of primitive `operators` is:
  119. * `<` Less than
  120. * `<=` Less than or equal to
  121. * `>` Greater than
  122. * `>=` Greater than or equal to
  123. * `=` Equal. If no operator is specified, then equality is assumed,
  124. so this operator is optional but MAY be included.
  125. For example, the comparator `>=1.2.7` would match the versions
  126. `1.2.7`, `1.2.8`, `2.5.3`, and `1.3.9`, but not the versions `1.2.6`
  127. or `1.1.0`. The comparator `>1` is equivalent to `>=2.0.0` and
  128. would match the versions `2.0.0` and `3.1.0`, but not the versions
  129. `1.0.1` or `1.1.0`.
  130. Comparators can be joined by whitespace to form a `comparator set`,
  131. which is satisfied by the **intersection** of all of the comparators
  132. it includes.
  133. A range is composed of one or more comparator sets, joined by `||`. A
  134. version matches a range if and only if every comparator in at least
  135. one of the `||`-separated comparator sets is satisfied by the version.
  136. For example, the range `>=1.2.7 <1.3.0` would match the versions
  137. `1.2.7`, `1.2.8`, and `1.2.99`, but not the versions `1.2.6`, `1.3.0`,
  138. or `1.1.0`.
  139. The range `1.2.7 || >=1.2.9 <2.0.0` would match the versions `1.2.7`,
  140. `1.2.9`, and `1.4.6`, but not the versions `1.2.8` or `2.0.0`.
  141. ### Prerelease Tags
  142. If a version has a prerelease tag (for example, `1.2.3-alpha.3`) then
  143. it will only be allowed to satisfy comparator sets if at least one
  144. comparator with the same `[major, minor, patch]` tuple also has a
  145. prerelease tag.
  146. For example, the range `>1.2.3-alpha.3` would be allowed to match the
  147. version `1.2.3-alpha.7`, but it would *not* be satisfied by
  148. `3.4.5-alpha.9`, even though `3.4.5-alpha.9` is technically "greater
  149. than" `1.2.3-alpha.3` according to the SemVer sort rules. The version
  150. range only accepts prerelease tags on the `1.2.3` version.
  151. Version `3.4.5` *would* satisfy the range because it does not have a
  152. prerelease flag, and `3.4.5` is greater than `1.2.3-alpha.7`.
  153. The purpose of this behavior is twofold. First, prerelease versions
  154. frequently are updated very quickly, and contain many breaking changes
  155. that are (by the author's design) not yet fit for public consumption.
  156. Therefore, by default, they are excluded from range-matching
  157. semantics.
  158. Second, a user who has opted into using a prerelease version has
  159. indicated the intent to use *that specific* set of
  160. alpha/beta/rc versions. By including a prerelease tag in the range,
  161. the user is indicating that they are aware of the risk. However, it
  162. is still not appropriate to assume that they have opted into taking a
  163. similar risk on the *next* set of prerelease versions.
  164. Note that this behavior can be suppressed (treating all prerelease
  165. versions as if they were normal versions, for range-matching)
  166. by setting the `includePrerelease` flag on the options
  167. object to any
  168. [functions](https://github.com/npm/node-semver#functions) that do
  169. range matching.
  170. #### Prerelease Identifiers
  171. The method `.inc` takes an additional `identifier` string argument that
  172. will append the value of the string as a prerelease identifier:
  173. ```javascript
  174. semver.inc('1.2.3', 'prerelease', 'beta')
  175. // '1.2.4-beta.0'
  176. ```
  177. command-line example:
  178. ```bash
  179. $ semver 1.2.3 -i prerelease --preid beta
  180. 1.2.4-beta.0
  181. ```
  182. Which then can be used to increment further:
  183. ```bash
  184. $ semver 1.2.4-beta.0 -i prerelease
  185. 1.2.4-beta.1
  186. ```
  187. To get out of the prerelease phase, use the `release` option:
  188. ```bash
  189. $ semver 1.2.4-beta.1 -i release
  190. 1.2.4
  191. ```
  192. #### Prerelease Identifier Base
  193. The method `.inc` takes an optional parameter 'identifierBase' string
  194. that will let you let your prerelease number as zero-based or one-based.
  195. Set to `false` to omit the prerelease number altogether.
  196. If you do not specify this parameter, it will default to zero-based.
  197. ```javascript
  198. semver.inc('1.2.3', 'prerelease', 'beta', '1')
  199. // '1.2.4-beta.1'
  200. ```
  201. ```javascript
  202. semver.inc('1.2.3', 'prerelease', 'beta', false)
  203. // '1.2.4-beta'
  204. ```
  205. command-line example:
  206. ```bash
  207. $ semver 1.2.3 -i prerelease --preid beta -n 1
  208. 1.2.4-beta.1
  209. ```
  210. ```bash
  211. $ semver 1.2.3 -i prerelease --preid beta -n false
  212. 1.2.4-beta
  213. ```
  214. ### Advanced Range Syntax
  215. Advanced range syntax desugars to primitive comparators in
  216. deterministic ways.
  217. Advanced ranges may be combined in the same way as primitive
  218. comparators using white space or `||`.
  219. #### Hyphen Ranges `X.Y.Z - A.B.C`
  220. Specifies an inclusive set.
  221. * `1.2.3 - 2.3.4` := `>=1.2.3 <=2.3.4`
  222. If a partial version is provided as the first version in the inclusive
  223. range, then the missing pieces are replaced with zeroes.
  224. * `1.2 - 2.3.4` := `>=1.2.0 <=2.3.4`
  225. If a partial version is provided as the second version in the
  226. inclusive range, then all versions that start with the supplied parts
  227. of the tuple are accepted, but nothing that would be greater than the
  228. provided tuple parts.
  229. * `1.2.3 - 2.3` := `>=1.2.3 <2.4.0-0`
  230. * `1.2.3 - 2` := `>=1.2.3 <3.0.0-0`
  231. #### X-Ranges `1.2.x` `1.X` `1.2.*` `*`
  232. Any of `X`, `x`, or `*` may be used to "stand in" for one of the
  233. numeric values in the `[major, minor, patch]` tuple.
  234. * `*` := `>=0.0.0` (Any non-prerelease version satisfies, unless
  235. `includePrerelease` is specified, in which case any version at all
  236. satisfies)
  237. * `1.x` := `>=1.0.0 <2.0.0-0` (Matching major version)
  238. * `1.2.x` := `>=1.2.0 <1.3.0-0` (Matching major and minor versions)
  239. A partial version range is treated as an X-Range, so the special
  240. character is in fact optional.
  241. * `""` (empty string) := `*` := `>=0.0.0`
  242. * `1` := `1.x.x` := `>=1.0.0 <2.0.0-0`
  243. * `1.2` := `1.2.x` := `>=1.2.0 <1.3.0-0`
  244. #### Tilde Ranges `~1.2.3` `~1.2` `~1`
  245. Allows patch-level changes if a minor version is specified on the
  246. comparator. Allows minor-level changes if not.
  247. * `~1.2.3` := `>=1.2.3 <1.(2+1).0` := `>=1.2.3 <1.3.0-0`
  248. * `~1.2` := `>=1.2.0 <1.(2+1).0` := `>=1.2.0 <1.3.0-0` (Same as `1.2.x`)
  249. * `~1` := `>=1.0.0 <(1+1).0.0` := `>=1.0.0 <2.0.0-0` (Same as `1.x`)
  250. * `~0.2.3` := `>=0.2.3 <0.(2+1).0` := `>=0.2.3 <0.3.0-0`
  251. * `~0.2` := `>=0.2.0 <0.(2+1).0` := `>=0.2.0 <0.3.0-0` (Same as `0.2.x`)
  252. * `~0` := `>=0.0.0 <(0+1).0.0` := `>=0.0.0 <1.0.0-0` (Same as `0.x`)
  253. * `~1.2.3-beta.2` := `>=1.2.3-beta.2 <1.3.0-0` Note that prereleases in
  254. the `1.2.3` version will be allowed, if they are greater than or
  255. equal to `beta.2`. So, `1.2.3-beta.4` would be allowed, but
  256. `1.2.4-beta.2` would not, because it is a prerelease of a
  257. different `[major, minor, patch]` tuple.
  258. #### Caret Ranges `^1.2.3` `^0.2.5` `^0.0.4`
  259. Allows changes that do not modify the left-most non-zero element in the
  260. `[major, minor, patch]` tuple. In other words, this allows patch and
  261. minor updates for versions `1.0.0` and above, patch updates for
  262. versions `0.X >=0.1.0`, and *no* updates for versions `0.0.X`.
  263. Many authors treat a `0.x` version as if the `x` were the major
  264. "breaking-change" indicator.
  265. Caret ranges are ideal when an author may make breaking changes
  266. between `0.2.4` and `0.3.0` releases, which is a common practice.
  267. However, it presumes that there will *not* be breaking changes between
  268. `0.2.4` and `0.2.5`. It allows for changes that are presumed to be
  269. additive (but non-breaking), according to commonly observed practices.
  270. * `^1.2.3` := `>=1.2.3 <2.0.0-0`
  271. * `^0.2.3` := `>=0.2.3 <0.3.0-0`
  272. * `^0.0.3` := `>=0.0.3 <0.0.4-0`
  273. * `^1.2.3-beta.2` := `>=1.2.3-beta.2 <2.0.0-0` Note that prereleases in
  274. the `1.2.3` version will be allowed, if they are greater than or
  275. equal to `beta.2`. So, `1.2.3-beta.4` would be allowed, but
  276. `1.2.4-beta.2` would not, because it is a prerelease of a
  277. different `[major, minor, patch]` tuple.
  278. * `^0.0.3-beta` := `>=0.0.3-beta <0.0.4-0` Note that prereleases in the
  279. `0.0.3` version *only* will be allowed, if they are greater than or
  280. equal to `beta`. So, `0.0.3-pr.2` would be allowed.
  281. When parsing caret ranges, a missing `patch` value desugars to the
  282. number `0`, but will allow flexibility within that value, even if the
  283. major and minor versions are both `0`.
  284. * `^1.2.x` := `>=1.2.0 <2.0.0-0`
  285. * `^0.0.x` := `>=0.0.0 <0.1.0-0`
  286. * `^0.0` := `>=0.0.0 <0.1.0-0`
  287. A missing `minor` and `patch` values will desugar to zero, but also
  288. allow flexibility within those values, even if the major version is
  289. zero.
  290. * `^1.x` := `>=1.0.0 <2.0.0-0`
  291. * `^0.x` := `>=0.0.0 <1.0.0-0`
  292. ### Range Grammar
  293. Putting all this together, here is a Backus-Naur grammar for ranges,
  294. for the benefit of parser authors:
  295. ```bnf
  296. range-set ::= range ( logical-or range ) *
  297. logical-or ::= ( ' ' ) * '||' ( ' ' ) *
  298. range ::= hyphen | simple ( ' ' simple ) * | ''
  299. hyphen ::= partial ' - ' partial
  300. simple ::= primitive | partial | tilde | caret
  301. primitive ::= ( '<' | '>' | '>=' | '<=' | '=' ) partial
  302. partial ::= xr ( '.' xr ( '.' xr qualifier ? )? )?
  303. xr ::= 'x' | 'X' | '*' | nr
  304. nr ::= '0' | ['1'-'9'] ( ['0'-'9'] ) *
  305. tilde ::= '~' partial
  306. caret ::= '^' partial
  307. qualifier ::= ( '-' pre )? ( '+' build )?
  308. pre ::= parts
  309. build ::= parts
  310. parts ::= part ( '.' part ) *
  311. part ::= nr | [-0-9A-Za-z]+
  312. ```
  313. ## Functions
  314. All methods and classes take a final `options` object argument. All
  315. options in this object are `false` by default. The options supported
  316. are:
  317. - `loose`: Be more forgiving about not-quite-valid semver strings.
  318. (Any resulting output will always be 100% strict compliant, of
  319. course.) For backwards compatibility reasons, if the `options`
  320. argument is a boolean value instead of an object, it is interpreted
  321. to be the `loose` param.
  322. - `includePrerelease`: Set to suppress the [default
  323. behavior](https://github.com/npm/node-semver#prerelease-tags) of
  324. excluding prerelease tagged versions from ranges unless they are
  325. explicitly opted into.
  326. Strict-mode Comparators and Ranges will be strict about the SemVer
  327. strings that they parse.
  328. * `valid(v)`: Return the parsed version, or null if it's not valid.
  329. * `inc(v, releaseType, options, identifier, identifierBase)`:
  330. Return the version incremented by the release
  331. type (`major`, `premajor`, `minor`, `preminor`, `patch`,
  332. `prepatch`, `prerelease`, or `release`), or null if it's not valid
  333. * `premajor` in one call will bump the version up to the next major
  334. version and down to a prerelease of that major version.
  335. `preminor`, and `prepatch` work the same way.
  336. * If called from a non-prerelease version, `prerelease` will work the
  337. same as `prepatch`. It increments the patch version and then makes a
  338. prerelease. If the input version is already a prerelease it simply
  339. increments it.
  340. * `release` will remove any prerelease part of the version.
  341. * `identifier` can be used to prefix `premajor`, `preminor`,
  342. `prepatch`, or `prerelease` version increments. `identifierBase`
  343. is the base to be used for the `prerelease` identifier.
  344. * `prerelease(v)`: Returns an array of prerelease components, or null
  345. if none exist. Example: `prerelease('1.2.3-alpha.1') -> ['alpha', 1]`
  346. * `major(v)`: Return the major version number.
  347. * `minor(v)`: Return the minor version number.
  348. * `patch(v)`: Return the patch version number.
  349. * `intersects(r1, r2, loose)`: Return true if the two supplied ranges
  350. or comparators intersect.
  351. * `parse(v)`: Attempt to parse a string as a semantic version, returning either
  352. a `SemVer` object or `null`.
  353. ### Comparison
  354. * `gt(v1, v2)`: `v1 > v2`
  355. * `gte(v1, v2)`: `v1 >= v2`
  356. * `lt(v1, v2)`: `v1 < v2`
  357. * `lte(v1, v2)`: `v1 <= v2`
  358. * `eq(v1, v2)`: `v1 == v2` This is true if they're logically equivalent,
  359. even if they're not the same string. You already know how to
  360. compare strings.
  361. * `neq(v1, v2)`: `v1 != v2` The opposite of `eq`.
  362. * `cmp(v1, comparator, v2)`: Pass in a comparison string, and it'll call
  363. the corresponding function above. `"==="` and `"!=="` do simple
  364. string comparison, but are included for completeness. Throws if an
  365. invalid comparison string is provided.
  366. * `compare(v1, v2)`: Return `0` if `v1 == v2`, or `1` if `v1` is greater, or `-1` if
  367. `v2` is greater. Sorts in ascending order if passed to `Array.sort()`.
  368. * `rcompare(v1, v2)`: The reverse of `compare`. Sorts an array of versions
  369. in descending order when passed to `Array.sort()`.
  370. * `compareBuild(v1, v2)`: The same as `compare` but considers `build` when two versions
  371. are equal. Sorts in ascending order if passed to `Array.sort()`.
  372. * `compareLoose(v1, v2)`: Short for `compare(v1, v2, { loose: true })`.
  373. * `diff(v1, v2)`: Returns the difference between two versions by the release type
  374. (`major`, `premajor`, `minor`, `preminor`, `patch`, `prepatch`, or `prerelease`),
  375. or null if the versions are the same.
  376. ### Sorting
  377. * `sort(versions)`: Returns a sorted array of versions based on the `compareBuild`
  378. function.
  379. * `rsort(versions)`: The reverse of `sort`. Returns an array of versions based on
  380. the `compareBuild` function in descending order.
  381. ### Comparators
  382. * `intersects(comparator)`: Return true if the comparators intersect
  383. ### Ranges
  384. * `validRange(range)`: Return the valid range or null if it's not valid.
  385. * `satisfies(version, range)`: Return true if the version satisfies the
  386. range.
  387. * `maxSatisfying(versions, range)`: Return the highest version in the list
  388. that satisfies the range, or `null` if none of them do.
  389. * `minSatisfying(versions, range)`: Return the lowest version in the list
  390. that satisfies the range, or `null` if none of them do.
  391. * `minVersion(range)`: Return the lowest version that can match
  392. the given range.
  393. * `gtr(version, range)`: Return `true` if the version is greater than all the
  394. versions possible in the range.
  395. * `ltr(version, range)`: Return `true` if the version is less than all the
  396. versions possible in the range.
  397. * `outside(version, range, hilo)`: Return true if the version is outside
  398. the bounds of the range in either the high or low direction. The
  399. `hilo` argument must be either the string `'>'` or `'<'`. (This is
  400. the function called by `gtr` and `ltr`.)
  401. * `intersects(range)`: Return true if any of the range comparators intersect.
  402. * `simplifyRange(versions, range)`: Return a "simplified" range that
  403. matches the same items in the `versions` list as the range specified. Note
  404. that it does *not* guarantee that it would match the same versions in all
  405. cases, only for the set of versions provided. This is useful when
  406. generating ranges by joining together multiple versions with `||`
  407. programmatically, to provide the user with something a bit more
  408. ergonomic. If the provided range is shorter in string-length than the
  409. generated range, then that is returned.
  410. * `subset(subRange, superRange)`: Return `true` if the `subRange` range is
  411. entirely contained by the `superRange` range.
  412. Note that, since ranges may be non-contiguous, a version might not be
  413. greater than a range, less than a range, *or* satisfy a range! For
  414. example, the range `1.2 <1.2.9 || >2.0.0` would have a hole from `1.2.9`
  415. until `2.0.0`, so version `1.2.10` would not be greater than the
  416. range (because `2.0.1` satisfies, which is higher), nor less than the
  417. range (since `1.2.8` satisfies, which is lower), and it also does not
  418. satisfy the range.
  419. If you want to know if a version satisfies or does not satisfy a
  420. range, use the `satisfies(version, range)` function.
  421. ### Coercion
  422. * `coerce(version, options)`: Coerces a string to semver if possible
  423. This aims to provide a very forgiving translation of a non-semver string to
  424. semver. It looks for the first digit in a string and consumes all
  425. remaining characters which satisfy at least a partial semver (e.g., `1`,
  426. `1.2`, `1.2.3`) up to the max permitted length (256 characters). Longer
  427. versions are simply truncated (`4.6.3.9.2-alpha2` becomes `4.6.3`). All
  428. surrounding text is simply ignored (`v3.4 replaces v3.3.1` becomes
  429. `3.4.0`). Only text which lacks digits will fail coercion (`version one`
  430. is not valid). The maximum length for any semver component considered for
  431. coercion is 16 characters; longer components will be ignored
  432. (`10000000000000000.4.7.4` becomes `4.7.4`). The maximum value for any
  433. semver component is `Number.MAX_SAFE_INTEGER || (2**53 - 1)`; higher value
  434. components are invalid (`9999999999999999.4.7.4` is likely invalid).
  435. If the `options.rtl` flag is set, then `coerce` will return the right-most
  436. coercible tuple that does not share an ending index with a longer coercible
  437. tuple. For example, `1.2.3.4` will return `2.3.4` in rtl mode, not
  438. `4.0.0`. `1.2.3/4` will return `4.0.0`, because the `4` is not a part of
  439. any other overlapping SemVer tuple.
  440. If the `options.includePrerelease` flag is set, then the `coerce` result will contain
  441. prerelease and build parts of a version. For example, `1.2.3.4-rc.1+rev.2`
  442. will preserve prerelease `rc.1` and build `rev.2` in the result.
  443. ### Clean
  444. * `clean(version)`: Clean a string to be a valid semver if possible
  445. This will return a cleaned and trimmed semver version. If the provided
  446. version is not valid a null will be returned. This does not work for
  447. ranges.
  448. ex.
  449. * `s.clean(' = v 2.1.5foo')`: `null`
  450. * `s.clean(' = v 2.1.5foo', { loose: true })`: `'2.1.5-foo'`
  451. * `s.clean(' = v 2.1.5-foo')`: `null`
  452. * `s.clean(' = v 2.1.5-foo', { loose: true })`: `'2.1.5-foo'`
  453. * `s.clean('=v2.1.5')`: `'2.1.5'`
  454. * `s.clean(' =v2.1.5')`: `'2.1.5'`
  455. * `s.clean(' 2.1.5 ')`: `'2.1.5'`
  456. * `s.clean('~1.0.0')`: `null`
  457. ## Constants
  458. As a convenience, helper constants are exported to provide information about what `node-semver` supports:
  459. ### `RELEASE_TYPES`
  460. - major
  461. - premajor
  462. - minor
  463. - preminor
  464. - patch
  465. - prepatch
  466. - prerelease
  467. ```
  468. const semver = require('semver');
  469. if (semver.RELEASE_TYPES.includes(arbitraryUserInput)) {
  470. console.log('This is a valid release type!');
  471. } else {
  472. console.warn('This is NOT a valid release type!');
  473. }
  474. ```
  475. ### `SEMVER_SPEC_VERSION`
  476. 2.0.0
  477. ```
  478. const semver = require('semver');
  479. console.log('We are currently using the semver specification version:', semver.SEMVER_SPEC_VERSION);
  480. ```
  481. ## Exported Modules
  482. <!--
  483. TODO: Make sure that all of these items are documented (classes aren't,
  484. eg), and then pull the module name into the documentation for that specific
  485. thing.
  486. -->
  487. You may pull in just the part of this semver utility that you need if you
  488. are sensitive to packing and tree-shaking concerns. The main
  489. `require('semver')` export uses getter functions to lazily load the parts
  490. of the API that are used.
  491. The following modules are available:
  492. * `require('semver')`
  493. * `require('semver/classes')`
  494. * `require('semver/classes/comparator')`
  495. * `require('semver/classes/range')`
  496. * `require('semver/classes/semver')`
  497. * `require('semver/functions/clean')`
  498. * `require('semver/functions/cmp')`
  499. * `require('semver/functions/coerce')`
  500. * `require('semver/functions/compare')`
  501. * `require('semver/functions/compare-build')`
  502. * `require('semver/functions/compare-loose')`
  503. * `require('semver/functions/diff')`
  504. * `require('semver/functions/eq')`
  505. * `require('semver/functions/gt')`
  506. * `require('semver/functions/gte')`
  507. * `require('semver/functions/inc')`
  508. * `require('semver/functions/lt')`
  509. * `require('semver/functions/lte')`
  510. * `require('semver/functions/major')`
  511. * `require('semver/functions/minor')`
  512. * `require('semver/functions/neq')`
  513. * `require('semver/functions/parse')`
  514. * `require('semver/functions/patch')`
  515. * `require('semver/functions/prerelease')`
  516. * `require('semver/functions/rcompare')`
  517. * `require('semver/functions/rsort')`
  518. * `require('semver/functions/satisfies')`
  519. * `require('semver/functions/sort')`
  520. * `require('semver/functions/valid')`
  521. * `require('semver/ranges/gtr')`
  522. * `require('semver/ranges/intersects')`
  523. * `require('semver/ranges/ltr')`
  524. * `require('semver/ranges/max-satisfying')`
  525. * `require('semver/ranges/min-satisfying')`
  526. * `require('semver/ranges/min-version')`
  527. * `require('semver/ranges/outside')`
  528. * `require('semver/ranges/simplify')`
  529. * `require('semver/ranges/subset')`
  530. * `require('semver/ranges/to-comparators')`
  531. * `require('semver/ranges/valid')`