-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ExistingDataTable] Show all glosses, with primary analysis lang first (
- Loading branch information
1 parent
28f5ce6
commit d7d73e5
Showing
8 changed files
with
123 additions
and
101 deletions.
There are no files selected for viewing
46 changes: 22 additions & 24 deletions
46
src/components/DataEntry/ExistingDataTable/ImmutableExistingData.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 9 additions & 5 deletions
14
src/components/DataEntry/ExistingDataTable/tests/ImmutableExistingData.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,51 @@ | ||
import { Status, Word } from "api/models"; | ||
import { Sense, Status, Word } from "api/models"; | ||
import { DomainWord } from "types/word"; | ||
|
||
/** Filter out words that do not have at least 1 active sense */ | ||
export function filterWordsWithSenses(words: Word[]): Word[] { | ||
/** Checks whether a sense is active | ||
* (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)) && | ||
// The undefined is for Statuses created before .accessibility was required. | ||
[Status.Active, Status.Protected, undefined].includes(sense.accessibility) | ||
); | ||
} | ||
|
||
/** Filter out words that do not have at least 1 active sense | ||
* (and in the specified domain if domainId is provided). */ | ||
export function filterWordsWithSenses( | ||
words: Word[], | ||
domainId?: string | ||
): Word[] { | ||
return words.filter((w) => | ||
w.senses.find((s) => | ||
[Status.Active, Status.Protected].includes(s.accessibility) | ||
) | ||
w.senses.find((s) => isActiveInDomain(s, domainId)) | ||
); | ||
} | ||
|
||
/** Filter out sense's glosses with empty def | ||
* (and if lang is specified, put glosses in that lang first). */ | ||
function filterGlosses(sense: Sense, lang?: string): Sense { | ||
const glosses = sense.glosses.filter((g) => g.def.trim()); | ||
if (lang) { | ||
glosses.sort((a, b) => +(b.language === lang) - +(a.language === lang)); | ||
} | ||
return { ...sense, glosses }; | ||
} | ||
|
||
export function filterWordsByDomain( | ||
words: Word[], | ||
domainId: string | ||
domainId: string, | ||
lang?: string | ||
): DomainWord[] { | ||
const domainWords: DomainWord[] = []; | ||
for (const currentWord of words) { | ||
const senses = currentWord.senses.filter((s) => | ||
// The undefined is for Statuses created before .accessibility was required in the frontend. | ||
[Status.Active, Status.Protected, undefined].includes(s.accessibility) | ||
const wordsInDomain = filterWordsWithSenses(words, domainId); | ||
wordsInDomain.sort((a, b) => a.vernacular.localeCompare(b.vernacular)); | ||
for (const w of wordsInDomain) { | ||
domainWords.push( | ||
...w.senses | ||
.filter((s) => isActiveInDomain(s, domainId)) | ||
.map((s) => new DomainWord({ ...w, senses: [filterGlosses(s, lang)] })) | ||
); | ||
for (const sense of senses) { | ||
if (sense.semanticDomains.map((dom) => dom.id).includes(domainId)) { | ||
// Only the first gloss is shown, and no definitions. | ||
domainWords.push(new DomainWord({ ...currentWord, senses: [sense] })); | ||
} | ||
} | ||
} | ||
return domainWords; | ||
} | ||
|
||
export function sortDomainWordsByVern(words: DomainWord[]): DomainWord[] { | ||
return words.sort( | ||
(a, b) => | ||
a.vernacular.localeCompare(b.vernacular) || | ||
a.gloss.def.localeCompare(b.gloss.def) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters