foundryvtt-reve-de-dragon/module/grammar.js

80 lines
2.3 KiB
JavaScript
Raw Permalink Normal View History

2020-12-08 03:04:00 +01:00
const articlesApostrophes = {
'de': 'd\'',
'le': 'l\'',
'la': 'l\''
2020-12-08 03:04:00 +01:00
}
export class Grammar {
/* -------------------------------------------- */
2020-12-08 03:04:00 +01:00
static apostrophe(article, word) {
if (articlesApostrophes[article] && Grammar.startsWithVoyel(word)) {
return articlesApostrophes[article] + word
}
return article + ' ' + word;
}
2021-01-26 19:47:18 +01:00
/* -------------------------------------------- */
2020-12-08 03:04:00 +01:00
static startsWithVoyel(word) {
return word.match(/^[aeiouy]/i)
}
2021-01-26 19:47:18 +01:00
static includesLowerCaseNoAccent(value, content) {
return Grammar.toLowerCaseNoAccent(value).includes(Grammar.toLowerCaseNoAccent(content));
}
/* -------------------------------------------- */
2021-01-26 19:47:18 +01:00
static toLowerCaseNoAccent(words) {
return words?.toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "") ?? words;
}
/* -------------------------------------------- */
static toLowerCaseNoAccentNoSpace(words) {
return words?.toLowerCase().normalize("NFD").replace(/[ \u0300-\u036f]/g, "") ?? words;
}
/* -------------------------------------------- */
static articleDetermine(genre) {
2021-02-28 01:38:40 +01:00
switch (Grammar.toLowerCaseNoAccent(genre)) {
case 'f': case 'feminin': return 'la';
case 'p': case 'mp': case 'fp': case 'pluriel': return 'les';
default:
case 'm': case 'masculin': return 'le';
}
}
/* -------------------------------------------- */
2021-02-28 01:38:40 +01:00
static articleIndetermine(genre) {
switch (Grammar.toLowerCaseNoAccent(genre)) {
case 'f': case 'feminin': return 'une';
case 'p': case 'fp': case 'mp': case 'pluriel': return 'des';
case 'n': case 'neutre': return 'du'
default:
case 'm': case 'masculin': return 'un';
}
2021-01-26 19:47:18 +01:00
}
/* -------------------------------------------- */
/**
* 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) {
2021-02-28 01:38:40 +01:00
switch (Grammar.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];
}
}
2020-12-08 03:04:00 +01:00
}