Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add data statistics script for MDN 2019 key result goal #3557

Merged
merged 6 commits into from
Mar 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"scripts": {
"confluence": "node ./node_modules/mdn-confluence/main/generate.es6.js --output-dir=. --bcd-module=./index.js",
"lint": "node test/lint",
"stats": "node scripts/statistics",
"release-notes": "node scripts/release-notes",
"render": "node scripts/render",
"show-errors": "npm test 1> /dev/null",
Expand Down
80 changes: 80 additions & 0 deletions scripts/statistics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
'use strict';
const bcd = require('..');

const browsers = ['chrome', 'chrome_android', 'edge', 'firefox', 'ie', 'safari', 'safari_ios', 'webview_android'];
let stats = { total: { all: 0, true: 0, null: 0, real: 0 } };
browsers.forEach(browser => {
stats[browser] = { all: 0, 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) {
browsers.forEach(function(browser) {
stats[browser].all++;
stats.total.all++;
let real_value = true;
if (!data.support[browser]) {
stats[browser].null++;
stats.total.null++;
real_value = false;
} else {
if (checkSupport(data.support[browser], null)) {
stats[browser].null++;
stats.total.null++;
real_value = false;
}
if (checkSupport(data.support[browser], true)) {
stats[browser].true++;
stats.total.true++;
real_value = false;
}
}
if (real_value) {
stats[browser].real++;
stats.total.real++;
}
});
}
};

const iterateData = (data) => {
for (let key in data) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this will also traverse into strings and arrays, with key being 0, 1, 2 and so on. processData will get called with 1-char strings as arguments and do nothing. This is pretty harmless, just something I was bitten by when writing very similar code with different conditions in processData.

if (key === '__compat') {
processData(data[key]);
} else {
iterateData(data[key]);
}
}
};

for (let data in bcd) {
if (!(data === 'browsers' || data === 'webextensions')) {
iterateData(bcd[data]);
}
}

const printTable = () => {
let table = `| browser | real 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].true / stats[entry].all) * 100).toFixed(2)}% | `;
table += `${((stats[entry].null / stats[entry].all) * 100).toFixed(2)}% |
`;
});

console.log(table);
}

console.log('Status as of version 0.0.xx (released on xx/xx/2019) for web platform features: \n')
printTable();