-
Notifications
You must be signed in to change notification settings - Fork 0
/
bcd.js
243 lines (225 loc) · 5.75 KB
/
bcd.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright 2024 Joseph P Medley
'use strict';
const bcd = require('@mdn/browser-compat-data');
const SKIPABLE = ['__compat','__name','__parent','browsers','description','mathml','mdn_url','getPossibleKeys','webdriver','webextensions','xpath','xslt'];
const URL_ROOT = 'https://developer.mozilla.org/en-US/docs/Web/';
const ENGINES = [
{
"name": "chrome",
"engine": "Chromium"
},
{
"name": "chrome_android",
"engine": "Chromium"
},
{
"name": "deno",
"engine": "IGNORE"
},
{
"name": "edge",
"engine": "Chromium"
},
{
"name": "ie",
"engine": "IGNORE"
},
{
"name": "firefox",
"engine": "Gecko"
},
{
"name": "firefox_android",
"engine": "Gecko"
},
{
"name": "nodejs",
"engine": "Chromium"
},
{
"name": "opera",
"engine": "Chromium"
},
{
"name": "opera_android",
"engine": "Chromium"
},
{
"name": "safari",
"engine": "WebKit"
},
{
"name": "safari_ios",
"engine": "WebKit"
},
{
"name": "samsunginternet_android",
"engine": "Chromium"
},
{
"name": "webview_android",
"engine": "Chromium"
},
]
const EMPTY_BURN_RECORD = Object.freeze({
key: null,
bcd: null,
flag: null,
mdn_exists: null,
mdn_url: '',
name: '',
origin_trial: null,
redirect: null,
type: ''
});
function generateFullKey(currentKey) {
if (!currentKey.__parent) { return currentKey.__name; }
let resultArray = new Array();
do {
resultArray.push(currentKey.__name);
currentKey = currentKey.__parent;
} while (currentKey.__parent);
resultArray = resultArray.reverse();
const result = resultArray.join('.');
return result;
}
class BCD {
constructor() {
this._decorate(bcd);
this._bindNewMembers(bcd);
return bcd;
}
_bindNewMembers(bcd) {
bcd.getPossibleKeys = this.getPossibleKeys;
this.getPossibleKeys.bind(bcd);
bcd.getByKey = this.getByKey;
this.getByKey.bind(bcd);
bcd.getRecordByKey = this.getRecordByKey;
this.getRecordByKey.bind(bcd);
bcd.getEngines = this.getEngines;
this.getEngines.bind(bcd);
bcd.getBrowsers = this.getBrowsers;
this.getBrowsers.bind(bcd);
bcd.getVersions = this.getVersions;
this.getVersions.bind(bcd);
}
_decorate(data) {
let keys = Object.keys(data);
if (keys.length) {
for (let k of keys) {
if (!data[k]) { continue; }
if (k == '__parent') { continue; }
if (typeof data[k] != 'object') { continue; }
data[k].__parent = data;
data[k].__name = k;
this._decorate(data[k]);
}
}
}
getVersions(key, browsers = ['chrome'], trunk = 'api') {
const branch = this.getByKey(key, trunk);
if (!branch) { return null; }
const support = branch?.__compat?.support;
if (!support) { return ''; }
let versions = [];
let temp;
for (let b of browsers) {
if (!support[b]) { continue; }
temp = support[b]
if (temp.version_added) {
versions.push(temp.version_added);
} else {
versions.push('Not supported');
}
}
// TODO: Figure out whether this needs to return a string or an array.
// return versions.join(', ');
return versions;
}
getBrowsers(key, trunk = 'api') {
const branch = this.getByKey(key, trunk);
if (!branch) { return null; }
const support = branch?.__compat?.support;
let browsers = [];
for (const s in support) {
if (s === '__parent') { continue; }
if (s === '__name') { continue; }
browsers.push(s);
}
return browsers;
}
getByKey(key, trunk = "api") {
let branch = this[trunk];
if (key === trunk) { return branch; }
let chain = key.split(".").reverse();
while (chain.length) {
let link = chain[chain.length - 1];
if (!branch[link]) { return null; }
branch = branch[link];
chain.pop();
}
return branch;
}
getEngines(key, trunk = 'api') {
const branch = this.getByKey(key, trunk);
if (!branch) { return null; }
const support = branch?.__compat?.support;
if (!support) { return null; }
let engines = [];
ENGINES.forEach(e => {
if (e.engine === 'IGNORE') { return; }
const browser = support[e.name];
if (!browser) { return; }
if (browser.version_added === 'false') { return; }
if (browser.version_added === 'null') { return; }
const engine = e.engine;
if (!engines.includes(engine)) {
engines.push(engine);
}
});
return engines;
}
getPossibleKeys(withString) {
let results = new Array();
(function getKeys(tree, results) {
let keys = Object.keys(tree);
if (!keys.length) { throw new Error(); }
for (let k in tree) {
if (SKIPABLE.includes(k)) { continue; }
let fullKey = generateFullKey(tree[k]);
if (fullKey.includes(withString)) {
results.push(fullKey);
}
getKeys(tree[k], results);
}
})(this, results);
return results;
}
getRecordByKey(key, trunk = 'api') {
// Assumes trunk in BCD and root in URL are congruent.
// May not always be true.
let burnRecord = Object.assign({}, EMPTY_BURN_RECORD);
burnRecord.key = key;
const bcdData = this.getByKey(key, trunk);
if (bcdData) {
burnRecord.bcd = true;
if (bcdData.__compat) {
burnRecord.mdn_url = bcdData.__compat.mdn_url;
}
} else {
burnRecord.bcd = false;
burnRecord.mdn_exists = false;
}
if (!burnRecord.mdn_url) {
let path = key.replace(".", "/");
burnRecord.mdn_url = `${URL_ROOT}${trunk.toUpperCase()}/${path}`;
}
return burnRecord;
}
}
// module.exports.BCD = BCD;
if (!global.__bcd) {
global.__bcd = new BCD();
}
module.exports.bcd = global.__bcd;
module.exports.EMPTY_BURN_RECORD = EMPTY_BURN_RECORD;