semver.js 9.7 KB

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