-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
99 lines (82 loc) · 2.23 KB
/
index.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
'use strict';
import fs from 'fs-extra';
import bcd from '@mdn/browser-compat-data' assert {type: 'json'};
const allbrowsers = ['chrome', 'chrome_android', 'edge', 'firefox', 'safari', 'safari_ios', 'webview_android'];
const getStats = (bcd, browsers, folder) => {
let stats = {all: 0};
for (const browser of browsers) {
stats[browser] = {true: 0, null: 0, real: 0};
}
const checkSupport = (supportData, type) => {
if (!Array.isArray(supportData)) {
supportData = [supportData];
}
return supportData.some(item => item.version_added === type || item.version_removed === type)
};
const processData = (data) => {
if (data.support) {
stats.all++;
browsers.forEach(function(browser) {
let real_value = true;
if (!data.support[browser]) {
stats[browser].null++;
real_value = false;
} else {
if (checkSupport(data.support[browser], null)) {
stats[browser].null++;
real_value = false;
}
if (checkSupport(data.support[browser], true)) {
stats[browser].true++;
real_value = false;
}
}
if (real_value) {
stats[browser].real++;
}
});
}
};
const iterateData = (data) => {
for (const [key, value] of Object.entries(data)) {
if (key === '__compat') {
processData(value);
} else {
iterateData(value);
}
}
};
if (folder) {
iterateData(bcd[folder]);
} else {
for (let data in bcd) {
if (!(data === 'browsers' || data === '__meta' || data === 'webextensions')) {
iterateData(bcd[data]);
}
}
}
return stats;
}
const addStats = (stats, newStats, version) => {
for (const [key, data] of Object.entries(newStats)) {
if (key === 'all') {
stats.all[version] = data;
continue;
}
stats[key].true[version] = data.true;
stats[key].null[version] = data.null;
stats[key].real[version] = data.real;
}
return stats;
}
const main = async () => {
const bcdversion = bcd.__meta.version;
const oldStats = JSON.parse(await fs.readFile("./bcd-stats.json"));
const newStats = getStats(bcd, allbrowsers, '');
const stats = addStats(oldStats, newStats, bcdversion);
await fs.writeFile("./bcd-stats.json", JSON.stringify(stats));
}
if (import.meta.url === `file://${process.argv[1]}`) {
main();
}
export default getStats;