4
\$\begingroup\$

I'm working on a few JavaScript functions for myself that I hope come in handy sometime. The first I wrote was a Capitalization function (yeah i know, real original. whatever) I was just wondering if

  1. I'm doing anything incredibly stupid
  2. There's anyway to make the code below more concise (like cool JS concision features i don't know about, tightening down on program flow, etc.)
  3. There's anything else cool i can do

Code:

String.prototype.capitalize = function(everyWord) {
    var words = this.split(" ");
    if (everyWord == true) {
        for (i = 0; i < words.length; i++) {
            words[i] = cap(words[i]);
        }
    }
    else {
        words[0] = cap(words[0]);
    }
    return words.join(" ");

    function cap(word) {
        return word.charAt(0).toUpperCase() + word.slice(1);
    }
};
var x = "abraham lincoln yada yada";
x = x.capitalize();
alert(x);
x = x.capitalize(true);
alert(x);

Thanks! http://jsfiddle.net/thomas4g/LpwRn/31/

\$\endgroup\$

2 Answers 2

4
\$\begingroup\$
function capitalize(str, everyWord) {
    var r = /\b(\w)/
    if (everyWord) r = /\b(\w)/g
    return str.replace(r, function (c) { return c.toUpperCase(); });
}

Regexes can be convenient for situations like this one...

\$\endgroup\$
3
  • 1
    \$\begingroup\$ +1 for using string manipuation. although this is window so make it either of the prototype or pass in the str value. \$\endgroup\$
    – Raynos
    Jun 1, 2011 at 16:06
  • \$\begingroup\$ @Raynos: OK, I switched to a classical function, instead, and I took in account the everyWord parameter which I forgot initially... Thanks for the remark. Note: actually, my first attempt tested the built in JavaScript capitalize() function... :-( \$\endgroup\$
    – PhiLho
    Jun 1, 2011 at 17:22
  • \$\begingroup\$ if you are going for real concision, how about calling your param ew and saying: var r=(ew)?/b(\w)/g:/\b(\w)/; \$\endgroup\$ Sep 28, 2011 at 21:36
4
\$\begingroup\$

Live example

String.capitalize = function(str, everyWord) {
    return _.map(_.words(str), function(val, key) {
        return key === 0 || everyWord ? _.capitalize(val) : val;
    }).join(" ");
};

Write to String not String.prototype. Extending native prototypes is not friendly.

Use underscore and underscore.string.

Basically don't re-invent the wheel ;)

\$\endgroup\$
4
  • 1
    \$\begingroup\$ but i want to reinvent the wheel. For my own learning purposes, and also so i can quickly grab my own code when i want it for a project instead of downloading something, etc. Also, what's the actual advantage of not using prototype? Other than not reinventing the wheel, anything else? (Thanks, btw :-) ) \$\endgroup\$ May 31, 2011 at 14:53
  • \$\begingroup\$ @ThomasShields other code breaks if you edit the prototype (Classic for ... in \$\endgroup\$
    – Raynos
    May 31, 2011 at 15:04
  • \$\begingroup\$ Object.prototype is the only one which breaks for..in if you add to it - you wouldn't/shouldn't need to use that operation with any other builtins. Array.prototype can be dicey in a mixed codebase purely because there are common methods people want to add, and they may clobber each other or get there first with a subtly different implementation. The rest of the builtin prototypes, I doubt anyone is that bothered about... \$\endgroup\$ Jun 1, 2011 at 16:00
  • \$\begingroup\$ @insin for (var i in "foobar"). I just generally dislike editing prototypes on native objects because it adds undocumented features and generally confuses state. \$\endgroup\$
    – Raynos
    Jun 1, 2011 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.