-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #270 from livingtongues/temporal-mayunmarka-glossi…
…ng-sort glossing sort depends on the dictionary glossLanguages order
- Loading branch information
Showing
6 changed files
with
134 additions
and
69 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { orderEntryAndDictionaryGlossLanguages, orderGlosses } from './glosses'; | ||
|
||
describe('orderGlosses', () => { | ||
const $t = (id: string) => { | ||
switch (id) { | ||
case 'gl.de': | ||
return 'German'; | ||
case 'gl.en': | ||
return 'English'; | ||
case 'gl.es': | ||
return 'Spanish'; | ||
default: | ||
return 'other'; | ||
} | ||
}; | ||
|
||
const glosses = { | ||
en: 'apple', | ||
es: 'manzana', | ||
scientific: '<i>Neolamarckia cadamba</i>', | ||
empty: '', | ||
null: null, | ||
de: 'apfel', | ||
}; | ||
const dictionaryGlossLanguages = ['de', 'es', 'en']; | ||
|
||
test('orders based on dictionaryGlossLanguages first', () => { | ||
expect(orderGlosses({ glosses, dictionaryGlossLanguages, $t })).toMatchInlineSnapshot( | ||
` | ||
[ | ||
"apfel", | ||
"manzana", | ||
"apple", | ||
"<i>Neolamarckia cadamba</i>", | ||
] | ||
` | ||
); | ||
}); | ||
|
||
test('adds language label when label set to true', () => { | ||
expect(orderGlosses({ glosses, dictionaryGlossLanguages, $t, label: true })) | ||
.toMatchInlineSnapshot(` | ||
[ | ||
"German: apfel", | ||
"Spanish: manzana", | ||
"English: apple", | ||
"other: <i>Neolamarckia cadamba</i>", | ||
] | ||
`); | ||
}); | ||
|
||
test('handles an empty glosses object', () => { | ||
expect(orderGlosses({ glosses: {}, dictionaryGlossLanguages, $t })).toMatchInlineSnapshot('[]'); | ||
}); | ||
|
||
test('example implementation with join and italics removal', () => { | ||
expect( | ||
orderGlosses({ glosses, dictionaryGlossLanguages, $t }) | ||
.join(', ') | ||
.replace(/<\/?i>/g, '') + '.' | ||
).toMatchInlineSnapshot('"apfel, manzana, apple, Neolamarckia cadamba."'); | ||
}); | ||
}); | ||
|
||
describe('orderEntryAndDictionaryGlossLanguages', () => { | ||
test('places dictionary gloss languages first, then leftovers from gloss object but does not duplicate', () => { | ||
expect(orderEntryAndDictionaryGlossLanguages({ es: '', en: '' }, ['en', 'de'])) | ||
.toMatchInlineSnapshot(` | ||
[ | ||
"en", | ||
"de", | ||
"es", | ||
] | ||
`); | ||
}); | ||
}); |
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,58 +1,27 @@ | ||
import type { IGloss } from '@living-dictionaries/types'; | ||
|
||
export function printGlosses(glosses: IGloss, t: (id: string) => string, { shorten = false } = {}) { | ||
return Object.keys(glosses) | ||
.filter((bcp) => glosses[bcp]) | ||
.sort() // sort by dictionary's gloss language order in the future | ||
.map((bcp) => { | ||
const gloss = glosses[bcp]; | ||
if (shorten) return gloss; | ||
return `${t('gl.' + bcp)}: ${gloss}`; | ||
}); | ||
} | ||
|
||
if (import.meta.vitest) { | ||
const t = (id) => (id === 'gl.en' ? 'English' : 'Spanish'); | ||
test('printGlosses', () => { | ||
const gloss = { | ||
en: 'apple', | ||
es: 'arbol', | ||
scientific: '<i>Neolamarckia cadamba</i>', | ||
empty: '', | ||
null: null, | ||
}; | ||
expect(printGlosses(gloss, t, { shorten: true })).toMatchInlineSnapshot(` | ||
[ | ||
"apple", | ||
"arbol", | ||
"<i>Neolamarckia cadamba</i>", | ||
] | ||
`); | ||
|
||
expect(printGlosses(gloss, t)).toMatchInlineSnapshot(` | ||
[ | ||
"English: apple", | ||
"Spanish: arbol", | ||
"Spanish: <i>Neolamarckia cadamba</i>", | ||
] | ||
`); | ||
|
||
expect( | ||
printGlosses(gloss, t, { shorten: true }) | ||
.join(', ') | ||
.replace(/<\/?i>/g, '') + '.' | ||
).toMatchInlineSnapshot('"apple, arbol, Neolamarckia cadamba."'); | ||
|
||
expect(printGlosses({}, t)).toMatchInlineSnapshot('[]'); | ||
export function orderGlosses({ glosses, dictionaryGlossLanguages, $t, label = false }: | ||
{ | ||
glosses: IGloss; | ||
dictionaryGlossLanguages: string[], | ||
$t: (id: string) => string, | ||
label?: boolean | ||
} | ||
) { | ||
const sortedGlossLanguages = orderEntryAndDictionaryGlossLanguages(glosses, dictionaryGlossLanguages); | ||
const glossLanguagesWithGloss = sortedGlossLanguages.filter((bcp) => glosses[bcp]) | ||
return glossLanguagesWithGloss.map((bcp) => { | ||
const gloss = glosses[bcp]; | ||
if (label) return `${$t('gl.' + bcp)}: ${gloss}`; | ||
return gloss; | ||
}); | ||
} | ||
|
||
export function showEntryGlossLanguages( | ||
entryGlosses: { [key: string]: string }, | ||
dictionaryLanguages: string[] | ||
export function orderEntryAndDictionaryGlossLanguages( | ||
glosses: IGloss, | ||
dictionaryGlossLanguages: string[] | ||
) { | ||
if (entryGlosses) { | ||
return [...new Set([...dictionaryLanguages, ...Object.keys(entryGlosses)])]; | ||
} | ||
return [...new Set(dictionaryLanguages)]; | ||
const combinedGlossingLanguages = [...dictionaryGlossLanguages, ...Object.keys(glosses || {})] | ||
const deduplicatedGlossingLanguages = [...new Set(combinedGlossingLanguages)] | ||
return deduplicatedGlossingLanguages; | ||
} |
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
80d973e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
living-dictionaries – ./packages/site
living-dictionaries.vercel.app
living-dictionaries-livingtongues.vercel.app
www.livingdictionaries.app
living-dictionaries-git-main-livingtongues.vercel.app
livingdictionaries.app