From 17bc326783a7c039042441bd3510c21c888638bb Mon Sep 17 00:00:00 2001 From: flothoni Date: Fri, 15 Jun 2018 16:41:34 +0200 Subject: [PATCH 1/7] compare.js: delete useless legacy code These function weren't not used anymore. link to #3288 --- browser/js/compare.js | 184 ------------------------------------------ 1 file changed, 184 deletions(-) diff --git a/browser/js/compare.js b/browser/js/compare.js index d5c86dba2..7ce664325 100644 --- a/browser/js/compare.js +++ b/browser/js/compare.js @@ -49,187 +49,3 @@ function compare(string1, string2) { if( comparaison_2){ return -1} return 0; } - -/* Fonction permettant la comparaison entre 2 caractères ascii - * @param char1: Un premier caractère - * @param char2: Un second caractère - * Renvoie -1 si le 1er caractère se positionne avant le second dans la table ASCII - * Renvoie 0 si les 2 caractères sont égaux (non-supérieur/inférieur à...) - * Renvoie 1 si le 1er caractère se positionne après le second dans la table ASCII - */ -function compare_ascii(char1, char2) { - var charInt1 = char1.charCodeAt(0); - var charInt2 = char2.charCodeAt(0); - //Vérification si le caractère est bien "normal" -> lettre - if (((charInt1 > 64 && charInt1 < 91) || (charInt1 > 96 && charInt1 < 123)) && ((charInt2 > 64 && charInt2 < 91) || (charInt2 > 96 && charInt2 < 123))) { - //Si le caractère ascii a un charCode inférieur au deuxième, on renvoie -1 - if (char1.charCodeAt(0) < char2.charCodeAt(0)) - return -1; - //Si le caractère ascii a un charCode supérieur au deuxième, on renvoie 1 - if (char1.charCodeAt(0) > char2.charCodeAt(0)) - return 1; - //Sinon, on renvoie 0 -> Cas d'égalité - return 0; - } - //Si le caractère n'est pas "normal" - else { - if ((charInt1 <= 64 || charInt1 >= 91) && (charInt1 < 96 || charInt1 >= 123) && (charInt2 <= 64 || charInt2 >= 91) && (charInt2 < 96 || charInt2 >= 123)) - return 0; - //Vérification pour le 1er paramètre - if ((charInt1 <= 64 || charInt1 >= 91) && (charInt1 < 96 || charInt1 >= 123)) - return 1; - //Si résultat négatif au-dessus, c'est le second paramètre qui ne vas pas - on choisira donc le 1er -> Renvoie de -1 - else - return -1; - } -} - -/* Fonction permettant la comparaison de deux chaînes en fonction de leurs tailles ainsi que d'un incrément (i), donné dans la fonction de comparaison générale (compare) - * @param string1: Une première chaîne de caractères - * @param string2: Une seconde chaîne de caractères - * Renvoie -1 si la première chaîne est vide - * Renvoie 0 si les deux chaînes sont vides - * Renvoie 1 si la seconde chaîne est vide - */ -function compare_length(string1, string2, i) { - //Si les deux chaînes sont équivalentes en longueur, on renvoie 0 - if (i == string1.length && string1.length == string2.length) - return 0; - //Si le i ne correspond seulement qu'à la longueur de la première chaîne, on renvoie -1 - if ((i == string1.length && string2.length !== 0) || string1.length === 0) - return -1; - //Si le i ne correspond seulement qu'à la longueur de la deuxième chaîne, on renvoie 1 - if ((i == string2.length && string1.length !== 0) || string2.length === 0) - return 1; -} - -/* Fonction permettant la comparaison de deux chaînes de caractères (sensés être une suite de chiffres) - * @param nbr1: Un premier 'grand' nombre - * @param nbr2: Un second 'grand' nombre - * Renvoie -1 si, à un caractère donné, le nombre de la 1ère chaîne est inférieur à celui de la 2ème chaîne à la même place, et que la longueur de la première chaîne est inférieure ou égale à la deuxième OU si la deuxième n'est pas considérée comme un nombre - * Renvoie 0 si les deux chaînes sont équivalentes OU si les deux chaînes ne sont pas considérées comme deux nombres - * Renvoie 1 si, à un caractère donné, le nombre de la 2ème chaîne est supérieur à celui de la 2ème chaîne à la même place, et que la longueur de la seconde chaîne est inférieure ou égale à la deuxième OU si la première chaîne n'est pas considérée comme un nombre - */ -function compare_numbers(nbr1, nbr2) { - //Si les deux paramètres sont des nombres ("123", "45") - var i; - if (!isNaN(nbr1) && !isNaN(nbr2)) { - var nbr1Int; - var nbr2Int; - //Pour chaque nombre, on va vouloir les différencier le plus rapidement possible, par une lecture de chaque caractère numérique de gauche à droite - for (i=0; i nbr2Int) && (nbr1.length < nbr2.length)) - return -1; - //Cas pour "2" et "1", ou "60" et "5" - if ((nbr1Int > nbr2Int) && (nbr1.length >= nbr2.length)) - return 1; - //Cas pour "33" et "3" - if ((nbr1Int == nbr2Int) && (nbr1.length > nbr2.length)) - return 1; - //Cas pour "10" et "2" - if ((nbr1Int < nbr2Int) && (nbr1.length >= nbr2.length)) - return 1; - } - //Cas d'égalité - if (nbr1.length == nbr2.length) - return 0; - } - //Si les deux ne sont pas des nombres ("123", "456A" ; "123A", "456" ; "123A", "456B") - else { - i = 0; - var boolnbr1 = false; - var subnbr1 = nbr1; - var j = 0; - var boolnbr2 = false; - var subnbr2 = nbr2; - if (isNaN(nbr1[0]) && !isNaN(nbr2[0]) && nbr1.length >= nbr2.length) - return 1; - if (!isNaN(nbr1[0]) && isNaN(nbr2[0]) && nbr1.length <= nbr2.length) - return -1; - //Si nbr1 n'est pas un nombre, alors on avance dans la chaîne jusqu'à temps que nous rattrapons un caaractère QUI N'EST PAS un nombre - if (isNaN(nbr1)) { - while (i < nbr1.length && !boolnbr1) { - if (isNaN(nbr1.charAt(i))) boolnbr1 = true; - i++; - } - //Substring pour nbr1 - subnbr1 = nbr1.substring(0, i-1); - } - //Même chose pour nbr2 - if (isNaN(nbr2)) { - while (j < nbr2.length && !boolnbr2) { - if (isNaN(nbr2.charAt(j))) boolnbr2 = true; - j++; - } - subnbr2 = nbr2.substring(0, j-1); - } - //Si les chaînes ne sont pas vides, alors on va comparer ces deux sous-nombres entre eux - if (subnbr1.length !== 0 && subnbr2.length !== 0) { - var result = compare_numbers(subnbr1, subnbr2); - //S'il y a cas d'égalité - if (result === 0) - //On refait une comparaison afin de comparer les chaînes par ce qui suivait le nombre contenu dans la sous-chaîne comparée de chacune - return compare(nbr1.substring(i-1, nbr1.length), nbr2.substring(i-1, nbr2.length)); - //Sinon, on retourne directement result, qui vaut -1 ou 1 selon le retour de la fonction compare_numbers sur les sous-chaînes - else - return result; - } - } -} - - -//transforme une chaine de caractere en tableau -// hi80-540 >>>> ["h", "i", 80, "-", 540] -function parseString(str){ - var result = [] - - var j = 0 - for (var i=0; i Date: Fri, 15 Jun 2018 17:19:47 +0200 Subject: [PATCH 2/7] compare.js: update funtion mySortedArray and add tests Make minor modification of function mySortedArray Also add tests on function mySortedArray link to #3288 --- browser/js/compare.js | 2 +- browser/test/QUnit/testFiles/compare_test.js | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/browser/js/compare.js b/browser/js/compare.js index 7ce664325..678684f1e 100644 --- a/browser/js/compare.js +++ b/browser/js/compare.js @@ -27,7 +27,7 @@ * Ne renvoie rien - l'ancien tableau est écrasé par le tri que l'on vient de faire */ function mySortedArray(tab) { - tab.sort(function(a,b){return compare(a,b);}); + tab.sort(function(a,b){return compare(a.toString(),b.toString());}); //tab.sort(function(a,b){return compareString(a,b);}); } diff --git a/browser/test/QUnit/testFiles/compare_test.js b/browser/test/QUnit/testFiles/compare_test.js index 577d98b5d..15ec382fe 100644 --- a/browser/test/QUnit/testFiles/compare_test.js +++ b/browser/test/QUnit/testFiles/compare_test.js @@ -4,6 +4,26 @@ QUnit.module("Compare", { }); +QUnit.test("mySortedArray", function(assert) { + + var array_1 = [0, 15, 35, 89, 42] + var array_2 = ["string_2", "string_4", "string_1"] + var array_3 = ["str1", 42, "str2"] + var getted_array_1 = [0, 15, 35, 42, 89] + var getted_array_2 = ["string_1", "string_2", "string_4"] + var getted_array_3 = [42, "str1", "str2"] + + mySortedArray(array_1) + mySortedArray(array_2) + mySortedArray(array_3) + console.log( array_3, getted_array_3) + assert.deepEqual(getted_array_1, array_1, "Array 1 (olny number) have been correctly sorted") + assert.deepEqual(getted_array_2, array_2, "Array 2 (only string) have been correctly sorted") + assert.deepEqual(getted_array_3, array_3, "Array 3 (mix string+number)have been correctly sorted") + +}); + + QUnit.test("compare", function(assert) { // avec des valeurs identiques -- GitLab From 58c7f9b8518b5d77499847eede80c7e7d3e47cb4 Mon Sep 17 00:00:00 2001 From: flothoni Date: Thu, 30 Aug 2018 10:26:14 +0200 Subject: [PATCH 3/7] compare.js: modification of the function compare et mySortedArray This function allow now to compare sting, number, or mix of both link to #3288 --- browser/js/compare.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/browser/js/compare.js b/browser/js/compare.js index 678684f1e..c6243cf1a 100644 --- a/browser/js/compare.js +++ b/browser/js/compare.js @@ -27,7 +27,7 @@ * Ne renvoie rien - l'ancien tableau est écrasé par le tri que l'on vient de faire */ function mySortedArray(tab) { - tab.sort(function(a,b){return compare(a.toString(),b.toString());}); + tab.sort(function(a,b){return compare(a,b);}); //tab.sort(function(a,b){return compareString(a,b);}); } @@ -42,6 +42,10 @@ var term = "*"; * Renvoie 1 si la deuxième chaîne doit être classée avant la première */ function compare(string1, string2) { + if (typeof(string1) != typeof(string2)){ + string1 = string1.toString() + string2 = string2.toString() + } var comparaison_1 = string1 > string2 var comparaison_2 = string1 < string2 -- GitLab From 0e3b1d5d2150aaaaf1047b5b85d4e85b1e06667a Mon Sep 17 00:00:00 2001 From: flothoni Date: Thu, 30 Aug 2018 10:28:10 +0200 Subject: [PATCH 4/7] update test of compare function New tests with new functionnality of the previous modifications of the function Warning: add tests to show limit of the fucntion when attented to sort number send as string link to #3288 --- browser/test/QUnit/testFiles/compare_test.js | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/browser/test/QUnit/testFiles/compare_test.js b/browser/test/QUnit/testFiles/compare_test.js index 15ec382fe..0e74fbcca 100644 --- a/browser/test/QUnit/testFiles/compare_test.js +++ b/browser/test/QUnit/testFiles/compare_test.js @@ -52,18 +52,13 @@ QUnit.test("compare", function(assert) { - getted = compare("42", "512") + getted = compare(42, 312) assert.equal(getted, -1, "number return correct order") - getted = compare("512", "42") - assert.equal(getted, 1, "reverse of the previous") - getted = compare("042", "512") - assert.equal(getted, -1, "number with 0 at first place return correct order") - getted = compare("512", "042") - assert.equal(getted, 1, "reverse of the previous") - - getted = compare("intron-42*42", "intron-512*42") - assert.equal(getted, -1, "straing, number + allele return correct order") - getted = compare("intron-512*42", "intron-42*42") - assert.equal(getted, 1, "reverse of the previous") + // !!! Number should be send as it, if not, it will be sort as string !!! + // see examples below + getted = compare("42", "312") + assert.equal(getted, 1, "number return incorrect order if sorted as string (42 after 312)") + getted = compare("intron-42*42", "intron-312*42") + assert.equal(getted, 1, "string, number + allele return incorrect order") }); -- GitLab From 8713667693b1fc1953008c127cbf966f3ebb3135 Mon Sep 17 00:00:00 2001 From: flothoni Date: Thu, 30 Aug 2018 10:29:20 +0200 Subject: [PATCH 5/7] test_compare.js: update test on function mySortedArray link to #3288 --- browser/test/QUnit/testFiles/compare_test.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/browser/test/QUnit/testFiles/compare_test.js b/browser/test/QUnit/testFiles/compare_test.js index 0e74fbcca..50d7a01f4 100644 --- a/browser/test/QUnit/testFiles/compare_test.js +++ b/browser/test/QUnit/testFiles/compare_test.js @@ -8,18 +8,22 @@ QUnit.test("mySortedArray", function(assert) { var array_1 = [0, 15, 35, 89, 42] var array_2 = ["string_2", "string_4", "string_1"] - var array_3 = ["str1", 42, "str2"] + var array_3 = ["str1", 42, "str2", 5] + var array_4 = [0, 15, 5, 35, 89, 101, 42] var getted_array_1 = [0, 15, 35, 42, 89] var getted_array_2 = ["string_1", "string_2", "string_4"] - var getted_array_3 = [42, "str1", "str2"] + var getted_array_3 = [5, 42, "str1", "str2"] + var getted_array_4 = [0, 5, 15, 35, 42, 89, 101] mySortedArray(array_1) mySortedArray(array_2) mySortedArray(array_3) - console.log( array_3, getted_array_3) - assert.deepEqual(getted_array_1, array_1, "Array 1 (olny number) have been correctly sorted") - assert.deepEqual(getted_array_2, array_2, "Array 2 (only string) have been correctly sorted") - assert.deepEqual(getted_array_3, array_3, "Array 3 (mix string+number)have been correctly sorted") + mySortedArray(array_4) + console.log( array_4, getted_array_4) + assert.deepEqual(array_1, getted_array_1, "Array 1 (olny number) have been correctly sorted") + assert.deepEqual(array_2, getted_array_2, "Array 2 (only string) have been correctly sorted") + assert.deepEqual(array_3, getted_array_3, "Array 3 (mix string+number)have been correctly sorted") + assert.deepEqual(array_4, getted_array_4, "Array 4 (number only, but with 101)have been correctly sorted") }); -- GitLab From f6392ebfdcfc144bad4b2003d33f546e27bbde48 Mon Sep 17 00:00:00 2001 From: flothoni Date: Thu, 30 Aug 2018 10:51:28 +0200 Subject: [PATCH 6/7] compare.js: update function compare to sort even is value are number formated as string link to #3288 --- browser/js/compare.js | 8 ++++++++ browser/test/QUnit/testFiles/compare_test.js | 12 ++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/browser/js/compare.js b/browser/js/compare.js index c6243cf1a..fd1144859 100644 --- a/browser/js/compare.js +++ b/browser/js/compare.js @@ -42,10 +42,18 @@ var term = "*"; * Renvoie 1 si la deuxième chaîne doit être classée avant la première */ function compare(string1, string2) { + if (typeof(string1) != typeof(string2)){ string1 = string1.toString() string2 = string2.toString() } + if (typeof(string1) == "string" && typeof(string2) == "string"){ + if (!isNaN( Number(string1) ) && !isNaN( Number(string2) ) ) { + string1 = Number(string1) + string2 = Number(string2) + } + } + var comparaison_1 = string1 > string2 var comparaison_2 = string1 < string2 diff --git a/browser/test/QUnit/testFiles/compare_test.js b/browser/test/QUnit/testFiles/compare_test.js index 50d7a01f4..50731d519 100644 --- a/browser/test/QUnit/testFiles/compare_test.js +++ b/browser/test/QUnit/testFiles/compare_test.js @@ -10,20 +10,24 @@ QUnit.test("mySortedArray", function(assert) { var array_2 = ["string_2", "string_4", "string_1"] var array_3 = ["str1", 42, "str2", 5] var array_4 = [0, 15, 5, 35, 89, 101, 42] + var array_5 = ["0", "15", "5"] var getted_array_1 = [0, 15, 35, 42, 89] var getted_array_2 = ["string_1", "string_2", "string_4"] var getted_array_3 = [5, 42, "str1", "str2"] var getted_array_4 = [0, 5, 15, 35, 42, 89, 101] + var getted_array_5 = ["0", "5", "15"] mySortedArray(array_1) mySortedArray(array_2) mySortedArray(array_3) mySortedArray(array_4) + mySortedArray(array_5) console.log( array_4, getted_array_4) assert.deepEqual(array_1, getted_array_1, "Array 1 (olny number) have been correctly sorted") assert.deepEqual(array_2, getted_array_2, "Array 2 (only string) have been correctly sorted") assert.deepEqual(array_3, getted_array_3, "Array 3 (mix string+number)have been correctly sorted") - assert.deepEqual(array_4, getted_array_4, "Array 4 (number only, but with 101)have been correctly sorted") + assert.deepEqual(array_4, getted_array_4, "Array 4 (number only, but with 101) have been correctly sorted") + assert.deepEqual(array_5, getted_array_5, "Array 4 (number only, formated as string) have been correctly sorted") }); @@ -58,10 +62,10 @@ QUnit.test("compare", function(assert) { getted = compare(42, 312) assert.equal(getted, -1, "number return correct order") - // !!! Number should be send as it, if not, it will be sort as string !!! - // see examples below getted = compare("42", "312") - assert.equal(getted, 1, "number return incorrect order if sorted as string (42 after 312)") + assert.equal(getted, -1, "number return incorrect order if sorted as string (42 after 312)") + // !!! You cannot send value as a mix betwenn string and number. It will be sort as string !!! + // see example below getted = compare("intron-42*42", "intron-312*42") assert.equal(getted, 1, "string, number + allele return incorrect order") -- GitLab From e2a3194109065845cde32396f37d0a45dd149182 Mon Sep 17 00:00:00 2001 From: flothoni Date: Wed, 5 Sep 2018 17:32:28 +0200 Subject: [PATCH 7/7] compare.js: return to previous behaviour; link to #3288 --- browser/js/compare.js | 16 +++-------- browser/test/QUnit/testFiles/compare_test.js | 30 +++++++++----------- 2 files changed, 17 insertions(+), 29 deletions(-) diff --git a/browser/js/compare.js b/browser/js/compare.js index fd1144859..37654c150 100644 --- a/browser/js/compare.js +++ b/browser/js/compare.js @@ -27,7 +27,7 @@ * Ne renvoie rien - l'ancien tableau est écrasé par le tri que l'on vient de faire */ function mySortedArray(tab) { - tab.sort(function(a,b){return compare(a,b);}); + tab.sort(function(a,b){return compare(a.toString(),b.toString());}); //tab.sort(function(a,b){return compareString(a,b);}); } @@ -42,17 +42,9 @@ var term = "*"; * Renvoie 1 si la deuxième chaîne doit être classée avant la première */ function compare(string1, string2) { - - if (typeof(string1) != typeof(string2)){ - string1 = string1.toString() - string2 = string2.toString() - } - if (typeof(string1) == "string" && typeof(string2) == "string"){ - if (!isNaN( Number(string1) ) && !isNaN( Number(string2) ) ) { - string1 = Number(string1) - string2 = Number(string2) - } - } + // verification du formatage + string1 = string1.toString() + string2 = string2.toString() var comparaison_1 = string1 > string2 var comparaison_2 = string1 < string2 diff --git a/browser/test/QUnit/testFiles/compare_test.js b/browser/test/QUnit/testFiles/compare_test.js index 50731d519..4464b30cf 100644 --- a/browser/test/QUnit/testFiles/compare_test.js +++ b/browser/test/QUnit/testFiles/compare_test.js @@ -6,28 +6,23 @@ QUnit.module("Compare", { QUnit.test("mySortedArray", function(assert) { - var array_1 = [0, 15, 35, 89, 42] - var array_2 = ["string_2", "string_4", "string_1"] - var array_3 = ["str1", 42, "str2", 5] + var array_1 = [0, 15, 35, 89, 42, 5] + var array_2 = ["string_2", "string_4", "string_1", "string_11"] + var array_3 = ["str1", 42, "str2", "str111", 5] var array_4 = [0, 15, 5, 35, 89, 101, 42] - var array_5 = ["0", "15", "5"] - var getted_array_1 = [0, 15, 35, 42, 89] - var getted_array_2 = ["string_1", "string_2", "string_4"] - var getted_array_3 = [5, 42, "str1", "str2"] - var getted_array_4 = [0, 5, 15, 35, 42, 89, 101] - var getted_array_5 = ["0", "5", "15"] + var getted_array_1 = [0, 15, 35, 42, 5, 89] + var getted_array_2 = ["string_1", "string_11", "string_2", "string_4"] + var getted_array_3 = [42, 5, "str1", "str111", "str2"] + var getted_array_4 = [0, 101, 15, 35, 42, 5, 89] mySortedArray(array_1) mySortedArray(array_2) mySortedArray(array_3) mySortedArray(array_4) - mySortedArray(array_5) - console.log( array_4, getted_array_4) - assert.deepEqual(array_1, getted_array_1, "Array 1 (olny number) have been correctly sorted") + assert.deepEqual(array_1, getted_array_1, "Array 1 (olny number) as been sorted as string (5 after 42)") assert.deepEqual(array_2, getted_array_2, "Array 2 (only string) have been correctly sorted") assert.deepEqual(array_3, getted_array_3, "Array 3 (mix string+number)have been correctly sorted") assert.deepEqual(array_4, getted_array_4, "Array 4 (number only, but with 101) have been correctly sorted") - assert.deepEqual(array_5, getted_array_5, "Array 4 (number only, formated as string) have been correctly sorted") }); @@ -59,12 +54,13 @@ QUnit.test("compare", function(assert) { assert.equal(getted, -1, "reverse of the previous") - + // !!! As number are converted to string, the sort can be erronous + // See example below: getted = compare(42, 312) - assert.equal(getted, -1, "number return correct order") - getted = compare("42", "312") + assert.equal(getted, 1, "number return correct order") + getted = compare("312", "42") assert.equal(getted, -1, "number return incorrect order if sorted as string (42 after 312)") - // !!! You cannot send value as a mix betwenn string and number. It will be sort as string !!! + // !!! You can send value as a mix betwenn string and number, but it will be sort as string !!! // see example below getted = compare("intron-42*42", "intron-312*42") assert.equal(getted, 1, "string, number + allele return incorrect order") -- GitLab