Skip to content

Instantly share code, notes, and snippets.

@mjackson
Last active February 23, 2025 04:50

Revisions

  1. mjackson revised this gist Oct 21, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion download-counts.mjs
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@ async function getDownloads(startYear, endYear = startYear) {

    let totalDownloads = downloadCounts.reduce((memo, n) => memo + n);

    return totalDownloads;
    return totalDownloads.toLocaleString();
    }

    getDownloads(2015, 2022).then(downloads => console.log(downloads));
  2. mjackson revised this gist Oct 21, 2022. 1 changed file with 30 additions and 1 deletion.
    31 changes: 30 additions & 1 deletion download-counts.mjs
    Original file line number Diff line number Diff line change
    @@ -1 +1,30 @@
    ‎‎
    const npmApiUrl = "https://api.npmjs.org/downloads/point";

    async function fetchDownloadCounts(packageName, year) {
    let range = `${year}-01-01:${year}-12-31`;
    let response = await fetch(`${npmApiUrl}/${range}/${packageName}`);
    return (await response.json()).downloads;
    }

    async function getDownloads(startYear, endYear = startYear) {
    if (endYear < startYear) {
    throw new Error(
    `End year (${endYear}) must be before start year (${startYear})`
    );
    }

    let years = [];
    for (let year = startYear; year <= endYear; year += 1) {
    years.push(year);
    }

    let downloadCounts = await Promise.all(
    years.map(year => fetchDownloadCounts("react-router", year))
    );

    let totalDownloads = downloadCounts.reduce((memo, n) => memo + n);

    return totalDownloads;
    }

    getDownloads(2015, 2022).then(downloads => console.log(downloads));
  3. mjackson created this gist Oct 21, 2022.
    1 change: 1 addition & 0 deletions download-counts.mjs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    ‎‎