forked from adamlaska/browser-compat-data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwalk.js
More file actions
50 lines (43 loc) · 1.1 KB
/
walk.js
File metadata and controls
50 lines (43 loc) · 1.1 KB
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
40
41
42
43
44
45
46
47
48
49
50
const bcd = require('..');
const { isBrowser, descendantKeys, joinPath } = require('./walkingUtils');
const query = require('./query');
function* lowLevelWalk(data = bcd, path, depth = Infinity) {
if (path !== undefined) {
const next = {
path,
data,
};
if (isBrowser(data)) {
next.browser = data;
} else if (data.__compat !== undefined) {
next.compat = data.__compat;
}
yield next;
}
if (depth > 0) {
for (const key of descendantKeys(data)) {
yield* lowLevelWalk(data[key], joinPath(path, key), depth - 1);
}
}
}
function* walk(entryPoints, data = bcd) {
const walkers = [];
if (entryPoints === undefined) {
walkers.push(lowLevelWalk(data));
} else {
entryPoints = Array.isArray(entryPoints) ? entryPoints : [entryPoints];
walkers.push(
...entryPoints.map(entryPoint =>
lowLevelWalk(query(entryPoint, data), entryPoint),
),
);
}
for (const walker of walkers) {
for (const step of walker) {
if (step.compat) {
yield step;
}
}
}
}
module.exports = { walk, lowLevelWalk };