Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / semver / ranges / subset.js
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')
5
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, ...`
8 //
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)
18 // - If EQ
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
23 // - If GT
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
26 // - If LT
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
30 // - Else return true
31
32 const subset = (sub, dom, options) => {
33   if (sub === dom)
34     return true
35
36   sub = new Range(sub, options)
37   dom = new Range(dom, options)
38   let sawNonNull = false
39
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
44       if (isSub)
45         continue OUTER
46     }
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.
51     if (sawNonNull)
52       return false
53   }
54   return true
55 }
56
57 const simpleSubset = (sub, dom, options) => {
58   if (sub === dom)
59     return true
60
61   if (sub.length === 1 && sub[0].semver === ANY)
62     return dom.length === 1 && dom[0].semver === ANY
63
64   const eqSet = new Set()
65   let gt, lt
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)
71     else
72       eqSet.add(c.semver)
73   }
74
75   if (eqSet.size > 1)
76     return null
77
78   let gtltComp
79   if (gt && lt) {
80     gtltComp = compare(gt.semver, lt.semver, options)
81     if (gtltComp > 0)
82       return null
83     else if (gtltComp === 0 && (gt.operator !== '>=' || lt.operator !== '<='))
84       return null
85   }
86
87   // will iterate one or zero times
88   for (const eq of eqSet) {
89     if (gt && !satisfies(eq, String(gt), options))
90       return null
91
92     if (lt && !satisfies(eq, String(lt), options))
93       return null
94
95     for (const c of dom) {
96       if (!satisfies(eq, String(c), options))
97         return false
98     }
99
100     return true
101   }
102
103   let higher, lower
104   let hasDomLT, hasDomGT
105   for (const c of dom) {
106     hasDomGT = hasDomGT || c.operator === '>' || c.operator === '>='
107     hasDomLT = hasDomLT || c.operator === '<' || c.operator === '<='
108     if (gt) {
109       if (c.operator === '>' || c.operator === '>=') {
110         higher = higherGT(gt, c, options)
111         if (higher === c && higher !== gt)
112           return false
113       } else if (gt.operator === '>=' && !satisfies(gt.semver, String(c), options))
114         return false
115     }
116     if (lt) {
117       if (c.operator === '<' || c.operator === '<=') {
118         lower = lowerLT(lt, c, options)
119         if (lower === c && lower !== lt)
120           return false
121       } else if (lt.operator === '<=' && !satisfies(lt.semver, String(c), options))
122         return false
123     }
124     if (!c.operator && (lt || gt) && gtltComp !== 0)
125       return false
126   }
127
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)
132     return false
133
134   if (lt && hasDomGT && !gt && gtltComp !== 0)
135     return false
136
137   return true
138 }
139
140 // >=1.2.3 is lower than >1.2.3
141 const higherGT = (a, b, options) => {
142   if (!a)
143     return b
144   const comp = compare(a.semver, b.semver, options)
145   return comp > 0 ? a
146     : comp < 0 ? b
147     : b.operator === '>' && a.operator === '>=' ? b
148     : a
149 }
150
151 // <=1.2.3 is higher than <1.2.3
152 const lowerLT = (a, b, options) => {
153   if (!a)
154     return b
155   const comp = compare(a.semver, b.semver, options)
156   return comp < 0 ? a
157     : comp > 0 ? b
158     : b.operator === '<' && a.operator === '<=' ? b
159     : a
160 }
161
162 module.exports = subset