Skip to content

Commit 2b4a3a3

Browse files
committed
Handle comparing and unique version
1 parent 42bb04e commit 2b4a3a3

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

app/api/versions/route.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,28 @@ async function getSecondaryResponse(os: TPrimaryPlatforms, currentResults: Resul
8585
}))
8686
})
8787

88-
console.log(JSON.stringify(secondaryDownloads))
89-
9088
return secondaryDownloads
9189
} catch (e) {
9290
throw Error("Unable to fetch secondary data.")
9391
}
9492
}
9593

94+
function compareVersions(a: string, b: string): number {
95+
const aParts = a.split('.').map(Number)
96+
const bParts = b.split('.').map(Number)
97+
98+
for (let i = 0; i < Math.max(aParts.length, bParts.length); i++) {
99+
const aPart = aParts[i] || 0
100+
const bPart = bParts[i] || 0
101+
102+
if (aPart !== bPart) {
103+
return bPart - aPart
104+
}
105+
}
106+
107+
return 0
108+
}
109+
96110
async function getVersions(os: TPrimaryPlatforms): Promise<Result> {
97111
const result: Result = []
98112

@@ -110,7 +124,17 @@ async function getVersions(os: TPrimaryPlatforms): Promise<Result> {
110124
result.push(...secondary)
111125
}
112126

113-
return result
127+
const uniqueVersions = new Map<string, { version: string, url: string }>()
128+
for (const item of result) {
129+
if (!uniqueVersions.has(item.version)) {
130+
uniqueVersions.set(item.version, item)
131+
}
132+
}
133+
134+
const deduplicated = Array.from(uniqueVersions.values())
135+
deduplicated.sort((a, b) => compareVersions(a.version, b.version))
136+
137+
return deduplicated
114138
}
115139

116140
export async function GET(request: NextRequest) {

0 commit comments

Comments
 (0)