Skip to content

Commit e7573bd

Browse files
committed
Nvl algorithme pour générer des code
+ rapide (90ms --> 40ms sur 100k gens) + propre et rapide à écrire sur un clavier AZERTY
1 parent c650109 commit e7573bd

File tree

3 files changed

+109
-50
lines changed

3 files changed

+109
-50
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"private": true,
33
"name": "stend-api",
4-
"version": "2.2.0",
4+
"version": "2.2.1",
55
"description": "Télécharger et envoyer des fichiers via Stend",
66
"main": "index.js",
77
"scripts": {

utils/generateCode.js

Lines changed: 47 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,57 @@
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',
1634
]
1735

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+
1842
// 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){
2144
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)]
3345

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
4651
}
4752

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)
5755

5856
// On retourne le code
5957
return code

utils/generateCode.old.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Liste de tout les caractères qu'on utilisera pour générer le code, ainsi que les caractères qui les entourent
2+
const alphabet = [
3+
{ char: 'a', surrounding: ['z', 'q'] }, { char: 'b', surrounding: ['v', 'n'] }, { char: 'c', surrounding: ['v', 'd'] },
4+
{ char: 'd', surrounding: ['s', 'e', 'f', 'c'] }, { char: 'e', surrounding: ['z', 'r'] }, { char: 'f', surrounding: ['d', 'r', 'g'] },
5+
{ char: 'g', surrounding: ['f', 'h'] }, { char: 'h', surrounding: ['g', 'y'] }, { char: 'i', surrounding: ['u', 'o'] },
6+
{ char: 'k', surrounding: ['l', 'i'] }, { char: 'l', surrounding: ['k', 'o'] }, { char: 'n', surrounding: ['b', 'h'] },
7+
{ char: 'o', surrounding: ['i', 'l'] }, { char: 'q', surrounding: ['a', 's'] }, { char: 'r', surrounding: ['e', 't'] },
8+
{ char: 's', surrounding: ['q', 'd'] }, { char: 't', surrounding: ['r', 'y'] }, { char: 'u', surrounding: ['y', 'i'] },
9+
{ char: 'v', surrounding: ['c', 'b'] }, { char: 'y', surrounding: ['t', 'u'] },
10+
{ char: 'z', surrounding: ['a', 'e'] } // IMPORTANT: toujours garder au moins 2 éléments uniques dans surrounding
11+
/*, { char: '1', surrounding: ['2'] }, { char: '2', surrounding: ['1','3'] },
12+
{ char: '3', surrounding: ['2','4'] }, { char: '4', surrounding: ['3','5'] }, { char: '5', surrounding: ['4','6'] },
13+
{ char: '6', surrounding: ['5','7'] }, { char: '7', surrounding: ['6','8'] }, { char: '8', surrounding: ['7','9'] },
14+
{ char: '9', surrounding: ['8','0'] }, { char: '0', surrounding: ['9'] },*/ // j'ai tenté de rendre le code plus propre ptdrr
15+
]
16+
17+
// Fonction qui génère un code aléatoire
18+
function generateCode(length){
19+
// Générer tout les caractères
20+
var code = ''
21+
for(var i = 0; i < length; i++){
22+
// Si on a pas de caractère, on en génère un aléatoire
23+
if(code.length < 1){
24+
code += alphabet[Math.floor(Math.random() * alphabet.length)].char
25+
continue
26+
} else {
27+
// Sinon, on prend le précédent caractère et on lui ajoute un caractère assez proche
28+
var lastChar = code[code.length - 1]
29+
var lastCharIndex = alphabet.findIndex(char => char.char === lastChar)
30+
var surrounding = alphabet[lastCharIndex].surrounding
31+
var char = surrounding[Math.floor(Math.random() * surrounding.length)]
32+
33+
// On évite que le caractère soit le même que le précédent
34+
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
35+
36+
// On évite à moitié que le caractère soit le même que l'avant dernier
37+
if(code.length > 1){
38+
var beforeLastChar = code[code.length - 2]
39+
if(char === beforeLastChar) char = surrounding[Math.floor(Math.random() * surrounding.length)]
40+
}
41+
42+
// On ajoute le caractère au code
43+
code += char
44+
}
45+
}
46+
47+
// On veut pas beaucoup de chiffres dans les codes, donc si on en a plus de deux, on les remplace par des lettres
48+
var numbers = code.match(/[0-9]/g)
49+
var alphabetWithoutNumbers = alphabet.filter(char => !char.char.match(/[0-9]/g))
50+
if(numbers && numbers.length > (length - 4)){
51+
numbers.forEach(number => {
52+
var index = code.indexOf(number)
53+
code = code.slice(0, index) + alphabetWithoutNumbers[Math.floor(Math.random() * alphabetWithoutNumbers.length)].char + code.slice(index + 1)
54+
})
55+
}
56+
57+
// On retourne le code
58+
return code
59+
}
60+
61+
module.exports = generateCode

0 commit comments

Comments
 (0)