From 3a576c1ec0d3a7bc38dfb68a2fb569e41158fee5 Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Fri, 12 Jan 2024 14:34:04 -0500 Subject: [PATCH 1/2] Fix invalid versions --- app/utils.ts | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/app/utils.ts b/app/utils.ts index cf31035..8fd0c3a 100644 --- a/app/utils.ts +++ b/app/utils.ts @@ -58,10 +58,16 @@ export function groupByMajor(downloads: DownloadsResponse['downloads']) { let groups: Record = {}; for (let [version, downloadCount] of Object.entries(downloads)) { - let major = getMajor(version); - - groups[major] ||= 0; - groups[major] += downloadCount; + try { + let major = getMajor(version); + + groups[major] ||= 0; + groups[major] += downloadCount; + } catch (e) { + console.group(`An error occurred and ${version} will be omitted from the dataset`); + console.error(e); + console.groupEnd(); + } } return Object.entries(groups).map(([major, downloadCount]) => { @@ -73,12 +79,18 @@ export function groupByMinor(downloads: DownloadsResponse['downloads']): Grouped let groups: Record = {}; for (let [version, downloadCount] of Object.entries(downloads)) { - let major = getMajor(version); - let minor = getMinor(version); - let majorMinor = `${major}.${minor}`; - - groups[majorMinor] ||= 0; - groups[majorMinor] += downloadCount; + try { + let major = getMajor(version); + let minor = getMinor(version); + let majorMinor = `${major}.${minor}`; + + groups[majorMinor] ||= 0; + groups[majorMinor] += downloadCount; + } catch (e) { + console.group(`An error occurred and ${version} will be omitted from the dataset`); + console.error(e); + console.groupEnd(); + } } return Object.entries(groups).map(([majorMinor, downloadCount]) => { From 01e8e41a1986606ba06497a0d68df70886e75af2 Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Fri, 12 Jan 2024 14:51:31 -0500 Subject: [PATCH 2/2] Use isValid instead of try/catch --- app/utils.ts | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/app/utils.ts b/app/utils.ts index 8fd0c3a..bf8d12f 100644 --- a/app/utils.ts +++ b/app/utils.ts @@ -2,6 +2,7 @@ import semverCoerce from 'semver/functions/coerce'; import semverCompare from 'semver/functions/compare'; import getMajor from 'semver/functions/major'; import getMinor from 'semver/functions/minor'; +import isValid from 'semver/functions/valid'; import type { DownloadsResponse, ErrorResponse } from './types'; @@ -58,16 +59,16 @@ export function groupByMajor(downloads: DownloadsResponse['downloads']) { let groups: Record = {}; for (let [version, downloadCount] of Object.entries(downloads)) { - try { - let major = getMajor(version); - - groups[major] ||= 0; - groups[major] += downloadCount; - } catch (e) { - console.group(`An error occurred and ${version} will be omitted from the dataset`); - console.error(e); - console.groupEnd(); + if (!isValid(version)) { + console.error(`${version} is invalid and will be omitted from the dataset.`); + + continue; } + + let major = getMajor(version); + + groups[major] ||= 0; + groups[major] += downloadCount; } return Object.entries(groups).map(([major, downloadCount]) => { @@ -79,18 +80,18 @@ export function groupByMinor(downloads: DownloadsResponse['downloads']): Grouped let groups: Record = {}; for (let [version, downloadCount] of Object.entries(downloads)) { - try { - let major = getMajor(version); - let minor = getMinor(version); - let majorMinor = `${major}.${minor}`; - - groups[majorMinor] ||= 0; - groups[majorMinor] += downloadCount; - } catch (e) { - console.group(`An error occurred and ${version} will be omitted from the dataset`); - console.error(e); - console.groupEnd(); + if (!isValid(version)) { + console.error(`${version} is invalid and will be omitted from the dataset.`); + + continue; } + + let major = getMajor(version); + let minor = getMinor(version); + let majorMinor = `${major}.${minor}`; + + groups[majorMinor] ||= 0; + groups[majorMinor] += downloadCount; } return Object.entries(groups).map(([majorMinor, downloadCount]) => {