-
Notifications
You must be signed in to change notification settings - Fork 5
/
find.js
32 lines (29 loc) · 825 Bytes
/
find.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import ruleSet from './rules/ruleSet'
export default function find (component, selector) {
let results = []
switch (selector.type) {
case 'ruleSet':
if (ruleSet(component, selector)) {
results.push(component)
}
break
case 'selectors':
let selectorsResult = new Set(selector.selectors.map(s => find(component, s)))
results.push(...selectorsResult)
return results
case 'custom':
if (selector.match(component)) {
results.push(component)
}
break
}
if (component.children) {
let children = Array.isArray(component.children) ? component.children : [component.children]
children.forEach(child => {
if (typeof child === 'object') {
results = [...results, ...find(child, selector)]
}
})
}
return results
}