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

@ -6,6 +6,7 @@ const articlesApostrophes = {
}
export class Grammar {
/* -------------------------------------------- */
static apostrophe(article, word) {
if (articlesApostrophes[article] && Grammar.startsWithVoyel(word)) {
return articlesApostrophes[article] + word
@ -13,28 +14,58 @@ export class Grammar {
return article + ' ' + word;
}
/* -------------------------------------------- */
static startsWithVoyel(word) {
return word.match(/^[aeiouy]/i)
}
/* -------------------------------------------- */
static toLowerCaseNoAccent(words) {
return words?.toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "") ?? words;
}
/* -------------------------------------------- */
static articleDetermine(genre) {
switch (genre?.toLowerCase()) {
switch (toLowerCaseNoAccent(genre)) {
case 'f': case 'feminin': return 'la';
case 'p': case 'pluriel': return 'les';
case 'p': case 'mp': case 'fp': case 'pluriel': return 'les';
default:
case 'm': case 'masculin': return 'le';
}
}
/* -------------------------------------------- */
static articleIndétermine(genre) {
switch (genre?.toLowerCase()) {
switch (toLowerCaseNoAccent(genre)) {
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'
default:
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('le', str => Grammar.articleDetermine(str));
Handlebars.registerHelper('un', str => Grammar.articleIndetermine(str));
Handlebars.registerHelper('accord', (genre, ...args) => Grammar.accord(genre, args));
return loadTemplates(templatePaths);
}