1 const Range = require('../classes/range.js')
2 const { ANY } = require('../classes/comparator.js')
3 const satisfies = require('../functions/satisfies.js')
4 const compare = require('../functions/compare.js')
6 // Complex range `r1 || r2 || ...` is a subset of `R1 || R2 || ...` iff:
7 // - Every simple range `r1, r2, ...` is a subset of some `R1, R2, ...`
9 // Simple range `c1 c2 ...` is a subset of simple range `C1 C2 ...` iff:
10 // - If c is only the ANY comparator
11 // - If C is only the ANY comparator, return true
12 // - Else return false
13 // - Let EQ be the set of = comparators in c
14 // - If EQ is more than one, return true (null set)
15 // - Let GT be the highest > or >= comparator in c
16 // - Let LT be the lowest < or <= comparator in c
17 // - If GT and LT, and GT.semver > LT.semver, return true (null set)
19 // - If GT, and EQ does not satisfy GT, return true (null set)
20 // - If LT, and EQ does not satisfy LT, return true (null set)
21 // - If EQ satisfies every C, return true
22 // - Else return false
24 // - If GT.semver is lower than any > or >= comp in C, return false
25 // - If GT is >=, and GT.semver does not satisfy every C, return false
27 // - If LT.semver is greater than any < or <= comp in C, return false
28 // - If LT is <=, and LT.semver does not satisfy every C, return false
29 // - If any C is a = range, and GT or LT are set, return false
32 const subset = (sub, dom, options) => {
36 sub = new Range(sub, options)
37 dom = new Range(dom, options)
38 let sawNonNull = false
40 OUTER: for (const simpleSub of sub.set) {
41 for (const simpleDom of dom.set) {
42 const isSub = simpleSubset(simpleSub, simpleDom, options)
43 sawNonNull = sawNonNull || isSub !== null
47 // the null set is a subset of everything, but null simple ranges in
48 // a complex range should be ignored. so if we saw a non-null range,
49 // then we know this isn't a subset, but if EVERY simple range was null,
50 // then it is a subset.
57 const simpleSubset = (sub, dom, options) => {
61 if (sub.length === 1 && sub[0].semver === ANY)
62 return dom.length === 1 && dom[0].semver === ANY
64 const eqSet = new Set()
66 for (const c of sub) {
67 if (c.operator === '>' || c.operator === '>=')
68 gt = higherGT(gt, c, options)
69 else if (c.operator === '<' || c.operator === '<=')
70 lt = lowerLT(lt, c, options)
80 gtltComp = compare(gt.semver, lt.semver, options)
83 else if (gtltComp === 0 && (gt.operator !== '>=' || lt.operator !== '<='))
87 // will iterate one or zero times
88 for (const eq of eqSet) {
89 if (gt && !satisfies(eq, String(gt), options))
92 if (lt && !satisfies(eq, String(lt), options))
95 for (const c of dom) {
96 if (!satisfies(eq, String(c), options))
104 let hasDomLT, hasDomGT
105 for (const c of dom) {
106 hasDomGT = hasDomGT || c.operator === '>' || c.operator === '>='
107 hasDomLT = hasDomLT || c.operator === '<' || c.operator === '<='
109 if (c.operator === '>' || c.operator === '>=') {
110 higher = higherGT(gt, c, options)
111 if (higher === c && higher !== gt)
113 } else if (gt.operator === '>=' && !satisfies(gt.semver, String(c), options))
117 if (c.operator === '<' || c.operator === '<=') {
118 lower = lowerLT(lt, c, options)
119 if (lower === c && lower !== lt)
121 } else if (lt.operator === '<=' && !satisfies(lt.semver, String(c), options))
124 if (!c.operator && (lt || gt) && gtltComp !== 0)
128 // if there was a < or >, and nothing in the dom, then must be false
129 // UNLESS it was limited by another range in the other direction.
130 // Eg, >1.0.0 <1.0.1 is still a subset of <2.0.0
131 if (gt && hasDomLT && !lt && gtltComp !== 0)
134 if (lt && hasDomGT && !gt && gtltComp !== 0)
140 // >=1.2.3 is lower than >1.2.3
141 const higherGT = (a, b, options) => {
144 const comp = compare(a.semver, b.semver, options)
147 : b.operator === '>' && a.operator === '>=' ? b
151 // <=1.2.3 is higher than <1.2.3
152 const lowerLT = (a, b, options) => {
155 const comp = compare(a.semver, b.semver, options)
158 : b.operator === '<' && a.operator === '<=' ? b
162 module.exports = subset