kinship: compute cousin type by degree and removal

Signed-off-by: Arc676/Alessandro Vinciguerra <alesvinciguerra@gmail.com>
This commit is contained in:
Arc676/Alessandro Vinciguerra committed 2026-11-01 10:40:54 +01:00
1 parent e2468f6d39
commit e0f7eb4913
2 files changed
+33 -4

No files matched your search

+28 -4
View File
@@ -3,6 +3,32 @@ import { kinshipFacts } from './facts.js'
// Mot genré quand le sexe de la personne nommée est connu, forme neutre ou double sinon
const g = (sex, male, female, neutral) => (sex === 'male' ? male : sex === 'female' ? female : neutral)
function ordinal(n) {
switch (n) {
case 1:
return "First"
case 2:
return "Second"
case 3:
return "Third"
default:
return `{n}th`
}
}
function times(n) {
switch (n) {
case 1:
return "once"
case 2:
return "twice"
case 3:
return "thrice"
default:
return `${n} times`
}
}
export default function nameEn(path) {
const { ups, downs, hasSpouse, sex } = kinshipFacts(path)
@@ -36,10 +62,8 @@ export default function nameEn(path) {
else if (ups >= 2 && downs >= 2) {
const degree = Math.min(ups, downs) - 1
const removed = Math.abs(ups - downs)
if (degree === 1 && removed === 0) label = 'First cousin'
else if (degree === 1) label = 'First cousin once removed'
else if (removed === 0) label = `Cousin, degree ${degree}`
else label = 'Distant cousin'
if (removed === 0) label = `${ordinal(degree)} cousin`
else label = `${ordinal(degree)} cousin, ${times(removed)} removed`
} else label = `${ups + downs} steps apart`
return hasSpouse ? `${label} by marriage` : label
@@ -67,6 +67,11 @@ describe('nommage anglais', () => {
expect(bothEn(['spouse', 'sibling'])).toEqual(['Brother-in-law', 'Sister-in-law', 'Brother/sister-in-law'])
expect(bothEn(['spouse'])).toEqual(['Partner', 'Partner', 'Partner'])
expect(bothEn(['up', 'sibling', 'down'])).toEqual(['First cousin', 'First cousin', 'First cousin'])
expect(bothEn(['up', 'up', 'sibling', 'down', 'down'])).toEqual(['Second cousin', 'Second cousin', 'Second cousin'])
expect(bothEn(['up', 'up', 'sibling', 'down'])).toEqual(['First cousin, once removed', 'First cousin, once removed', 'First cousin, once removed'])
expect(bothEn(['up', 'up', 'up', 'up', 'up', 'up', 'sibling', 'down', 'down', 'down'])).toEqual(['Third cousin, thrice removed', 'Third cousin, thrice removed', 'Third cousin, thrice removed'])
expect(bothEn(['up', 'up', 'up', 'sibling', 'down'])).toEqual(['First cousin, twice removed', 'First cousin, twice removed', 'First cousin, twice removed'])
expect(bothEn(['up', 'up', 'up', 'up', 'up', 'sibling', 'down'])).toEqual(['First cousin, 4 times removed', 'First cousin, 4 times removed', 'First cousin, 4 times removed'])
})
it('une langue inconnue retombe sur l’anglais', () => {