Skip to content

Commit

Permalink
Ensure creatorsToString() always returns a string
Browse files Browse the repository at this point in the history
The current implementation will return undefined if the creators
array is empty. This causes other functions which expect a string
(which is what TypeScript assumes) to fail.

Add a return type to the function to assert the intention of the
function. We could also have allowed it to return undefined if the
creators array is empty but that seems to be against the naming of the
function, creatorsToString.

Add a check to handle single array and empty creators array
separately. If there are no creators then return an empty string.
  • Loading branch information
kasperg committed Sep 19, 2024
1 parent 3d2fc45 commit 2eb2469
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/core/utils/helpers/general.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEffect, useRef } from "react";
import dayjs from "dayjs";
import { uniq } from "lodash";
import { first, uniq } from "lodash";
import { vi } from "vitest";
import { CoverProps } from "../../../components/cover/cover";
import { UseTextFunction } from "../text";
Expand Down Expand Up @@ -67,13 +67,18 @@ const getCreatorsFromManifestations = (manifestations: Manifestation[]) => {
return Array.from(new Set(creators)) as string[];
};

export const creatorsToString = (creators: string[], t: UseTextFunction) => {
export const creatorsToString = (
creators: string[],
t: UseTextFunction
): string => {
if (creators.length > 1) {
const firstTwo = creators.slice(0, 2);
return `${firstTwo.join(", ")} ${t("etAlText")}`;
}

return creators[0];
if (creators.length === 1) {
return first(creators) as string;
}
return "";
};

export const getCreatorTextFromManifestations = (
Expand Down

0 comments on commit 2eb2469

Please sign in to comment.