Skip to content

Commit

Permalink
[numeric-no-underscore] Simplifying logic for getUniqueAnswerForms, a…
Browse files Browse the repository at this point in the history
…s we don't need the full scope of what isEqual was providing previously.

<see below>
  • Loading branch information
SonicScrewdriver committed Jan 20, 2025
1 parent c907b26 commit 1051dc3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 28 deletions.
2 changes: 1 addition & 1 deletion packages/perseus/src/widgets/numeric-input/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe("generateExamples", () => {
},
{
name: "integer",
simplify: "optional",
simplify: "required",
},
];
const fakePerseusStrings: Partial<PerseusStrings> = {
Expand Down
38 changes: 11 additions & 27 deletions packages/perseus/src/widgets/numeric-input/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,33 +84,17 @@ export const shouldShowExamples = (
const getUniqueAnswerForms = function (
list: readonly PerseusNumericInputAnswerForm[],
): PerseusNumericInputAnswerForm[] {
return list.reduce<PerseusNumericInputAnswerForm[]>(
(uniqueList, element) => {
// Check if the element is already in the list.
const inList = uniqueList.some((uniqueElement) =>
compareAnswerForms(element, uniqueElement),
);
// If it's in the list, return the list as is
if (inList) {
return uniqueList;
}
// If it's not in the list, add it.
return uniqueList.concat([element]);
},
[],
);
};

/**
* This is a helper function to compare two answer forms
* to see if they are identical. Given that the answer forms
* are simple objects, we can just compare the properties.
*/
const compareAnswerForms = function (
a: PerseusNumericInputAnswerForm,
b: PerseusNumericInputAnswerForm,
): boolean {
return a.simplify === b.simplify && a.name === b.name;
// We use a Set to keep track of the forms we've already seen.
const foundForms = new Set<string>();
return list.filter((form) => {
// If we've already seen this form, skip it.
if (foundForms.has(form.name)) {
return false;
}
// Otherwise, add it to the set and return true.
foundForms.add(form.name);
return true;
});
};

/**
Expand Down

0 comments on commit 1051dc3

Please sign in to comment.