-1

In my project I need to take care about the case insensitive, and I don't know how can I code something like that in JavaScript.

If I write on my terminal, I need for my code to understand the same thing :

`BOB
bob 
Bob`

My code :

       #!/usr/bin/env node

let chunk = "";

process.stdin.on("data", data => {
    chunk += data.toString();
});

process.stdin.on("end", () => {
    chunk.replace(/^\s*[\r\n]/gm,"").split(/\s+/).ignoreCase.forEach(function (s) {
        process.stdout.write(
        s === 'bob'
        ? 'boy \n'
        : s === 'alicia'
           ? 'girl\n'
           : s === 'cookie'
               ? 'dog \n'
               : 'unknown \n');
    });
});

The result I need to display is :

`boy
boy
boy`

I tried to do it with ignoreCase but it does not work, can you explain me why please?

3 Answers3

2

Simply use String.prototype.toLowerCase on all your strings on input so that when you compare them there is only one way they can be represented.

process.stdin.on("data", data => {
  chunk += data.toString().toLowerCase();
});
zfrisch
  • 8,474
  • 1
  • 22
  • 34
  • toUpperCase is recommended – mplungjan Oct 09 '18 at 16:54
  • 3
    @mplungjan why? – zfrisch Oct 09 '18 at 16:55
  • @mplungjan are there weird Unicode corner-cases that make converting to upper case more reliable? – Pointy Oct 09 '18 at 16:57
  • Thank you @zfrisch for you answer – Shirley Truffier-Blanc Oct 09 '18 at 16:59
  • [answer here, maybe(?)](https://stackoverflow.com/questions/26877514/is-it-better-to-compare-strings-using-tolowercase-or-touppercase-in-javascript) This deals with internationalization, but kind of answers the question of "why", specifically the second comment on the original question about characters making a "round trip" – mhodges Oct 09 '18 at 17:01
  • 1
    @mhodges thanks! It DOES seem like a heckuva micro optimization, but I'll take a look! – zfrisch Oct 09 '18 at 17:03
  • 1
    @zfrisch Without internationalization, though, toLowerCase will almost always be more efficient since most text is lowercase (less character conversions to make). [More info about that here](https://www.petefreitag.com/item/175.cfm) – mhodges Oct 09 '18 at 17:05
  • https://stackoverflow.com/questions/2140627/javascript-case-insensitive-string-comparison and this https://learn.microsoft.com/en-gb/visualstudio/code-quality/ca1308-normalize-strings-to-uppercase?view=vs-2015 – mplungjan Oct 09 '18 at 17:06
  • @mplungjan That is only when dealing with internationalization and locale comparisons. In general, most string comparisons are more performant with `.toLowerCase()` for the reason I stated above – mhodges Oct 09 '18 at 17:09
  • Assuming that is an issue. – mplungjan Oct 09 '18 at 17:19
1

Just take the input and force it to all lower or upper case with String.toLowerCase() or String.toUpperCase() and then compare it to the same cased string:

console.log("test" === "Test"); // false
console.log("test" === "Test".toLowerCase()); // true
console.log("TeSt".toUpperCase() === "Test".toUpperCase()); // true
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • toUpperCase is recommended – mplungjan Oct 09 '18 at 16:54
  • 5
    @mplungjan Why? I've been in the JS world for 20+ years and never heard that. All that matters is that the strings be the same case - it doesn't matter whether it's upper or lower. – Scott Marcus Oct 09 '18 at 16:55
  • @mplungjan and by whom is it recommended? – Pointy Oct 09 '18 at 16:59
  • [answer here, maybe(?)](https://stackoverflow.com/questions/26877514/is-it-better-to-compare-strings-using-tolowercase-or-touppercase-in-javascript) This deals with internationalization, but kind of answers the question of "why", specifically the second comment on the original question about characters making a "round trip" – mhodges Oct 09 '18 at 17:00
  • https://learn.microsoft.com/en-gb/visualstudio/code-quality/ca1308-normalize-strings-to-uppercase?view=vs-2015 – mplungjan Oct 09 '18 at 17:06
  • @ScottMarcus sure enough you can write a quick test using `.fromCharCode()` to try all the 16-bit code points and some of them fail going to lower case and back to upper case. That's news to me. – Pointy Oct 09 '18 at 17:14
  • @zfrisch it's just a simple `for` loop from 0 to 65535 (probably should stop before then), callling `String.fromCharCode()` and checking whether going to lower case and back to upper case results in the same string as just going straight to upper case. – Pointy Oct 09 '18 at 18:08
  • @Pointy oh, I see! I'll build that out and check for myself. Seems very odd, but an interesting discussion point. – zfrisch Oct 09 '18 at 18:11
  • @Pointy Thanks. Learn something new every day! – Scott Marcus Oct 09 '18 at 19:39
1

The RegExp.prototype.ignorecase property holds a boolean value for whether the the "i" flag is set for a regular expression. This is not a function and does not provide any manipulation operation to the expression or string.

See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/ignoreCase

What you may want to consider doing is calling something like the String.prototype.toLowerCase() function which will convert the string to lower case.

Edit: if it helps, I think you might place the toLowerCase() before the split(), since toLowerCase() is the String's function, not an array's function. And unless you want to later call it separately on each string, might be fastest to do it in one place, something like this:

chunk.replace(/^\s*[\r\n]/gm,"").toLowerCase().split(/\s+/).forEach(function (s) {
   // function here
});
Neely
  • 111
  • 2
  • 4