outside.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. const SemVer = require('../classes/semver')
  2. const Comparator = require('../classes/comparator')
  3. const {ANY} = Comparator
  4. const Range = require('../classes/range')
  5. const satisfies = require('../functions/satisfies')
  6. const gt = require('../functions/gt')
  7. const lt = require('../functions/lt')
  8. const lte = require('../functions/lte')
  9. const gte = require('../functions/gte')
  10. const outside = (version, range, hilo, options) => {
  11. version = new SemVer(version, options)
  12. range = new Range(range, options)
  13. let gtfn, ltefn, ltfn, comp, ecomp
  14. switch (hilo) {
  15. case '>':
  16. gtfn = gt
  17. ltefn = lte
  18. ltfn = lt
  19. comp = '>'
  20. ecomp = '>='
  21. break
  22. case '<':
  23. gtfn = lt
  24. ltefn = gte
  25. ltfn = gt
  26. comp = '<'
  27. ecomp = '<='
  28. break
  29. default:
  30. throw new TypeError('Must provide a hilo val of "<" or ">"')
  31. }
  32. // If it satisfies the range it is not outside
  33. if (satisfies(version, range, options)) {
  34. return false
  35. }
  36. // From now on, variable terms are as if we're in "gtr" mode.
  37. // but note that everything is flipped for the "ltr" function.
  38. for (let i = 0; i < range.set.length; ++i) {
  39. const comparators = range.set[i]
  40. let high = null
  41. let low = null
  42. comparators.forEach((comparator) => {
  43. if (comparator.semver === ANY) {
  44. comparator = new Comparator('>=0.0.0')
  45. }
  46. high = high || comparator
  47. low = low || comparator
  48. if (gtfn(comparator.semver, high.semver, options)) {
  49. high = comparator
  50. } else if (ltfn(comparator.semver, low.semver, options)) {
  51. low = comparator
  52. }
  53. })
  54. // If the edge version comparator has a operator then our version
  55. // isn't outside it
  56. if (high.operator === comp || high.operator === ecomp) {
  57. return false
  58. }
  59. // If the lowest version comparator has an operator and our version
  60. // is less than it then it isn't higher than the range
  61. if ((!low.operator || low.operator === comp) &&
  62. ltefn(version, low.semver)) {
  63. return false
  64. } else if (low.operator === ecomp && ltfn(version, low.semver)) {
  65. return false
  66. }
  67. }
  68. return true
  69. }
  70. module.exports = outside