Skip to content

Commit 3173675

Browse files
fix(backend): fix potential duplicates inside the discovered packages
1 parent 9fac94a commit 3173675

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

src/lib/repositories.ts

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,20 +136,42 @@ export const githubRepos = {
136136
sveltejs: [...new Set(repositories.flatMap(([, { repos }]) => repos.map(r => r.repoName)))]
137137
};
138138

139+
/**
140+
* A utility function to only keep unique items in
141+
* an array, based on the uniqTransform parameter.
142+
*
143+
* @param arr the input array
144+
* @param uniqTransform the transformation function
145+
* to make items unique
146+
* @returns the filtered array, containing only unique items
147+
*
148+
* @see {@link https://stackoverflow.com/a/70503699/12070367|Original implementation}
149+
*/
150+
function uniq<T, U>(arr: T[], uniqTransform: (item: T) => U) {
151+
const track = new Set<U>();
152+
return arr.filter(item => {
153+
const value = uniqTransform(item);
154+
return track.has(value) ? false : track.add(value);
155+
});
156+
}
157+
139158
/**
140159
* Get a list of objects containing
141160
* the repo owner, the repo name, and the
142161
* associated transformation function to
143162
* transform a release tag name into a package
144163
* name.
145164
*/
146-
export const transformationRepos = repositories.flatMap(([, { repos }]) =>
147-
repos.map(r => ({
148-
owner: "sveltejs",
149-
repoName: r.repoName,
150-
tagToName: (tag: string) => {
151-
const [name] = r.metadataFromTag(tag);
152-
return name;
153-
}
154-
}))
165+
export const transformationRepos = uniq(
166+
repositories.flatMap(([, { repos }]) =>
167+
repos.map(r => ({
168+
owner: "sveltejs",
169+
repoName: r.repoName,
170+
tagToName: (tag: string) => {
171+
const [name] = r.metadataFromTag(tag);
172+
return name;
173+
}
174+
}))
175+
),
176+
item => item.repoName
155177
);

src/lib/server/package-discoverer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ export class PackageDiscoverer {
3535
const uniquePackages = [...new Set(releases.map(({ tag_name }) => repo.tagToName(tag_name)))];
3636
// 3. Replace or add the value in the array
3737
for (const [i, { owner: o, repoName: n }] of this.#packages.entries()) {
38-
if (o === owner && repoName == n) {
39-
this.#packages[i]!.packages = uniquePackages;
38+
if (o === owner && repoName == n && this.#packages[i]) {
39+
this.#packages[i].packages = uniquePackages;
4040
return;
4141
}
4242
}

0 commit comments

Comments
 (0)