Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

semver.js 7.8KB

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