semver.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. const debug = require('../internal/debug')
  2. const { MAX_LENGTH, MAX_SAFE_INTEGER } = require('../internal/constants')
  3. const { safeRe: re, t } = require('../internal/re')
  4. const parseOptions = require('../internal/parse-options')
  5. const { compareIdentifiers } = require('../internal/identifiers')
  6. class SemVer {
  7. constructor (version, options) {
  8. options = parseOptions(options)
  9. if (version instanceof SemVer) {
  10. if (version.loose === !!options.loose &&
  11. version.includePrerelease === !!options.includePrerelease) {
  12. return version
  13. } else {
  14. version = version.version
  15. }
  16. } else if (typeof version !== 'string') {
  17. throw new TypeError(`Invalid version. Must be a string. Got type "${typeof version}".`)
  18. }
  19. if (version.length > MAX_LENGTH) {
  20. throw new TypeError(
  21. `version is longer than ${MAX_LENGTH} characters`
  22. )
  23. }
  24. debug('SemVer', version, options)
  25. this.options = options
  26. this.loose = !!options.loose
  27. // this isn't actually relevant for versions, but keep it so that we
  28. // don't run into trouble passing this.options around.
  29. this.includePrerelease = !!options.includePrerelease
  30. const m = version.trim().match(options.loose ? re[t.LOOSE] : re[t.FULL])
  31. if (!m) {
  32. throw new TypeError(`Invalid Version: ${version}`)
  33. }
  34. this.raw = version
  35. // these are actually numbers
  36. this.major = +m[1]
  37. this.minor = +m[2]
  38. this.patch = +m[3]
  39. if (this.major > MAX_SAFE_INTEGER || this.major < 0) {
  40. throw new TypeError('Invalid major version')
  41. }
  42. if (this.minor > MAX_SAFE_INTEGER || this.minor < 0) {
  43. throw new TypeError('Invalid minor version')
  44. }
  45. if (this.patch > MAX_SAFE_INTEGER || this.patch < 0) {
  46. throw new TypeError('Invalid patch version')
  47. }
  48. // numberify any prerelease numeric ids
  49. if (!m[4]) {
  50. this.prerelease = []
  51. } else {
  52. this.prerelease = m[4].split('.').map((id) => {
  53. if (/^[0-9]+$/.test(id)) {
  54. const num = +id
  55. if (num >= 0 && num < MAX_SAFE_INTEGER) {
  56. return num
  57. }
  58. }
  59. return id
  60. })
  61. }
  62. this.build = m[5] ? m[5].split('.') : []
  63. this.format()
  64. }
  65. format () {
  66. this.version = `${this.major}.${this.minor}.${this.patch}`
  67. if (this.prerelease.length) {
  68. this.version += `-${this.prerelease.join('.')}`
  69. }
  70. return this.version
  71. }
  72. toString () {
  73. return this.version
  74. }
  75. compare (other) {
  76. debug('SemVer.compare', this.version, this.options, other)
  77. if (!(other instanceof SemVer)) {
  78. if (typeof other === 'string' && other === this.version) {
  79. return 0
  80. }
  81. other = new SemVer(other, this.options)
  82. }
  83. if (other.version === this.version) {
  84. return 0
  85. }
  86. return this.compareMain(other) || this.comparePre(other)
  87. }
  88. compareMain (other) {
  89. if (!(other instanceof SemVer)) {
  90. other = new SemVer(other, this.options)
  91. }
  92. return (
  93. compareIdentifiers(this.major, other.major) ||
  94. compareIdentifiers(this.minor, other.minor) ||
  95. compareIdentifiers(this.patch, other.patch)
  96. )
  97. }
  98. comparePre (other) {
  99. if (!(other instanceof SemVer)) {
  100. other = new SemVer(other, this.options)
  101. }
  102. // NOT having a prerelease is > having one
  103. if (this.prerelease.length && !other.prerelease.length) {
  104. return -1
  105. } else if (!this.prerelease.length && other.prerelease.length) {
  106. return 1
  107. } else if (!this.prerelease.length && !other.prerelease.length) {
  108. return 0
  109. }
  110. let i = 0
  111. do {
  112. const a = this.prerelease[i]
  113. const b = other.prerelease[i]
  114. debug('prerelease compare', i, a, b)
  115. if (a === undefined && b === undefined) {
  116. return 0
  117. } else if (b === undefined) {
  118. return 1
  119. } else if (a === undefined) {
  120. return -1
  121. } else if (a === b) {
  122. continue
  123. } else {
  124. return compareIdentifiers(a, b)
  125. }
  126. } while (++i)
  127. }
  128. compareBuild (other) {
  129. if (!(other instanceof SemVer)) {
  130. other = new SemVer(other, this.options)
  131. }
  132. let i = 0
  133. do {
  134. const a = this.build[i]
  135. const b = other.build[i]
  136. debug('build compare', i, a, b)
  137. if (a === undefined && b === undefined) {
  138. return 0
  139. } else if (b === undefined) {
  140. return 1
  141. } else if (a === undefined) {
  142. return -1
  143. } else if (a === b) {
  144. continue
  145. } else {
  146. return compareIdentifiers(a, b)
  147. }
  148. } while (++i)
  149. }
  150. // preminor will bump the version up to the next minor release, and immediately
  151. // down to pre-release. premajor and prepatch work the same way.
  152. inc (release, identifier, identifierBase) {
  153. switch (release) {
  154. case 'premajor':
  155. this.prerelease.length = 0
  156. this.patch = 0
  157. this.minor = 0
  158. this.major++
  159. this.inc('pre', identifier, identifierBase)
  160. break
  161. case 'preminor':
  162. this.prerelease.length = 0
  163. this.patch = 0
  164. this.minor++
  165. this.inc('pre', identifier, identifierBase)
  166. break
  167. case 'prepatch':
  168. // If this is already a prerelease, it will bump to the next version
  169. // drop any prereleases that might already exist, since they are not
  170. // relevant at this point.
  171. this.prerelease.length = 0
  172. this.inc('patch', identifier, identifierBase)
  173. this.inc('pre', identifier, identifierBase)
  174. break
  175. // If the input is a non-prerelease version, this acts the same as
  176. // prepatch.
  177. case 'prerelease':
  178. if (this.prerelease.length === 0) {
  179. this.inc('patch', identifier, identifierBase)
  180. }
  181. this.inc('pre', identifier, identifierBase)
  182. break
  183. case 'major':
  184. // If this is a pre-major version, bump up to the same major version.
  185. // Otherwise increment major.
  186. // 1.0.0-5 bumps to 1.0.0
  187. // 1.1.0 bumps to 2.0.0
  188. if (
  189. this.minor !== 0 ||
  190. this.patch !== 0 ||
  191. this.prerelease.length === 0
  192. ) {
  193. this.major++
  194. }
  195. this.minor = 0
  196. this.patch = 0
  197. this.prerelease = []
  198. break
  199. case 'minor':
  200. // If this is a pre-minor version, bump up to the same minor version.
  201. // Otherwise increment minor.
  202. // 1.2.0-5 bumps to 1.2.0
  203. // 1.2.1 bumps to 1.3.0
  204. if (this.patch !== 0 || this.prerelease.length === 0) {
  205. this.minor++
  206. }
  207. this.patch = 0
  208. this.prerelease = []
  209. break
  210. case 'patch':
  211. // If this is not a pre-release version, it will increment the patch.
  212. // If it is a pre-release it will bump up to the same patch version.
  213. // 1.2.0-5 patches to 1.2.0
  214. // 1.2.0 patches to 1.2.1
  215. if (this.prerelease.length === 0) {
  216. this.patch++
  217. }
  218. this.prerelease = []
  219. break
  220. // This probably shouldn't be used publicly.
  221. // 1.0.0 'pre' would become 1.0.0-0 which is the wrong direction.
  222. case 'pre': {
  223. const base = Number(identifierBase) ? 1 : 0
  224. if (!identifier && identifierBase === false) {
  225. throw new Error('invalid increment argument: identifier is empty')
  226. }
  227. if (this.prerelease.length === 0) {
  228. this.prerelease = [base]
  229. } else {
  230. let i = this.prerelease.length
  231. while (--i >= 0) {
  232. if (typeof this.prerelease[i] === 'number') {
  233. this.prerelease[i]++
  234. i = -2
  235. }
  236. }
  237. if (i === -1) {
  238. // didn't increment anything
  239. if (identifier === this.prerelease.join('.') && identifierBase === false) {
  240. throw new Error('invalid increment argument: identifier already exists')
  241. }
  242. this.prerelease.push(base)
  243. }
  244. }
  245. if (identifier) {
  246. // 1.2.0-beta.1 bumps to 1.2.0-beta.2,
  247. // 1.2.0-beta.fooblz or 1.2.0-beta bumps to 1.2.0-beta.0
  248. let prerelease = [identifier, base]
  249. if (identifierBase === false) {
  250. prerelease = [identifier]
  251. }
  252. if (compareIdentifiers(this.prerelease[0], identifier) === 0) {
  253. if (isNaN(this.prerelease[1])) {
  254. this.prerelease = prerelease
  255. }
  256. } else {
  257. this.prerelease = prerelease
  258. }
  259. }
  260. break
  261. }
  262. default:
  263. throw new Error(`invalid increment argument: ${release}`)
  264. }
  265. this.raw = this.format()
  266. if (this.build.length) {
  267. this.raw += `+${this.build.join('.')}`
  268. }
  269. return this
  270. }
  271. }
  272. module.exports = SemVer