forked from adamlaska/browser-compat-data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisit.js
More file actions
39 lines (33 loc) · 988 Bytes
/
visit.js
File metadata and controls
39 lines (33 loc) · 988 Bytes
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
33
34
35
36
37
38
39
const bcd = require('..');
const query = require('./query');
const { descendantKeys, joinPath, isFeature } = require('./walkingUtils');
const BREAK = Symbol('break');
const CONTINUE = Symbol('continue');
function visit(visitor, options = {}) {
const { entryPoint, data } = options;
const test = options.test !== undefined ? options.test : () => true;
const tree = entryPoint === undefined ? bcd : query(entryPoint, data);
let outcome;
if (isFeature(tree) && test(entryPoint, tree.__compat)) {
outcome = visitor(entryPoint, tree.__compat);
}
if (outcome === BREAK) {
return outcome;
}
if (outcome !== CONTINUE) {
for (const key of descendantKeys(tree)) {
const suboutcome = visit(visitor, {
entryPoint: joinPath(entryPoint, key),
test,
data,
});
if (suboutcome === BREAK) {
return suboutcome;
}
}
}
return outcome;
}
visit.BREAK = BREAK;
visit.CONTINUE = CONTINUE;
module.exports = visit;