6ae29a3c92b89280eebf25ee41cd1a45298ad3b7
[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 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 that of any > 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   sub = new Range(sub, options)
34   dom = new Range(dom, options)
35   let sawNonNull = false
36
37   OUTER: for (const simpleSub of sub.set) {
38     for (const simpleDom of dom.set) {
39       const isSub = simpleSubset(simpleSub, simpleDom, options)
40       sawNonNull = sawNonNull || isSub !== null
41       if (isSub)
42         continue OUTER
43     }
44     // the null set is a subset of everything, but null simple ranges in
45     // a complex range should be ignored.  so if we saw a non-null range,
46     // then we know this isn't a subset, but if EVERY simple range was null,
47     // then it is a subset.
48     if (sawNonNull)
49       return false
50   }
51   return true
52 }
53
54 const simpleSubset = (sub, dom, options) => {
55   if (sub.length === 1 && sub[0].semver === ANY)
56     return dom.length === 1 && dom[0].semver === ANY
57
58   const eqSet = new Set()
59   let gt, lt
60   for (const c of sub) {
61     if (c.operator === '>' || c.operator === '>=')
62       gt = higherGT(gt, c, options)
63     else if (c.operator === '<' || c.operator === '<=')
64       lt = lowerLT(lt, c, options)
65     else
66       eqSet.add(c.semver)
67   }
68
69   if (eqSet.size > 1)
70     return null
71
72   let gtltComp
73   if (gt && lt) {
74     gtltComp = compare(gt.semver, lt.semver, options)
75     if (gtltComp > 0)
76       return null
77     else if (gtltComp === 0 && (gt.operator !== '>=' || lt.operator !== '<='))
78       return null
79   }
80
81   // will iterate one or zero times
82   for (const eq of eqSet) {
83     if (gt && !satisfies(eq, String(gt), options))
84       return null
85
86     if (lt && !satisfies(eq, String(lt), options))
87       return null
88
89     for (const c of dom) {
90       if (!satisfies(eq, String(c), options))
91         return false
92     }
93     return true
94   }
95
96   let higher, lower
97   let hasDomLT, hasDomGT
98   for (const c of dom) {
99     hasDomGT = hasDomGT || c.operator === '>' || c.operator === '>='
100     hasDomLT = hasDomLT || c.operator === '<' || c.operator === '<='
101     if (gt) {
102       if (c.operator === '>' || c.operator === '>=') {
103         higher = higherGT(gt, c, options)
104         if (higher === c)
105           return false
106       } else if (gt.operator === '>=' && !satisfies(gt.semver, String(c), options))
107         return false
108     }
109     if (lt) {
110       if (c.operator === '<' || c.operator === '<=') {
111         lower = lowerLT(lt, c, options)
112         if (lower === c)
113           return false
114       } else if (lt.operator === '<=' && !satisfies(lt.semver, String(c), options))
115         return false
116     }
117     if (!c.operator && (lt || gt) && gtltComp !== 0)
118       return false
119   }
120
121   // if there was a < or >, and nothing in the dom, then must be false
122   // UNLESS it was limited by another range in the other direction.
123   // Eg, >1.0.0 <1.0.1 is still a subset of <2.0.0
124   if (gt && hasDomLT && !lt && gtltComp !== 0)
125     return false
126
127   if (lt && hasDomGT && !gt && gtltComp !== 0)
128     return false
129
130   return true
131 }
132
133 // >=1.2.3 is lower than >1.2.3
134 const higherGT = (a, b, options) => {
135   if (!a)
136     return b
137   const comp = compare(a.semver, b.semver, options)
138   return comp > 0 ? a
139     : comp < 0 ? b
140     : b.operator === '>' && a.operator === '>=' ? b
141     : a
142 }
143
144 // <=1.2.3 is higher than <1.2.3
145 const lowerLT = (a, b, options) => {
146   if (!a)
147     return b
148   const comp = compare(a.semver, b.semver, options)
149   return comp < 0 ? a
150     : comp > 0 ? b
151     : b.operator === '<' && a.operator === '<=' ? b
152     : a
153 }
154
155 module.exports = subset