forked from adamlaska/browser-compat-data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatistics.js
More file actions
213 lines (194 loc) · 6.08 KB
/
statistics.js
File metadata and controls
213 lines (194 loc) · 6.08 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
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
#!/usr/bin/env node
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
'use strict';
const chalk = require('chalk');
const bcd = require('..');
const { getRefDate } = require('./release-utils');
const { argv } = require('yargs').command(
'$0 [folder]',
'Print a markdown-formatted table displaying the statistics of real, ranged, true, and null values for each browser',
yargs => {
yargs
.positional('folder', {
describe: 'Limit the statistics to a specific folder',
type: 'string',
default: '',
})
.option('all', {
alias: 'a',
describe: 'Show statistics for all browsers within BCD',
type: 'boolean',
nargs: 0,
});
},
);
/**
* @typedef {import('../../types').Identifier} Identifier
*
* @typedef {object} VersionStats
* @property {number} all The total number of occurrences for the browser.
* @property {number} true The total number of `true` values for the browser.
* @property {number} null The total number of `null` values for the browser.
* @property {number} range The total number of range values for the browser.
* @property {number} real The total number of real values for the browser.
*/
/**
* Check whether a support statement is a specified type
*
* @param {Identifier} supportData The support statement to check
* @param {string|boolean|null} type What type of support (true, null, ranged)
* @returns {boolean} If the support statement has the type
*/
const checkSupport = (supportData, type) => {
if (!Array.isArray(supportData)) {
supportData = [supportData];
}
if (type == '≤') {
return supportData.some(
item =>
(typeof item.version_added == 'string' &&
item.version_added.startsWith('≤')) ||
(typeof item.version_removed == 'string' &&
item.version_removed.startsWith('≤')),
);
}
return supportData.some(
item => item.version_added === type || item.version_removed === type,
);
};
/**
* Iterate through all of the browsers and count the number of true, null, real, and ranged values for each browser
*
* @param {Identifier} data The data to process and count stats for
* @param {string[]} browsers The browsers to test
* @param {object.<string, VersionStats>} stats The stats object to update
* @returns {void}
*/
const processData = (data, browsers, stats) => {
if (data.support) {
browsers.forEach(browser => {
stats[browser].all++;
stats.total.all++;
if (!data.support[browser]) {
stats[browser].null++;
stats.total.null++;
} else if (checkSupport(data.support[browser], null)) {
stats[browser].null++;
stats.total.null++;
} else if (checkSupport(data.support[browser], true)) {
stats[browser].true++;
stats.total.true++;
} else if (checkSupport(data.support[browser], '≤')) {
stats[browser].range++;
stats.total.range++;
} else {
stats[browser].real++;
stats.total.real++;
}
});
}
};
/**
* Iterate through all of the data and process statistics
*
* @param {Identifier} data The compat data to iterate
* @param {string[]} browsers The browsers to test
* @param {object.<string, VersionStats>} stats The stats object to update
* @returns {void}
*/
const iterateData = (data, browsers, stats) => {
for (const key in data) {
if (key === '__compat') {
processData(data[key], browsers, stats);
} else {
iterateData(data[key], browsers, stats);
}
}
};
/**
* Get all of the stats
*
* @param {string} folder The folder to show statistics for (or all folders if blank)
* @param {boolean} allBrowsers If true, get stats for all browsers, not just main eight
* @returns {object.<string, VersionStats>?}
*/
const getStats = (folder, allBrowsers) => {
/**
* @constant {string[]}
*/
const browsers = allBrowsers
? Object.keys(bcd.browsers)
: [
'chrome',
'chrome_android',
'edge',
'firefox',
'ie',
'safari',
'safari_ios',
'webview_android',
];
/** @type {object.<string, VersionStats>} */
let stats = { total: { all: 0, true: 0, null: 0, range: 0, real: 0 } };
browsers.forEach(browser => {
stats[browser] = { all: 0, true: 0, null: 0, range: 0, real: 0 };
});
if (folder) {
if (bcd[folder]) {
iterateData(bcd[folder], browsers, stats);
} else {
console.error(chalk`{red.bold Folder "${folder}/" doesn't exist!}`);
return null;
}
} else {
for (const data in bcd) {
if (!(data === 'browsers' || data === 'webextensions')) {
iterateData(bcd[data], browsers, stats);
}
}
}
return stats;
};
/**
* Print statistics of BCD
*
* @param {object.<string, VersionStats>} stats The stats object to print from
* @param {string} folder The folder to show statistics for (or all folders if blank)
* @returns {void}
*/
const printStats = (stats, folder) => {
if (!stats) {
console.error(`No stats${folder ? ` for folder ${folder}` : ''}!`);
return;
}
let releaseDate = getRefDate('v' + process.env.npm_package_version).slice(
0,
'YYYY-MM-DD'.length,
);
console.log(
chalk`{bold Status as of version ${
process.env.npm_package_version
} (released on ${releaseDate}) for ${
folder ? `the \`${folder}/\` directory` : 'web platform features'
}}: \n`,
);
let table = `| browser | real values | ranged values | \`true\` values | \`null\` values |
| --- | --- | --- | --- | --- |
`;
Object.keys(stats).forEach(entry => {
table += `| ${entry.replace('_', ' ')} | `;
table += `${((stats[entry].real / stats[entry].all) * 100).toFixed(2)}% | `;
table += `${((stats[entry].range / stats[entry].all) * 100).toFixed(
2,
)}% | `;
table += `${((stats[entry].true / stats[entry].all) * 100).toFixed(2)}% | `;
table += `${((stats[entry].null / stats[entry].all) * 100).toFixed(2)}% |
`;
});
console.log(table);
};
if (require.main === module) {
printStats(getStats(argv.folder, argv.all), argv.folder);
}
module.exports = getStats;