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