semver.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. const debug = require('../internal/debug')
  2. const { MAX_LENGTH, MAX_SAFE_INTEGER } = require('../internal/constants')
  3. const { safeRe: re, safeSrc: src, 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. if (release.startsWith('pre')) {
  154. if (!identifier && identifierBase === false) {
  155. throw new Error('invalid increment argument: identifier is empty')
  156. }
  157. // Avoid an invalid semver results
  158. if (identifier) {
  159. const r = new RegExp(`^${this.options.loose ? src[t.PRERELEASELOOSE] : src[t.PRERELEASE]}$`)
  160. const match = `-${identifier}`.match(r)
  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