|
1 |
| - |
2 |
| -// Liste de tout les caractères qu'on utilisera pour générer le code, ainsi que les caractères qui les entourent |
3 |
| -const alphabet = [ |
4 |
| - { char: 'a', surrounding: ['z', 'q'] }, { char: 'b', surrounding: ['v', 'n'] }, { char: 'c', surrounding: ['v', 'd'] }, |
5 |
| - { char: 'd', surrounding: ['s', 'e', 'f', 'c'] }, { char: 'e', surrounding: ['z', 'r'] }, { char: 'f', surrounding: ['d', 'r', 'g'] }, |
6 |
| - { char: 'g', surrounding: ['f', 'h'] }, { char: 'h', surrounding: ['g', 'y'] }, { char: 'i', surrounding: ['u', 'o'] }, |
7 |
| - { char: 'k', surrounding: ['l', 'i'] }, { char: 'l', surrounding: ['k', 'o'] }, { char: 'n', surrounding: ['b', 'h'] }, |
8 |
| - { char: 'o', surrounding: ['i', 'l'] }, { char: 'q', surrounding: ['a', 's'] }, { char: 'r', surrounding: ['e', 't'] }, |
9 |
| - { char: 's', surrounding: ['q', 'd'] }, { char: 't', surrounding: ['r', 'y'] }, { char: 'u', surrounding: ['y', 'i'] }, |
10 |
| - { char: 'v', surrounding: ['c', 'b'] }, { char: 'y', surrounding: ['t', 'u'] }, |
11 |
| - { char: 'z', surrounding: ['a', 'e'] } // IMPORTANT: toujours garder au moins 2 éléments uniques dans surrounding |
12 |
| - /*, { char: '1', surrounding: ['2'] }, { char: '2', surrounding: ['1','3'] }, |
13 |
| - { char: '3', surrounding: ['2','4'] }, { char: '4', surrounding: ['3','5'] }, { char: '5', surrounding: ['4','6'] }, |
14 |
| - { char: '6', surrounding: ['5','7'] }, { char: '7', surrounding: ['6','8'] }, { char: '8', surrounding: ['7','9'] }, |
15 |
| - { char: '9', surrounding: ['8','0'] }, { char: '0', surrounding: ['9'] },*/ // j'ai tenté de rendre le code plus propre ptdrr |
| 1 | +const charsGroups = [ |
| 2 | + 'df', |
| 3 | + 'az', |
| 4 | + 'iu', |
| 5 | + 'li', |
| 6 | + 'rt', |
| 7 | + 're', |
| 8 | + 'er', |
| 9 | + 'nn', |
| 10 | + 'po', |
| 11 | + 'oi', |
| 12 | + 'tu', |
| 13 | + 'tr', |
| 14 | + 'es', |
| 15 | + 'ed', |
| 16 | + 'as', |
| 17 | + 'de', |
| 18 | + 'mo', |
| 19 | + 'se', |
| 20 | + 'op', |
| 21 | + 'lo', |
| 22 | + 'zq', |
| 23 | + 'tg', |
| 24 | + 'cv', |
| 25 | + 'pl', |
| 26 | + 'sc', |
| 27 | + 'om', |
| 28 | + 'sdf', |
| 29 | + 'oui', |
| 30 | + 'jkl', |
| 31 | + 'aze', |
| 32 | + 'fer', |
| 33 | + 'ser', |
16 | 34 | ]
|
17 | 35 |
|
| 36 | +function chooseRandomCharGroup(lastChar){ |
| 37 | + var random = charsGroups[Math.floor(Math.random() * charsGroups.length)] |
| 38 | + if(lastChar && (random.endsWith(lastChar) || random.startsWith(lastChar))) return chooseRandomCharGroup(lastChar) |
| 39 | + return random |
| 40 | +} |
| 41 | + |
18 | 42 | // Fonction qui génère un code aléatoire
|
19 |
| -function generateCode(length){ |
20 |
| - // Générer tout les caractères |
| 43 | +function generateCode(length = 8){ |
21 | 44 | var code = ''
|
22 |
| - for(var i = 0; i < length; i++){ |
23 |
| - // Si on a pas de caractère, on en génère un aléatoire |
24 |
| - if(code.length < 1){ |
25 |
| - code += alphabet[Math.floor(Math.random() * alphabet.length)].char |
26 |
| - continue |
27 |
| - } else { |
28 |
| - // Sinon, on prend le précédent caractère et on lui ajoute un caractère assez proche |
29 |
| - var lastChar = code[code.length - 1] |
30 |
| - var lastCharIndex = alphabet.findIndex(char => char.char === lastChar) |
31 |
| - var surrounding = alphabet[lastCharIndex].surrounding |
32 |
| - var char = surrounding[Math.floor(Math.random() * surrounding.length)] |
33 | 45 |
|
34 |
| - // On évite que le caractère soit le même que le précédent |
35 |
| - while(char === lastChar) char = surrounding[Math.floor(Math.random() * surrounding.length)] // si le caractère est le même, on en génère un autre |
36 |
| - |
37 |
| - // On évite à moitié que le caractère soit le même que l'avant dernier |
38 |
| - if(code.length > 1){ |
39 |
| - var beforeLastChar = code[code.length - 2] |
40 |
| - if(char === beforeLastChar) char = surrounding[Math.floor(Math.random() * surrounding.length)] |
41 |
| - } |
42 |
| - |
43 |
| - // On ajoute le caractère au code |
44 |
| - code += char |
45 |
| - } |
| 46 | + // Répéter jusque la longueur voulu soit atteinte |
| 47 | + while (code.length < length){ |
| 48 | + var random = chooseRandomCharGroup(code[code.length - 1]) |
| 49 | + if(code.length > 1 && code.endsWith(random)) continue |
| 50 | + code += random |
46 | 51 | }
|
47 | 52 |
|
48 |
| - // On veut pas beaucoup de chiffres dans les codes, donc si on en a plus de deux, on les remplace par des lettres |
49 |
| - var numbers = code.match(/[0-9]/g) |
50 |
| - var alphabetWithoutNumbers = alphabet.filter(char => !char.char.match(/[0-9]/g)) |
51 |
| - if(numbers && numbers.length > (length - 4)){ |
52 |
| - numbers.forEach(number => { |
53 |
| - var index = code.indexOf(number) |
54 |
| - code = code.slice(0, index) + alphabetWithoutNumbers[Math.floor(Math.random() * alphabetWithoutNumbers.length)].char + code.slice(index + 1) |
55 |
| - }) |
56 |
| - } |
| 53 | + // Cut le code s'il est trop long |
| 54 | + code = code.slice(0, length) |
57 | 55 |
|
58 | 56 | // On retourne le code
|
59 | 57 | return code
|
|
0 commit comments