Ajout de helper "accord"

pour accorder selon le genre
This commit is contained in:
Vincent Vandemeulebrouck 2021-02-25 02:00:27 +01:00
parent eaeb164627
commit 382ea2c485
2 changed files with 39 additions and 7 deletions

View File

@ -1,11 +1,12 @@
const articlesApostrophes = { const articlesApostrophes = {
'de' : 'd\'', 'de': 'd\'',
'le' : 'l\'', 'le': 'l\'',
'la' : 'l\'' 'la': 'l\''
} }
export class Grammar { export class Grammar {
/* -------------------------------------------- */
static apostrophe(article, word) { static apostrophe(article, word) {
if (articlesApostrophes[article] && Grammar.startsWithVoyel(word)) { if (articlesApostrophes[article] && Grammar.startsWithVoyel(word)) {
return articlesApostrophes[article] + word return articlesApostrophes[article] + word
@ -13,28 +14,58 @@ export class Grammar {
return article + ' ' + word; return article + ' ' + word;
} }
/* -------------------------------------------- */
static startsWithVoyel(word) { static startsWithVoyel(word) {
return word.match(/^[aeiouy]/i) return word.match(/^[aeiouy]/i)
} }
/* -------------------------------------------- */
static toLowerCaseNoAccent(words) { static toLowerCaseNoAccent(words) {
return words?.toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "") ?? words; return words?.toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "") ?? words;
} }
/* -------------------------------------------- */
static articleDetermine(genre) { static articleDetermine(genre) {
switch (genre?.toLowerCase()) { switch (toLowerCaseNoAccent(genre)) {
case 'f': case 'feminin': return 'la'; case 'f': case 'feminin': return 'la';
case 'p': case 'pluriel': return 'les'; case 'p': case 'mp': case 'fp': case 'pluriel': return 'les';
default: default:
case 'm': case 'masculin': return 'le'; case 'm': case 'masculin': return 'le';
} }
} }
/* -------------------------------------------- */
static articleIndétermine(genre) { static articleIndétermine(genre) {
switch (genre?.toLowerCase()) { switch (toLowerCaseNoAccent(genre)) {
case 'f': case 'feminin': return 'une'; case 'f': case 'feminin': return 'une';
case 'p': case 'pluriel': return 'des'; case 'p': case 'fp': case 'mp': case 'pluriel': return 'des';
case 'n': case 'neutre': return 'du' case 'n': case 'neutre': return 'du'
default: default:
case 'm': case 'masculin': return 'un'; case 'm': case 'masculin': return 'un';
} }
} }
/* -------------------------------------------- */
/**
* renvoie un des mots en fonction du genre:
*
* - masculin/neutre/m/n : mots[0]
* - feminin/f : mots[1]
* - pluriel/mp/p : mots[2]
* - fp : mots[3]
*
* @param {*} genre
* @param {...any} mots
*/
static accord(genre, ...mots) {
switch (toLowerCaseNoAccent(genre)) {
default:
case 'n': case 'neutre':
case 'm': case 'masculin': return mots[0];
case 'f': case 'feminin': return mots[1];
case 'p': case 'mp': case 'pluriel': return mots[2]
case 'fp': return mots[3];
}
}
} }

View File

@ -274,6 +274,7 @@ export class RdDUtility {
Handlebars.registerHelper('upper', str => str?.toUpperCase() ?? 'NULL'); Handlebars.registerHelper('upper', str => str?.toUpperCase() ?? 'NULL');
Handlebars.registerHelper('le', str => Grammar.articleDetermine(str)); Handlebars.registerHelper('le', str => Grammar.articleDetermine(str));
Handlebars.registerHelper('un', str => Grammar.articleIndetermine(str)); Handlebars.registerHelper('un', str => Grammar.articleIndetermine(str));
Handlebars.registerHelper('accord', (genre, ...args) => Grammar.accord(genre, args));
return loadTemplates(templatePaths); return loadTemplates(templatePaths);
} }