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.

README.md 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # sver-compat
  2. [![Build Status](https://travis-ci.org/phated/sver-compat.svg?branch=master)](https://travis-ci.org/phated/sver-compat)
  3. Fork of @guybedford's [Sver](https://github.com/guybedford/sver) library. Adds compatibility for node <6.
  4. ```
  5. npm install sver-compat
  6. ```
  7. ```js
  8. var Semver = require('sver-compat').Semver;
  9. var SemverRange = require('sver-compat').SemverRange;
  10. // Static usage:
  11. SemverRange.match('^1.2.3', '1.2.4'); // true
  12. // Class usage:
  13. var range = new SemverRange('^1.2.3');
  14. var version = new Semver('1.2.4');
  15. version.matches(range); // true
  16. range.has(version); // true
  17. ```
  18. ### Range support
  19. Restricts version ranges to the simplified cases:
  20. * `*`: Wildcard range
  21. * `MAJOR`: Match exact major
  22. * `MAJOR.MINOR` Match exact major and minor
  23. * `MAJOR.MINOR.PATCH[-PRE]` Match exact semver
  24. * `~MAJOR.MINOR.PATCH[-PRE]`: Match patch bumps
  25. * `^MAJOR.MINOR.PATCH[-PRE]`: Match minor and patch bumps
  26. Invalid ranges will fallback to being detected as exact string matches.
  27. ### Prerelease Matching
  28. By default, as per convention, ranges like `^1.2.3-alpha` only match prerelease ranges on the same patch (`1.2.3-alpha.4`), but
  29. not prerelease ranges from further patches (`1.3.4-alpha`).
  30. To alter this matching, a third boolean argument can be provided to the match function to support these unstable matches:
  31. ```js
  32. SemverRange.match('^1.2.3', '1.5.6-beta'); // false
  33. SemverRange.match('^1.2.3', '1.5.6-beta', true); // true
  34. ```
  35. ### Best Version Match
  36. ```js
  37. var versions = ['1.2.3', '1.3.4-alpha', '1.3.4-alpha.1', '1.3.4-beta'];
  38. var range = new SemverRange('*');
  39. var bestStableMatch = range.bestMatch(versions);
  40. bestStableMatch.toString(); // 1.2.3
  41. var bestUnstableMatch = range.bestMatch(versions, true);
  42. bestUnstableMatch.toString(); // 1.3.4-beta
  43. ```
  44. ### Version and Range Sorting
  45. ```js
  46. var versions = ['2.4.5', '2.3.4-alpha', '1.2.3', '2.3.4-alpha.2'];
  47. var ranges = ['^1.2.3', '1.2', '2.3.4'];
  48. versions.sort(Semver.compare); // [1.2.3, 2.3.4-alpha, 2.3.4-alpha.2, 2.4.5]
  49. ranges.sort(SemverRange.compare) // [1.2, ^1.2.3, 2.3.4]
  50. ```
  51. ### Conversion from Node Semver Ranges
  52. A utility function is included to convert Node Semver ranges into Semver ranges.
  53. This requires `semver` to be installed in the application running this process.
  54. _Note this conversion is lossy by definition._
  55. ```js
  56. var convertRange = require('sver-compat/convert-range');
  57. convertRange('>=2.3.4 <3.0.0').toString(); // ^2.3.4
  58. convertRange('1 || 2 || 3').toString(); // ^3.0.0
  59. ```
  60. ### Semver and Semver Range Validation
  61. When a version string fails semver validation it falls back to being treated as a tag, still as a `Semver` instance.
  62. For example:
  63. ```js
  64. var version = new Semver('x.y.z');
  65. version.tag === 'x.y.z'; // true
  66. version = new Semver('^1.2.3');
  67. version.major === undefined; // true
  68. version.tag === '^1.2.3'; // true
  69. ```
  70. For validation, rather use `Semver.isValid` and `SemverRange.isValid`:
  71. ```js
  72. Semver.isValid('x.y.z'); // false
  73. Semver.isValid('^1.2.3'); // false
  74. SemverRange.isValid('^1.2.3'); // true
  75. ```
  76. ## API
  77. ### Semver
  78. Static methods:
  79. * `Semver.isValid(version: string): boolean`: Whether the given string is a valid semver.
  80. * `Semver.compare(v1: Semver|string, v2: Semver|string): number`: 1 if v1 > v2, -1 if v1 < v2, 0 if equal.
  81. For a given Semver instance `version = new Semver('X.Y.Z')`,
  82. * `version.major`: The major version number.
  83. * `version.minor`: The minor version number.
  84. * `version.patch`: The patch version number.
  85. * `version.pre`: The prerelease identifer, as an array of strings (`.`-separated).
  86. * `version.build`: The build identifier, as a string.
  87. * `version.tag`: If not a valid semver, the full tag string.
  88. * `version.gt(otherVersion: Semver|string): bool`: Whether this version is greater than the other version.
  89. * `version.lt(otherVersion: Semver|string): bool`: Whether this version is less than the other version.
  90. * `version.eq(otherVerion: Semver|string): bool`: Whether this version equals the other version.
  91. * `version.matches(range: SemverRange|string, unstable?: bool): bool`: Whether this version matches the given version range.
  92. * `version.toString(): string`: Convert the version back to a string.
  93. ### SemverRange
  94. Static methods:
  95. * `SemverRange.match(range: SemverRange|string, version: Semver|string, unstable = false): bool`: Whether the version matches the range.
  96. * `SemverRange.isValid(range: string): bool`: Whether the given range string is a valid semver range (in this simplified grammar).
  97. * `SemverRange.compare(r1: SemverRange|string, r2: SemverRange|string): number`: 1 if r1 > r2, -1 if r1 < r2, 0 if equal.
  98. For a given SemverRange instance `range = new SemverRange('^X.Y.Z')`,
  99. * `range.type: string`: Returns `'wildcard'`, `'major'`, `'stable'` or `'exact'`.
  100. * `range.version: Smever`: Returns the `Semver` instance corresponding to the range.
  101. * `range.isExact: string`: Returns true if the range is an exact version only.
  102. * `range.isStable: string`: Returns true if the range is a stable version range.
  103. * `range.isMajor: string`: Returns true if the range is a major version range.
  104. * `range.isWildcard: string`: Returns true if the range is the wildcard version range.
  105. * `range.gt(otherRange: SemverRange|string): bool`: Whether the range is greater than the other range.
  106. * `range.lt(otherRange: SemverRange|string): bool`: Whether the range is less than the other range.
  107. * `range.eq(otherRange: SemverRange|string): bool`: Whether the range is exactly the same as the other range.
  108. * `range.has(version: Semver|string, unstable = false): bool`: Whether the range includes the given version.
  109. * `range.contains(otherRange: SemverRange|string): bool`: Whether the range fully contains the other range.
  110. * `range.intersect(otherRange: SemverRange|string): SemverRange|undefined`: The intersection range, if any.
  111. * `range.bestMatch(versions: (Semver|string)[], unstable = false): Semver|undefined`: The intersection range, if any.
  112. * `range.toString()`: Convert the range back to a string.
  113. ## License
  114. MIT