-
Notifications
You must be signed in to change notification settings - Fork 1
/
diff.js
129 lines (111 loc) · 2.57 KB
/
diff.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import {
data as mdnData,
keyMap,
unusedFilters as unusedMdnFilters,
} from "./mdn.js";
import {
data as refData,
unusedFilters as unusedWebrefFilters,
} from "./webref.js";
import keyCoverages from "./key-coverages.js";
import * as utils from "./utils.js";
for (const key in keyCoverages) {
keyCoverages[key] = new Set(keyCoverages[key]);
}
/**
* Diff an MDN object and a Webref Object,
* both of which have homogeneous values.
* @param {Object} mdn
* @param {Object} ref
*/
function diff(mdn, ref) {
let mdnOnly = {};
for (const key in mdn) {
if (!(key in ref)) {
// Tolerate prefixed keys
if (!/^\W*?-/u.test(key)) {
mdnOnly[key] = mdn[key];
}
delete mdn[key];
} else {
let mdnItem = mdn[key];
let refItem = ref[key];
let mdnOnly = {};
for (const key in mdnItem) {
if (!(key in refItem)) {
mdnOnly[key] = mdnItem[key];
delete mdnItem[key];
continue;
}
let mdn = mdnItem[key];
let ref = refItem[key];
if (typeof mdn == "object") {
diff(mdn, ref);
if (utils.isEmpty(mdn)) {
delete mdnItem[key];
}
continue;
}
if (
(key == "initial" && mdn != "seeIndividualProperties") ||
key == "syntax"
) {
if (mdn === ref) {
delete mdnItem[key];
continue;
}
} else {
const coverage = keyCoverages[mdn];
if (
[ref].flat()
.every((item) => coverage?.has(item))
) {
delete mdnItem[key];
continue;
}
mdn = {
key: mdn,
text: keyMap[mdn] || "[[Missing]]",
};
}
mdnItem[key] = { mdn, ref };
}
if (!utils.isEmpty(mdnOnly)) {
mdnItem["[[MDN-only]]"] = mdnOnly;
} else if (utils.isEmpty(mdnItem)) {
delete mdn[key];
}
}
}
if (!utils.isEmpty(mdnOnly)) {
mdn["[[MDN-only]]"] = mdnOnly;
}
}
for (const key in mdnData) {
if (!(key in refData)) {
delete mdnData[key];
continue;
}
let mdn = mdnData[key];
let ref = refData[key];
diff(mdn, ref);
if (utils.isEmpty(mdn)) {
delete mdnData[key];
continue;
}
for (const key in mdn) {
if (key in ref) {
mdn[key] = {
spec: ref[key].spec,
...mdn[key],
};
}
}
}
console.info("Diff generated.");
export {
/** The diff from the MDN data to the Webref data */
mdnData as diff,
unusedMdnFilters,
unusedWebrefFilters,
};