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

84 lines
2.4 KiB
JavaScript
Raw 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
2022-11-04 20:41:16 +01:00
static equalsInsensitive(a, b) {
return Grammar.toLowerCaseNoAccent(a) == Grammar.toLowerCaseNoAccent(b)
}
2022-06-12 08:17:59 +02:00
static includesLowerCaseNoAccent(value, content) {
2024-11-25 22:16:52 +01:00
return Grammar.toLowerCaseNoAccent(value)?.includes(Grammar.toLowerCaseNoAccent(content));
2022-06-12 08:17:59 +02:00
}
2022-11-04 20:41:16 +01:00
/* -------------------------------------------- */
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
}