Skip to content

Commit

Permalink
Cleanup .find/.findIndex/.every/.some usage (#2889)
Browse files Browse the repository at this point in the history
This primarily fixes the handful of places we use .find or .findIndex where .every or .some is more appropriate.
  • Loading branch information
imnasnainaec authored Feb 12, 2024
1 parent 81dd270 commit c2635ba
Show file tree
Hide file tree
Showing 17 changed files with 45 additions and 56 deletions.
10 changes: 2 additions & 8 deletions src/backend/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,8 @@ axiosInstance.interceptors.response.use(undefined, (err: AxiosError) => {
status >= StatusCodes.BAD_REQUEST &&
status <= StatusCodes.NETWORK_AUTHENTICATION_REQUIRED
) {
// Suppress error pop-ups for URLs the frontend already explicitly
// handles.
if (
url !== undefined &&
whiteListedErrorUrls.some((whiteListedUrl) =>
url.endsWith(whiteListedUrl)
)
) {
// Suppress error pop-ups for URLs the frontend already explicitly handles.
if (url && whiteListedErrorUrls.some((u) => url.endsWith(u))) {
return Promise.reject(err);
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/AppBar/AppBarTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface TabProps {
}

export function tabColor(currentTab: Path, tabName: Path): string {
return currentTab.indexOf(tabName) !== -1
return currentTab.indexOf(tabName) > -1
? themeColors.darkShade
: themeColors.lightShade;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ interface SenseListProps {
export function SenseList(props: SenseListProps): ReactElement {
const { t } = useTranslation();

const hasPartsOfSpeech = !!props.selectedWord.senses.find(
const hasPartsOfSpeech = props.selectedWord.senses.some(
(s) => s.grammaticalInfo.catGroup !== GramCatGroup.Unspecified
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ interface VernListProps {
export function VernList(props: VernListProps): ReactElement {
const { t } = useTranslation();

const hasPartsOfSpeech = !!props.vernacularWords.find((w) =>
w.senses.find(
const hasPartsOfSpeech = props.vernacularWords.some((w) =>
w.senses.some(
(s) => s.grammaticalInfo.catGroup !== GramCatGroup.Unspecified
)
);
Expand Down
27 changes: 13 additions & 14 deletions src/components/DataEntry/DataEntryTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export function addSemanticDomainToSense(
if (!sense) {
throw new Error("Word has no sense with specified guid");
}
if (sense.semanticDomains.find((s) => s.id == semDom.id)) {
if (sense.semanticDomains.some((s) => s.id == semDom.id)) {
return word;
}
sense.semanticDomains.push(makeSemDomCurrent(semDom));
Expand Down Expand Up @@ -302,7 +302,7 @@ export default function DataEntryTable(
const switchSense = useCallback(
(oldGuid: string, newGuid: string): void => {
const entry = state.recentWords.find((w) => w.senseGuid === oldGuid);
if (!entry || !entry.word.senses.find((s) => s.guid === newGuid)) {
if (!entry || entry.word.senses.every((s) => s.guid !== newGuid)) {
return;
}
setState((prevState) => {
Expand All @@ -325,7 +325,7 @@ export default function DataEntryTable(
setState((prevState) => {
const recentWords = [...prevState.recentWords];
word.senses.forEach((s) => {
if (s.semanticDomains.find((dom) => dom.id === domId)) {
if (s.semanticDomains.some((dom) => dom.id === domId)) {
recentWords.push({ word, senseGuid: s.guid });
}
});
Expand Down Expand Up @@ -455,8 +455,8 @@ export default function DataEntryTable(
const soloSense =
selectedDup?.senses.length === 1 ? selectedDup.senses[0] : undefined;
const emptySense =
soloSense?.definitions.find((d) => d.text) ||
soloSense?.glosses.find((g) => g.def)
soloSense?.definitions.some((d) => d.text) ||
soloSense?.glosses.some((g) => g.def)
? undefined
: soloSense;

Expand Down Expand Up @@ -509,7 +509,7 @@ export default function DataEntryTable(
if (!oldEntry) {
return;
}
if (!oldEntry.word.senses.find((s) => s.guid === newGuid)) {
if (oldEntry.word.senses.every((s) => s.guid !== newGuid)) {
return;
}
switchSense(oldGuid, newGuid);
Expand All @@ -530,7 +530,7 @@ export default function DataEntryTable(
// and only keep Retire ids if they are still in the display.
if (
prevState.defunctWordIds[id] === DefunctStatus.Recent ||
prevState.recentWords.find((w) => w.word.id === id)
prevState.recentWords.some((w) => w.word.id === id)
) {
defunctWordIds[id] = DefunctStatus.Retire;
}
Expand Down Expand Up @@ -571,7 +571,7 @@ export default function DataEntryTable(
return;
}
const oldId = ids.find((id) =>
state.recentWords.find((w) => w.word.id === id)
state.recentWords.some((w) => w.word.id === id)
);
if (oldId) {
// Do an update if there's one to be done.
Expand Down Expand Up @@ -655,8 +655,7 @@ export default function DataEntryTable(
audio: Pronunciation[],
oldId: string
): Promise<void> => {
const isInDisplay =
state.recentWords.findIndex((w) => w.word.id === oldId) > -1;
const isInDisplay = state.recentWords.some((w) => w.word.id === oldId);

defunctWord(oldId);
const newWord = await backend.updateDuplicate(oldId, word);
Expand Down Expand Up @@ -800,7 +799,7 @@ export default function DataEntryTable(
// If selected sense already has this domain, add audio without updating first.
if (
oldSense.glosses[0].def === state.newGloss &&
oldSense.semanticDomains.find((d) => d.id === semDom.id)
oldSense.semanticDomains.some((d) => d.id === semDom.id)
) {
enqueueSnackbar(
t("addWords.senseInWord", {
Expand Down Expand Up @@ -831,7 +830,7 @@ export default function DataEntryTable(
// Otherwise, if new gloss matches a sense, update that sense.
for (const sense of oldWord.senses) {
if (sense.glosses?.length && sense.glosses[0].def === state.newGloss) {
if (sense.semanticDomains.find((d) => d.id === semDom.id)) {
if (sense.semanticDomains.some((d) => d.id === semDom.id)) {
// User is trying to add a sense that already exists.
enqueueSnackbar(
t("addWords.senseInWord", {
Expand Down Expand Up @@ -955,8 +954,8 @@ export default function DataEntryTable(

// If a sense with a new guid was added, it needs to replace the old sense in the display.
if (newWord.senses.length > oldEntry.word.senses.length) {
const newSense = newWord.senses.find(
(sense) => !oldEntry.word.senses.find((s) => s.guid === sense.guid)
const newSense = newWord.senses.find((sense) =>
oldEntry.word.senses.every((s) => s.guid !== sense.guid)
);
if (newSense) {
queueSenseSwitch(oldEntry.senseGuid, newSense.guid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ describe("DataEntryTable", () => {
// Confirm the semantic domain was added.
const wordUpdated: Word = mockUpdateWord.mock.calls[0][0];
const doms = wordUpdated.senses[0].semanticDomains;
expect(doms.find((d) => d.id === mockSemDomId)).toBeTruthy();
expect(doms.some((d) => d.id === mockSemDomId)).toBeTruthy();
});
});
});
Expand Down
4 changes: 2 additions & 2 deletions src/components/DataEntry/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { DomainWord } from "types/word";
* (and in the specified domain if domainId is provided). */
function isActiveInDomain(sense: Sense, domainId?: string): boolean {
return (
(!domainId || !!sense.semanticDomains.find((d) => d.id === domainId)) &&
(!domainId || sense.semanticDomains.some((d) => d.id === domainId)) &&
// The undefined is for Statuses created before .accessibility was required.
[Status.Active, Status.Protected, undefined].includes(sense.accessibility)
);
Expand All @@ -18,7 +18,7 @@ export function filterWordsWithSenses(
domainId?: string
): Word[] {
return words.filter((w) =>
w.senses.find((s) => isActiveInDomain(s, domainId))
w.senses.some((s) => isActiveInDomain(s, domainId))
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default function DateScheduleEdit(
d.getMonth() === date.getMonth() &&
d.getDate() === date.getDate()
);
if (index >= 0) {
if (index > -1) {
schedule.splice(index, 1);
} else {
schedule.push(date);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@ export default function ProjectPickersDay(
): ReactElement {
const { days, ...pickersDayProps } = props;
const date = pickersDayProps.day.toDate();
const selected = days
? days.findIndex(
(d) =>
d.getDate() === date.getDate() &&
d.getMonth() === date.getMonth() &&
d.getFullYear() === date.getFullYear()
) > -1
: false;
const selected = days?.some(
(d) =>
d.getDate() === date.getDate() &&
d.getMonth() === date.getMonth() &&
d.getFullYear() === date.getFullYear()
);

return <PickersDay {...pickersDayProps} selected={selected} />;
}
2 changes: 1 addition & 1 deletion src/components/ProjectSettings/ProjectSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default function ProjectSelect(

// This prevents an out-of-range Select error while useEffect is underway.
const projectList = [...projList];
if (!projectList.find((p) => p.name === props.project.name)) {
if (projectList.every((p) => p.name !== props.project.name)) {
projectList.push(props.project);
}
projectList.sort((a: Project, b: Project) => a.name.localeCompare(b.name));
Expand Down
2 changes: 1 addition & 1 deletion src/components/ProjectSettings/tests/SettingsTabTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function tabHasSomeSetting(
tab: ProjectSettingsTab,
settings: Setting[]
): boolean {
return settingsByTab[tab].findIndex((s) => settings.includes(s)) !== -1;
return settingsByTab[tab].some((s) => settings.includes(s));
}

/** Given a project permission `perm` and a boolean `hasSchedule` (indicating whether any
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function getWordsContainingChar(
): string[] {
const wordsWithChar: string[] = [];
for (const word of words) {
if (word.indexOf(character) !== -1 && !wordsWithChar.includes(word)) {
if (word.indexOf(character) > -1 && !wordsWithChar.includes(word)) {
wordsWithChar.push(word);
if (wordsWithChar.length === maxCount) {
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const characterInventorySlice = createSlice({
}

const index = state.validCharacters.findIndex((c) => c == action.payload);
if (index !== -1) {
if (index > -1) {
state.validCharacters.splice(index, 1);
}

Expand All @@ -39,7 +39,7 @@ const characterInventorySlice = createSlice({
const index = state.rejectedCharacters.findIndex(
(c) => c == action.payload
);
if (index !== -1) {
if (index > -1) {
state.rejectedCharacters.splice(index, 1);
}

Expand Down
2 changes: 1 addition & 1 deletion src/goals/MergeDuplicates/MergeDupsCompleted.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export function doWordsIncludeMerges(
// The undo operation will fail if any of the children are in the frontier.
return (
merge.parentIds.every((id) => wordIds.includes(id)) &&
!merge.childIds.some((id) => wordIds.includes(id))
merge.childIds.every((id) => !wordIds.includes(id))
);
}

Expand Down
18 changes: 8 additions & 10 deletions src/goals/MergeDuplicates/Redux/MergeDupsReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,8 @@ const mergeDuplicatesSlice = createSlice({
const nonDeletedSenses = Object.values(state.tree.words).flatMap((w) =>
Object.values(w.sensesGuids).flatMap((s) => s)
);
const deletedWords = possibleWords.filter(
(w) =>
!w.senses.map((s) => s.guid).find((g) => nonDeletedSenses.includes(g))
const deletedWords = possibleWords.filter((w) =>
w.senses.every((s) => !nonDeletedSenses.includes(s.guid))
);
state.mergeWords = deletedWords.map((w) =>
newMergeWords(w, [{ srcWordId: w.id, getAudio: false }], true)
Expand Down Expand Up @@ -230,7 +229,7 @@ const mergeDuplicatesSlice = createSlice({
const newOrder = action.payload.destOrder;

// Ensure the move is valid.
if (oldOrder !== -1 && newOrder !== undefined && oldOrder !== newOrder) {
if (oldOrder > -1 && newOrder !== undefined && oldOrder !== newOrder) {
// Move the sense pair to its new place.
const pair = sensePairs.splice(oldOrder, 1)[0];
sensePairs.splice(newOrder, 0, pair);
Expand Down Expand Up @@ -341,9 +340,8 @@ function createMergeWords(
if (
onlyChild[0].srcWordId === wordId &&
onlyChild.length === word.senses.length &&
!onlyChild.find(
(ms) =>
![Status.Active, Status.Protected].includes(ms.sense.accessibility)
onlyChild.every((ms) =>
[Status.Active, Status.Protected].includes(ms.sense.accessibility)
) &&
compareFlags(mergeWord.flag, word.flag) === 0
) {
Expand Down Expand Up @@ -371,8 +369,8 @@ function createMergeWords(
parent.senses.push(mergeSense.sense);
}
});
const getAudio = !msList.find(
(ms) => ms.sense.accessibility === Status.Separate
const getAudio = msList.every(
(ms) => ms.sense.accessibility !== Status.Separate
);
return { srcWordId: msList[0].srcWordId, getAudio };
}
Expand Down Expand Up @@ -429,7 +427,7 @@ function combineIntoFirstSense(mergeSenses: MergeTreeSense[]): void {

// Put the duplicate's domains in the main sense if the id is new.
dupSense.semanticDomains.forEach((dom) => {
if (!mainSense.semanticDomains.find((d) => d.id === dom.id)) {
if (mainSense.semanticDomains.every((d) => d.id !== dom.id)) {
mainSense.semanticDomains.push({ ...dom });
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe("MergeDupsReducer", () => {
for (const mergeSenseId of Object.keys(words[wordId].sensesGuids)) {
const guids = words[wordId].sensesGuids[mergeSenseId];
const order = guids.findIndex((g) => g === guid);
if (order !== -1) {
if (order > -1) {
return { wordId, mergeSenseId, order };
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/types/word.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function updateSpeakerInAudio(
!p.protected &&
p.fileName === update.fileName &&
p.speakerId !== update.speakerId;
if (audio.findIndex(updatePredicate) === -1) {
if (!audio.some(updatePredicate)) {
return;
}
return audio.map((a) => (updatePredicate(a) ? update : a));
Expand Down

0 comments on commit c2635ba

Please sign in to comment.