EqualsIgnoreCase in Javascript

0

In Java, the equalsIgnoreCase() method is used to check case sensitivity. In Javascript?

I have the following example: perform the function " existeDisco(titulo):boolean ". Returns true if the disk whose title matches the one passed as a parameter exists. The method is case-insensitive.

Code of the requested function:

this.existeDisco = function (nombreRecibido){
        for(var i=0; i<this.arrayDiscos.length; i++){
            var nombre = this.arrayDiscos[i].nombre;
            if(nombre == nombreRecibido)
                return this.arrayDiscos[i];
        }
        return false;
    }

How do we make the function detect the nombreRecibido in uppercase or lowercase as long as it is the exact name? That is to say: "Fernando" or "FERNANDO" or "fernando".

    
asked by omaza1990 29.05.2017 в 21:17
source

2 answers

0

Make a shift to Uppercase [ toUpperCase() ] or Lower case [ toLowerCase() ]

this.existeDisco = function (nombreRecibido){
    for(var i=0; i<this.arrayDiscos.length; i++){
        var nombre = this.arrayDiscos[i].nombre;
        if(nombre.toUpperCase() == nombreRecibido.toUpperCase())
            return this.arrayDiscos[i];
    }
    return false;
}

Or you can try with Regular Expressions something like this:

Using RegExp.test

this.existeDisco = function (nombreRecibido){
    var regex = new RegExp("^"+nombreRecibido+"$","gi")
    for(var i=0; i<this.arrayDiscos.length; i++){
        var nombre = this.arrayDiscos[i].nombre;
        if(regex.test(nombre))
            return this.arrayDiscos[i];
    }
    return false;
}

Or using String.match ()

this.existeDisco = function (nombreRecibido){
    var regex = new RegExp("^"+nombreRecibido+"$","gi")
    for(var i=0; i<this.arrayDiscos.length; i++){
        var nombre = this.arrayDiscos[i].nombre;
        if(nombre.match(regex))
            return this.arrayDiscos[i];
    }
    return false;
}

A simplified form, but with less performance:

this.existeDisco = function (nombreRecibido){
    var regex = new RegExp("^"+nombreRecibido+"$","gi")
    var disco = this.arrayDiscos.find(disc){
        return disc.nombre.match(regex);
    };

    return disco ? disco :false;
}
    
answered by 29.05.2017 в 21:23
0

Another option would be to create the function equals in Class String and receive as parameters the value to be compared and a flag indicating if IgnoreCase or not.

You define the function in the following way:

String.prototype.equals = function(value, ignoreCase) {
    var i = ignoreCase ? "i":"";

    var regex = new RegExp("^"+value+"$","g"+i);
    return this.match(regex);
};

You use it like this:

this.existeDisco = function (nombreRecibido){
    for(var i=0; i<this.arrayDiscos.length; i++){
        var nombre = this.arrayDiscos[i].nombre;

        //comparas el resultado de equals, si son iguales retornara la cadena, si no, retornara null
        if(nombre.equals(nombreRecibido, true))
            return this.arrayDiscos[i];
    }
    return false;
}
    
answered by 31.05.2017 в 04:00