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

comparator.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. const ANY = Symbol('SemVer ANY')
  2. // hoisted class for cyclic dependency
  3. class Comparator {
  4. static get ANY () {
  5. return ANY
  6. }
  7. constructor (comp, options) {
  8. if (!options || typeof options !== 'object') {
  9. options = {
  10. loose: !!options,
  11. includePrerelease: false
  12. }
  13. }
  14. if (comp instanceof Comparator) {
  15. if (comp.loose === !!options.loose) {
  16. return comp
  17. } else {
  18. comp = comp.value
  19. }
  20. }
  21. debug('comparator', comp, options)
  22. this.options = options
  23. this.loose = !!options.loose
  24. this.parse(comp)
  25. if (this.semver === ANY) {
  26. this.value = ''
  27. } else {
  28. this.value = this.operator + this.semver.version
  29. }
  30. debug('comp', this)
  31. }
  32. parse (comp) {
  33. const r = this.options.loose ? re[t.COMPARATORLOOSE] : re[t.COMPARATOR]
  34. const m = comp.match(r)
  35. if (!m) {
  36. throw new TypeError(`Invalid comparator: ${comp}`)
  37. }
  38. this.operator = m[1] !== undefined ? m[1] : ''
  39. if (this.operator === '=') {
  40. this.operator = ''
  41. }
  42. // if it literally is just '>' or '' then allow anything.
  43. if (!m[2]) {
  44. this.semver = ANY
  45. } else {
  46. this.semver = new SemVer(m[2], this.options.loose)
  47. }
  48. }
  49. toString () {
  50. return this.value
  51. }
  52. test (version) {
  53. debug('Comparator.test', version, this.options.loose)
  54. if (this.semver === ANY || version === ANY) {
  55. return true
  56. }
  57. if (typeof version === 'string') {
  58. try {
  59. version = new SemVer(version, this.options)
  60. } catch (er) {
  61. return false
  62. }
  63. }
  64. return cmp(version, this.operator, this.semver, this.options)
  65. }
  66. intersects (comp, options) {
  67. if (!(comp instanceof Comparator)) {
  68. throw new TypeError('a Comparator is required')
  69. }
  70. if (!options || typeof options !== 'object') {
  71. options = {
  72. loose: !!options,
  73. includePrerelease: false
  74. }
  75. }
  76. if (this.operator === '') {
  77. if (this.value === '') {
  78. return true
  79. }
  80. return new Range(comp.value, options).test(this.value)
  81. } else if (comp.operator === '') {
  82. if (comp.value === '') {
  83. return true
  84. }
  85. return new Range(this.value, options).test(comp.semver)
  86. }
  87. const sameDirectionIncreasing =
  88. (this.operator === '>=' || this.operator === '>') &&
  89. (comp.operator === '>=' || comp.operator === '>')
  90. const sameDirectionDecreasing =
  91. (this.operator === '<=' || this.operator === '<') &&
  92. (comp.operator === '<=' || comp.operator === '<')
  93. const sameSemVer = this.semver.version === comp.semver.version
  94. const differentDirectionsInclusive =
  95. (this.operator === '>=' || this.operator === '<=') &&
  96. (comp.operator === '>=' || comp.operator === '<=')
  97. const oppositeDirectionsLessThan =
  98. cmp(this.semver, '<', comp.semver, options) &&
  99. (this.operator === '>=' || this.operator === '>') &&
  100. (comp.operator === '<=' || comp.operator === '<')
  101. const oppositeDirectionsGreaterThan =
  102. cmp(this.semver, '>', comp.semver, options) &&
  103. (this.operator === '<=' || this.operator === '<') &&
  104. (comp.operator === '>=' || comp.operator === '>')
  105. return (
  106. sameDirectionIncreasing ||
  107. sameDirectionDecreasing ||
  108. (sameSemVer && differentDirectionsInclusive) ||
  109. oppositeDirectionsLessThan ||
  110. oppositeDirectionsGreaterThan
  111. )
  112. }
  113. }
  114. module.exports = Comparator
  115. const {re, t} = require('../internal/re')
  116. const cmp = require('../functions/cmp')
  117. const debug = require('../internal/debug')
  118. const SemVer = require('./semver')
  119. const Range = require('./range')