Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Résolution d'un bug sur le calcul des quadrigrammes. #36

Merged
merged 1 commit into from
Sep 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 32 additions & 10 deletions src/js/class/Event.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,42 @@ export class Events {
* Takes a name and returns its tetragraph.
* "Pierre Martin" will give "PMAR"
* "Jean-Paul Dupont" will give "JPDU"
* If the last name is short, we try to use elements from the previous names :
* "Pierre O" => "PIEO", "Pierre O-LY" => "POLY", "Pierre-Jean O" => "PJEO", "Pierre Martin-O" => "PMAO"
* @param {string} name of the user, separated by spaces or '-' for composed names.
*/
static compute_tetragraph(name) {
const words = name.replace("-", " ").split(" "); // split by space or -
let tetragraph = ""
for (let i = 0; i < words.length; i++) {
if (i >= words.length - 1) {
// takes all first letters from the last word to finish the tetragraph
let lettersMissing = Math.max(0, 4 - tetragraph.length)
tetragraph += words[i].substring(0, lettersMissing).toUpperCase();
} else {
tetragraph += words[i][0].toUpperCase();
}
const words = name.replace("-", " ").replace(/\s+/, " ").trim().split(" "); // split by space or -, removing duplicate blanks
let tetragraph = "";
let n = Math.min(3, words.length - 1);
// take first letters of the first words, up to the before-last one, or 3 (consider the last one as the family name)
for (let i = 0; i < n; i++) {
tetragraph += words[i][0];
}

// Add the last first letter (family name)
tetragraph += words[words.length-1][0]

// Now, fix the tetragraph to have 4 letters, using the first letters of the last words
let lettersMissingCount = Math.max(0, 4 - tetragraph.length);
let suffix = "";
let j = words.length - 1;
// Handle the case where the last word is too short (e.g we need 3 letters, but the name is LY)
// In this case, we just add letters from the previous word (e.g. Mary Ly --> MALY)
while (lettersMissingCount > 0 && j >= 0) {
// Remove the last letter from the tetragraph, it will be added back by the suffix at the right place
tetragraph = tetragraph.slice(0, -1)

// substring treats everything > length as if it were length, so it's fine to do that
let toAdd = words[j].substring(0, lettersMissingCount + 1);
lettersMissingCount -= toAdd.length - 1; // -1 since we remove a letter from the tetragraph
suffix = toAdd + suffix;
j--;
}

tetragraph += suffix;
tetragraph = tetragraph.toUpperCase();

return tetragraph;
}
}