You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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