semver.js 9.1 KB

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