semver.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. if (this.major < other.major) {
  94. return -1
  95. }
  96. if (this.major > other.major) {
  97. return 1
  98. }
  99. if (this.minor < other.minor) {
  100. return -1
  101. }
  102. if (this.minor > other.minor) {
  103. return 1
  104. }
  105. if (this.patch < other.patch) {
  106. return -1
  107. }
  108. if (this.patch > other.patch) {
  109. return 1
  110. }
  111. return 0
  112. }
  113. comparePre (other) {
  114. if (!(other instanceof SemVer)) {
  115. other = new SemVer(other, this.options)
  116. }
  117. // NOT having a prerelease is > having one
  118. if (this.prerelease.length && !other.prerelease.length) {
  119. return -1
  120. } else if (!this.prerelease.length && other.prerelease.length) {
  121. return 1
  122. } else if (!this.prerelease.length && !other.prerelease.length) {
  123. return 0
  124. }
  125. let i = 0
  126. do {
  127. const a = this.prerelease[i]
  128. const b = other.prerelease[i]
  129. debug('prerelease compare', i, a, b)
  130. if (a === undefined && b === undefined) {
  131. return 0
  132. } else if (b === undefined) {
  133. return 1
  134. } else if (a === undefined) {
  135. return -1
  136. } else if (a === b) {
  137. continue
  138. } else {
  139. return compareIdentifiers(a, b)
  140. }
  141. } while (++i)
  142. }
  143. compareBuild (other) {
  144. if (!(other instanceof SemVer)) {
  145. other = new SemVer(other, this.options)
  146. }
  147. let i = 0
  148. do {
  149. const a = this.build[i]
  150. const b = other.build[i]
  151. debug('build compare', i, a, b)
  152. if (a === undefined && b === undefined) {
  153. return 0
  154. } else if (b === undefined) {
  155. return 1
  156. } else if (a === undefined) {
  157. return -1
  158. } else if (a === b) {
  159. continue
  160. } else {
  161. return compareIdentifiers(a, b)
  162. }
  163. } while (++i)
  164. }
  165. // preminor will bump the version up to the next minor release, and immediately
  166. // down to pre-release. premajor and prepatch work the same way.
  167. inc (release, identifier, identifierBase) {
  168. if (release.startsWith('pre')) {
  169. if (!identifier && identifierBase === false) {
  170. throw new Error('invalid increment argument: identifier is empty')
  171. }
  172. // Avoid an invalid semver results
  173. if (identifier) {
  174. const match = `-${identifier}`.match(this.options.loose ? re[t.PRERELEASELOOSE] : re[t.PRERELEASE])
  175. if (!match || match[1] !== identifier) {
  176. throw new Error(`invalid identifier: ${identifier}`)
  177. }
  178. }
  179. }
  180. switch (release) {
  181. case 'premajor':
  182. this.prerelease.length = 0
  183. this.patch = 0
  184. this.minor = 0
  185. this.major++
  186. this.inc('pre', identifier, identifierBase)
  187. break
  188. case 'preminor':
  189. this.prerelease.length = 0
  190. this.patch = 0
  191. this.minor++
  192. this.inc('pre', identifier, identifierBase)
  193. break
  194. case 'prepatch':
  195. // If this is already a prerelease, it will bump to the next version
  196. // drop any prereleases that might already exist, since they are not
  197. // relevant at this point.
  198. this.prerelease.length = 0
  199. this.inc('patch', identifier, identifierBase)
  200. this.inc('pre', identifier, identifierBase)
  201. break
  202. // If the input is a non-prerelease version, this acts the same as
  203. // prepatch.
  204. case 'prerelease':
  205. if (this.prerelease.length === 0) {
  206. this.inc('patch', identifier, identifierBase)
  207. }
  208. this.inc('pre', identifier, identifierBase)
  209. break
  210. case 'release':
  211. if (this.prerelease.length === 0) {
  212. throw new Error(`version ${this.raw} is not a prerelease`)
  213. }
  214. this.prerelease.length = 0
  215. break
  216. case 'major':
  217. // If this is a pre-major version, bump up to the same major version.
  218. // Otherwise increment major.
  219. // 1.0.0-5 bumps to 1.0.0
  220. // 1.1.0 bumps to 2.0.0
  221. if (
  222. this.minor !== 0 ||
  223. this.patch !== 0 ||
  224. this.prerelease.length === 0
  225. ) {
  226. this.major++
  227. }
  228. this.minor = 0
  229. this.patch = 0
  230. this.prerelease = []
  231. break
  232. case 'minor':
  233. // If this is a pre-minor version, bump up to the same minor version.
  234. // Otherwise increment minor.
  235. // 1.2.0-5 bumps to 1.2.0
  236. // 1.2.1 bumps to 1.3.0
  237. if (this.patch !== 0 || this.prerelease.length === 0) {
  238. this.minor++
  239. }
  240. this.patch = 0
  241. this.prerelease = []
  242. break
  243. case 'patch':
  244. // If this is not a pre-release version, it will increment the patch.
  245. // If it is a pre-release it will bump up to the same patch version.
  246. // 1.2.0-5 patches to 1.2.0
  247. // 1.2.0 patches to 1.2.1
  248. if (this.prerelease.length === 0) {
  249. this.patch++
  250. }
  251. this.prerelease = []
  252. break
  253. // This probably shouldn't be used publicly.
  254. // 1.0.0 'pre' would become 1.0.0-0 which is the wrong direction.
  255. case 'pre': {
  256. const base = Number(identifierBase) ? 1 : 0
  257. if (this.prerelease.length === 0) {
  258. this.prerelease = [base]
  259. } else {
  260. let i = this.prerelease.length
  261. while (--i >= 0) {
  262. if (typeof this.prerelease[i] === 'number') {
  263. this.prerelease[i]++
  264. i = -2
  265. }
  266. }
  267. if (i === -1) {
  268. // didn't increment anything
  269. if (identifier === this.prerelease.join('.') && identifierBase === false) {
  270. throw new Error('invalid increment argument: identifier already exists')
  271. }
  272. this.prerelease.push(base)
  273. }
  274. }
  275. if (identifier) {
  276. // 1.2.0-beta.1 bumps to 1.2.0-beta.2,
  277. // 1.2.0-beta.fooblz or 1.2.0-beta bumps to 1.2.0-beta.0
  278. let prerelease = [identifier, base]
  279. if (identifierBase === false) {
  280. prerelease = [identifier]
  281. }
  282. if (compareIdentifiers(this.prerelease[0], identifier) === 0) {
  283. if (isNaN(this.prerelease[1])) {
  284. this.prerelease = prerelease
  285. }
  286. } else {
  287. this.prerelease = prerelease
  288. }
  289. }
  290. break
  291. }
  292. default:
  293. throw new Error(`invalid increment argument: ${release}`)
  294. }
  295. this.raw = this.format()
  296. if (this.build.length) {
  297. this.raw += `+${this.build.join('.')}`
  298. }
  299. return this
  300. }
  301. }
  302. module.exports = SemVer