Naeco
Naeco

Reputation: 133

How to make a switch case with UTF8 string in JavaScript?

I have this function who work all the time but just for the "µ" possibility, it does not work:

function obtenirFacteur(multiple){
    var facteur = 1;
    switch(multiple){
        case "T" :
            facteur = 1000000000000;
            break;
        case "G" :
            facteur = 1000000000;
        case "M" :
            facteur = 1000000;
            break;
        case "K" :
            facteur = 1000;
            break;
        case "h" :
            facteur = 100;
            break;
        case "da" :
            facteur = 10;
            break;
        case "d" :
            facteur = 0.1;
            break;
        case "c" :
            facteur = 0.01;
            break;
        case "m" :
            facteur = 0.001;
            break;
        case "μ":
            facteur = 0.000001;
            break;
        case "n" :
            facteur = 0.000000001;
            break;
        case "p" :
            facteur = 0.000000000001;
            break;
        case "f" :
            facteur = 0.000000000000001;
            break;
    }
    return facteur;
}

But it does not work for the string "µ".

My file is in UTF8 without BOM in notepad++, what usually work for all the situation.

Upvotes: 1

Views: 465

Answers (5)

Naeco
Naeco

Reputation: 133

My solution is really not the best, but i should go ahead, so :

function obtenirFacteur(multiple){
    var facteur = 1;
    if(multiple)="µ"){
      multiple = mu;
    }
    switch(multiple){
        case "T" :
            facteur = 1000000000000;
            break;
        case "G" :
            facteur = 1000000000;
        case "M" :
            facteur = 1000000;
            break;
        case "K" :
            facteur = 1000;
            break;
        case "h" :
            facteur = 100;
            break;
        case "da" :
            facteur = 10;
            break;
        case "d" :
            facteur = 0.1;
            break;
        case "c" :
            facteur = 0.01;
            break;
        case "m" :
            facteur = 0.001;
            break;
        case "mu":
            facteur = 0.000001;
            break;
        case "n" :
            facteur = 0.000000001;
            break;
        case "p" :
            facteur = 0.000000000001;
            break;
        case "f" :
            facteur = 0.000000000000001;
            break;
    }
    return facteur;
}

When it works for some of you, it es probably just a behaviour for a navigator, not a programmation problem. Thanks for your help !

Upvotes: 0

Dathan
Dathan

Reputation: 4553

The data you are reading from file is encoded, you have to decode it.

Use the following

switch(decodeURIComponent(multiple))

e.g.

let decodedData = decodeURIComponent(`%c2%b5`)
console.log('decodedData : ', decodedData)

Upvotes: 0

Stefan Becker
Stefan Becker

Reputation: 5962

Can't reproduce your problem in REPL of Node

$ node --version
v8.9.4
$ node
> function test(m) {
... switch(m) {
..... case "μ": return 100; break;
..... default: return 0; break;
..... }
... }
undefined
> test('a')
0
> test('μ')
100
>

Works even with LC_ALL=C

PS: I know this is not an answer, but SO doesn't let me comment yet.

Upvotes: 0

arhak
arhak

Reputation: 2542

My file is in UTF8 without BOM in notepad++,

check after entering the letter whether it remain as such, I guess it changes after pasting it you can try also with accented letters like á and check whether the file is still UTF-8 without BOM, if it is not, change it back to it, and fix the letter if it turned into something else, from that point it will be ok

Upvotes: 0

Izio
Izio

Reputation: 398

var mu = "µ"
console.log("µ".localeCompare(mu, "pl");)

Should be fine

Upvotes: 1

Related Questions