← Back to context

Comment by golergka

11 years ago

Do you seriously think that "word starts with a capital letter" is an easy function to write?

I feel like you haven't spent enough time with Unicode.

return string[0] === string[0].toUpperCase();

You're welcome!

  • WRONG. Typical junior developer mistake. Even if we disregard trivial problems such as non-letter input or empty input, this will only work with English and a few other languages. This will TOTALLY fail with e.g. Turkish input (I ı vs İ i).

    • Not really. (string[0] == string[0].toUpperCase() && string[0] != string[0].toLowerCase()) is the correct way to approach this problem. If toUpperCase() and toLowerCase() don't handle Turkish letters, then that's a BUG in those methods, which should be reported and someone should freaking take responsibility for them and fix them.

      Adding another module with a method does not fix the original problem, it just creates more problems for other people to solve.

  • > var isCapital function(s) { return s[0] === s[0].toUpperCase(); };

    > isCapital("שלום");

    true

    > isCapital("1");

    true

    > isCapital('\uD83D\uDE00'); // Smiling emoji

    true

    > isCapital(\u279B); // Right-facing arrow

    true

    > isCapital("\u061Casd"); //Bidirectional control character

    true

    > isCapital(" ");

    true

    • If "true" is not a valid answer, what would've been one? Similar code in C# returns the same. E.g. Console.WriteLine("שלום".ToUpperInvariant()=="שלום") returns true.

      3 replies →

  • Not that I agree with micro modules (I would rather see a set of js core libs), but your code fails with empty strings.

    • That code fails in a whole host of cases, kind of proving that sometimes trivial "just one line" functions aren't actual that trivial to get right.

      edit: fortunately there's an npm modules you can included to fix it...

    • Maybe F1axs knows that at this particular spot the string will never be empty? There are two philosophies in libraries; one is to check for every possible error in the lib function, the other is to state the pre-conditions in the documentation.

    • > Not that I agree with micro modules (I would rather see a set of js core libs)

      Why not both? If you just want one part of a module, you can just import the dependency directly, if you want a whole core lib, you can do that.

      Some people really like underscore and import that. I use ES6/7 functions for the most part, but underscore has some useful things I can import on their own.