Created
September 7, 2023 08:12
-
-
Save ddbeck/66e73c1a5f06dadaf00f35d8e8d909b5 to your computer and use it in GitHub Desktop.
Statcounter + BCD cleanup tools
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { query } from '../src/index.js'; // You'll have to sort this bit out yourself. It's the pre-TS version of https://github.com/mdn/browser-compat-data/blob/main/utils/query.ts | |
import TSV from 'tsv'; | |
const browsers = [ | |
'chrome', | |
'edge', | |
'firefox', | |
'opera', | |
'safari', | |
'samsunginternet_android', | |
].map(q => query(`browsers.${q}`)); | |
const lines = []; | |
for (const data of browsers) { | |
for (const [version, details] of Object.entries(data.releases)) { | |
lines.push({ | |
browser: data.name, | |
version, | |
concatenate: data.name + ' ' + version, | |
release_date: details.release_date, | |
}); | |
} | |
} | |
console.log(TSV.stringify(lines)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { parse } from 'csv-parse/sync'; | |
import TSV from 'tsv'; | |
import { readFileSync } from 'node:fs'; | |
// e.g., https://gs.statcounter.com/browser-version-market-share | |
const PATH_TO_STATCOUNTER_CSV = 'statcounter-july-2023.csv'; | |
const re = { | |
edge: /(Edge) ([\d.]+)/, | |
chrome: /(Chrome) ([\d.]+)/, | |
safari: /(Safari) ([\d.]+)/, | |
firefox: /(Firefox) ([\d.]+)/, | |
samsunginternet_android: /(Samsung Internet) ([\d.]+)/, | |
opera: /(Opera) ([\d.]+)/, | |
}; | |
function cleanVersionNumber(browser, version) { | |
if (version === -1) return '-1'; | |
if (browser === 'Samsung Internet') return version; | |
if (version.endsWith('.0')) return version.match(/(\d+)[.]0/)[1]; // BCD doesn't do .0 version numbers, except for Samsung Internet | |
return version; | |
} | |
const errBrowser = 'NOT IN BCD'; | |
function splitBrowserVersions(str) { | |
// Browsers where Statcounter does something odd or combines releases | |
if ( | |
[ | |
'Chrome for Android', | |
'Safari iPhone', | |
'Chrome for iPhone', | |
'Safari iPad', | |
'Android 0', | |
'Chrome iPad', | |
'360 Safe Browser 0', | |
'Mozilla 0', | |
'Sogou Explorer 0', | |
'Sony PS4 0', | |
'Unknown 0', | |
'Other', | |
].includes(str) | |
) { | |
return [str, errBrowser, '-1']; | |
} | |
// Browsers unknown to BCD | |
if ( | |
str.startsWith('UC Browser ') || | |
str.startsWith('Yandex Browser ') || | |
str.startsWith('QQ Browser ') || | |
str.startsWith('Coc Coc ') || | |
str.startsWith('KaiOS ') || | |
str.startsWith('Whale ') || | |
str.startsWith('Opera Mini ') || | |
str.startsWith('Instabridge ') || | |
str.startsWith('IE ') || | |
str.startsWith('Puffin ') | |
) { | |
return [str, errBrowser, '-1']; | |
} | |
for (const exp of Object.values(re)) { | |
const result = str.match(exp); | |
if (Array.isArray(result)) { | |
return [str, ...result.slice(1)]; | |
} | |
} | |
throw Error(`unhandled string: ${str}`); | |
} | |
const statcounter = parse( | |
readFileSync(PATH_TO_STATCOUNTER_CSV), | |
{ | |
columns: true, | |
skip_empty_lines: true, | |
}, | |
); | |
const lines = []; | |
for (const row of statcounter) { | |
const browserVersion = row['Browser Version']; | |
const marketSharePerc = row['Market Share Perc. (July 2023)']; | |
const [original, browser, version] = splitBrowserVersions(browserVersion); | |
lines.push({ | |
original, | |
browser, | |
version: cleanVersionNumber(browser, version), | |
marketSharePerc, | |
}); | |
} | |
console.log(TSV.stringify(lines)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment