subset.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. 'use strict'
  2. const Range = require('../classes/range.js')
  3. const Comparator = require('../classes/comparator.js')
  4. const { ANY } = Comparator
  5. const satisfies = require('../functions/satisfies.js')
  6. const compare = require('../functions/compare.js')
  7. // Complex range `r1 || r2 || ...` is a subset of `R1 || R2 || ...` iff:
  8. // - Every simple range `r1, r2, ...` is a null set, OR
  9. // - Every simple range `r1, r2, ...` which is not a null set is a subset of
  10. // some `R1, R2, ...`
  11. //
  12. // Simple range `c1 c2 ...` is a subset of simple range `C1 C2 ...` iff:
  13. // - If c is only the ANY comparator
  14. // - If C is only the ANY comparator, return true
  15. // - Else if in prerelease mode, return false
  16. // - else replace c with `[>=0.0.0]`
  17. // - If C is only the ANY comparator
  18. // - if in prerelease mode, return true
  19. // - else replace C with `[>=0.0.0]`
  20. // - Let EQ be the set of = comparators in c
  21. // - If EQ is more than one, return true (null set)
  22. // - Let GT be the highest > or >= comparator in c
  23. // - Let LT be the lowest < or <= comparator in c
  24. // - If GT and LT, and GT.semver > LT.semver, return true (null set)
  25. // - If any C is a = range, and GT or LT are set, return false
  26. // - If EQ
  27. // - If GT, and EQ does not satisfy GT, return true (null set)
  28. // - If LT, and EQ does not satisfy LT, return true (null set)
  29. // - If EQ satisfies every C, return true
  30. // - Else return false
  31. // - If GT
  32. // - If GT.semver is lower than any > or >= comp in C, return false
  33. // - If GT is >=, and GT.semver does not satisfy every C, return false
  34. // - If GT.semver has a prerelease, and not in prerelease mode
  35. // - If no C has a prerelease and the GT.semver tuple, return false
  36. // - If LT
  37. // - If LT.semver is greater than any < or <= comp in C, return false
  38. // - If LT is <=, and LT.semver does not satisfy every C, return false
  39. // - If GT.semver has a prerelease, and not in prerelease mode
  40. // - If no C has a prerelease and the LT.semver tuple, return false
  41. // - Else return true
  42. const subset = (sub, dom, options = {}) => {
  43. if (sub === dom) {
  44. return true
  45. }
  46. sub = new Range(sub, options)
  47. dom = new Range(dom, options)
  48. let sawNonNull = false
  49. OUTER: for (const simpleSub of sub.set) {
  50. for (const simpleDom of dom.set) {
  51. const isSub = simpleSubset(simpleSub, simpleDom, options)
  52. sawNonNull = sawNonNull || isSub !== null
  53. if (isSub) {
  54. continue OUTER
  55. }
  56. }
  57. // the null set is a subset of everything, but null simple ranges in
  58. // a complex range should be ignored. so if we saw a non-null range,
  59. // then we know this isn't a subset, but if EVERY simple range was null,
  60. // then it is a subset.
  61. if (sawNonNull) {
  62. return false
  63. }
  64. }
  65. return true
  66. }
  67. const minimumVersionWithPreRelease = [new Comparator('>=0.0.0-0')]
  68. const minimumVersion = [new Comparator('>=0.0.0')]
  69. const simpleSubset = (sub, dom, options) => {
  70. if (sub === dom) {
  71. return true
  72. }
  73. if (sub.length === 1 && sub[0].semver === ANY) {
  74. if (dom.length === 1 && dom[0].semver === ANY) {
  75. return true
  76. } else if (options.includePrerelease) {
  77. sub = minimumVersionWithPreRelease
  78. } else {
  79. sub = minimumVersion
  80. }
  81. }
  82. if (dom.length === 1 && dom[0].semver === ANY) {
  83. if (options.includePrerelease) {
  84. return true
  85. } else {
  86. dom = minimumVersion
  87. }
  88. }
  89. const eqSet = new Set()
  90. let gt, lt
  91. for (const c of sub) {
  92. if (c.operator === '>' || c.operator === '>=') {
  93. gt = higherGT(gt, c, options)
  94. } else if (c.operator === '<' || c.operator === '<=') {
  95. lt = lowerLT(lt, c, options)
  96. } else {
  97. eqSet.add(c.semver)
  98. }
  99. }
  100. if (eqSet.size > 1) {
  101. return null
  102. }
  103. let gtltComp
  104. if (gt && lt) {
  105. gtltComp = compare(gt.semver, lt.semver, options)
  106. if (gtltComp > 0) {
  107. return null
  108. } else if (gtltComp === 0 && (gt.operator !== '>=' || lt.operator !== '<=')) {
  109. return null
  110. }
  111. }
  112. // will iterate one or zero times
  113. for (const eq of eqSet) {
  114. if (gt && !satisfies(eq, String(gt), options)) {
  115. return null
  116. }
  117. if (lt && !satisfies(eq, String(lt), options)) {
  118. return null
  119. }
  120. for (const c of dom) {
  121. if (!satisfies(eq, String(c), options)) {
  122. return false
  123. }
  124. }
  125. return true
  126. }
  127. let higher, lower
  128. let hasDomLT, hasDomGT
  129. // if the subset has a prerelease, we need a comparator in the superset
  130. // with the same tuple and a prerelease, or it's not a subset
  131. let needDomLTPre = lt &&
  132. !options.includePrerelease &&
  133. lt.semver.prerelease.length ? lt.semver : false
  134. let needDomGTPre = gt &&
  135. !options.includePrerelease &&
  136. gt.semver.prerelease.length ? gt.semver : false
  137. // exception: <1.2.3-0 is the same as <1.2.3
  138. if (needDomLTPre && needDomLTPre.prerelease.length === 1 &&
  139. lt.operator === '<' && needDomLTPre.prerelease[0] === 0) {
  140. needDomLTPre = false
  141. }
  142. for (const c of dom) {
  143. hasDomGT = hasDomGT || c.operator === '>' || c.operator === '>='
  144. hasDomLT = hasDomLT || c.operator === '<' || c.operator === '<='
  145. if (gt) {
  146. if (needDomGTPre) {
  147. if (c.semver.prerelease && c.semver.prerelease.length &&
  148. c.semver.major === needDomGTPre.major &&
  149. c.semver.minor === needDomGTPre.minor &&
  150. c.semver.patch === needDomGTPre.patch) {
  151. needDomGTPre = false
  152. }
  153. }
  154. if (c.operator === '>' || c.operator === '>=') {
  155. higher = higherGT(gt, c, options)
  156. if (higher === c && higher !== gt) {
  157. return false
  158. }
  159. } else if (gt.operator === '>=' && !satisfies(gt.semver, String(c), options)) {
  160. return false
  161. }
  162. }
  163. if (lt) {
  164. if (needDomLTPre) {
  165. if (c.semver.prerelease && c.semver.prerelease.length &&
  166. c.semver.major === needDomLTPre.major &&
  167. c.semver.minor === needDomLTPre.minor &&
  168. c.semver.patch === needDomLTPre.patch) {
  169. needDomLTPre = false
  170. }
  171. }
  172. if (c.operator === '<' || c.operator === '<=') {
  173. lower = lowerLT(lt, c, options)
  174. if (lower === c && lower !== lt) {
  175. return false
  176. }
  177. } else if (lt.operator === '<=' && !satisfies(lt.semver, String(c), options)) {
  178. return false
  179. }
  180. }
  181. if (!c.operator && (lt || gt) && gtltComp !== 0) {
  182. return false
  183. }
  184. }
  185. // if there was a < or >, and nothing in the dom, then must be false
  186. // UNLESS it was limited by another range in the other direction.
  187. // Eg, >1.0.0 <1.0.1 is still a subset of <2.0.0
  188. if (gt && hasDomLT && !lt && gtltComp !== 0) {
  189. return false
  190. }
  191. if (lt && hasDomGT && !gt && gtltComp !== 0) {
  192. return false
  193. }
  194. // we needed a prerelease range in a specific tuple, but didn't get one
  195. // then this isn't a subset. eg >=1.2.3-pre is not a subset of >=1.0.0,
  196. // because it includes prereleases in the 1.2.3 tuple
  197. if (needDomGTPre || needDomLTPre) {
  198. return false
  199. }
  200. return true
  201. }
  202. // >=1.2.3 is lower than >1.2.3
  203. const higherGT = (a, b, options) => {
  204. if (!a) {
  205. return b
  206. }
  207. const comp = compare(a.semver, b.semver, options)
  208. return comp > 0 ? a
  209. : comp < 0 ? b
  210. : b.operator === '>' && a.operator === '>=' ? b
  211. : a
  212. }
  213. // <=1.2.3 is higher than <1.2.3
  214. const lowerLT = (a, b, options) => {
  215. if (!a) {
  216. return b
  217. }
  218. const comp = compare(a.semver, b.semver, options)
  219. return comp < 0 ? a
  220. : comp > 0 ? b
  221. : b.operator === '<' && a.operator === '<=' ? b
  222. : a
  223. }
  224. module.exports = subset