Skip to content

Instantly share code, notes, and snippets.

@HighLiuk
Last active December 21, 2022 23:50
Show Gist options
  • Save HighLiuk/fe203e7bffc78c0615a9bb693f9984d5 to your computer and use it in GitHub Desktop.
Save HighLiuk/fe203e7bffc78c0615a9bb693f9984d5 to your computer and use it in GitHub Desktop.
React Weekly Downloads segmented by version

React Weekly Downloads segmented by version

pie showData title By Major Release
"17":6077758
"16":5672055
"18":4997284
"15":360262
"Other":160137
Loading
pie showData title By Minor Release
"17.0":6077758
"18.2":4113997
"16.14":3579091
"16.13":1124089
"18.1":627715
"16.8":295467
"18.0":250010
"16.12":239582
"15.7":207205
"Other":752582
Loading
pie showData title By Patch Release
"17.0.2":5570062
"18.2.0":4113997
"16.14.0":3579091
"16.13.1":1080795
"18.1.0":627715
"17.0.1":479759
"18.0.0":250010
"16.12.0":239582
"15.7.0":207205
"16.8.6":194754
"Other":924526
Loading
/**
* Run these commands:
* `npm install node-fetch@2`
* `node main`
*
* Then enjoy README.md!
*/
const fs = require("fs")
const fetch = require("node-fetch")
/**
* @type {number}
*/
let totalDownloads = 0
/**
* @type {string}
*/
let text = ""
/**
* @param {string} str
* @returns {void}
*/
const write = (str = "") => {
text += str + "\n"
}
/**
* @returns {Promise<{package: string; downloads: Record<string,number>}>}
*/
const get = async () => {
const res = await fetch("https://api.npmjs.org/versions/react/last-week")
return await res.json()
}
/**
* @param {{package: string; downloads: Record<string,number>}} data
* @returns {Record<string, number>}
*/
const getDownloads = (data) => data.downloads
/**
* @param {Record<string,number>} data
* @returns {{ tag: string; downloads: number }[]}
*/
const formatResponse = (data) => {
return Object.entries(data).map(([tag, downloads]) => ({
tag,
downloads,
}))
}
/**
* @param {{ tag: string; downloads: number }[]} versions
* @param {RegExp} regex
* @returns {{ tag: string; downloads: number }[]}
*/
const segmentByVersion = (versions, regex) => {
/** @type {Record<string,number>} */
const versionsMap = {}
for (const version of versions) {
const tag = regex.exec(version.tag)?.[0] ?? ""
versionsMap[tag] ??= 0
versionsMap[tag] += version.downloads
totalDownloads += version.downloads
}
return formatResponse(versionsMap)
}
/**
* @param {{ tag: string; downloads: number }[]} versions
* @returns {{ tag: string; downloads: number }[]}
*/
const sortByDownloads = (versions) => {
return versions.sort((a, b) => {
return a.downloads < b.downloads ? 1 : -1
})
}
/**
* @param {{ tag: string; downloads: number }[]} versions
* @returns {{ tag: string; downloads: number; downloadsPercentage: string }[]}
*/
const addDownloadsPercentage = (versions) => {
const versionsWithPercentage = versions.map((version) => ({
...version,
downloadsPercentage: `${Math.floor(
(100 * version.downloads) / totalDownloads
)}%`,
}))
totalDownloads = 0
return versionsWithPercentage
}
/**
* @param {{ tag: string; downloads: number; downloadsPercentage: string }[]} versions
* @param {string} heading
* @returns {void}
*/
const printResults = (versions, heading) => {
write("```mermaid")
write(`pie showData title ${heading}`)
/**
* @type {number}
*/
let otherDownloads = 0
for (const version of versions) {
if (version.downloadsPercentage === "0%") {
otherDownloads += version.downloads
} else {
write(`"${version.tag}":${version.downloads}`)
}
}
write(`"Other":${otherDownloads}`)
write("```")
write()
}
/**
* @param {string} heading
* @param {RegExp} regex
* @returns {Promise<void>}
*/
const printTable = (heading, regex) => {
return get()
.then(getDownloads)
.then(formatResponse)
.then((v) => segmentByVersion(v, regex))
.then(sortByDownloads)
.then(addDownloadsPercentage)
.then((v) => printResults(v, heading))
}
/**
* @returns {Promise<void>}
*/
const main = async () => {
write("# React Weekly Downloads segmented by version")
write()
await printTable("By Major Release", /^(\d+)/)
await printTable("By Minor Release", /^(\d+\.\d+)/)
await printTable("By Patch Release", /^(\d+\.\d+\.\d+)/)
fs.writeFileSync("README.md", text)
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment