0

consider the following code:

lawCannotUse = lawCannotUse.map(function(cannotUse){
  console.log(cannotUse, lodashCapitalize(cannotUse));
  upperCaseCannotUse.push(lodashCapitalize(cannotUse));
});

lawCannotUse is an array of strings.

The console.log shows:

enter image description here

As you can see, the first on is capitalized, but nothing else is, attack should be and so should spark.

What is: lodashCapitalize ?

var lodashCapitalize  = require('../../../node_modules/lodash/string/capitalize');

Any ideas?

TheWebs
  • 12,470
  • 30
  • 107
  • 211

2 Answers2

2

It appears that all strings in your array after the first one have a leading space. _.capitalize makes no effort to trim the string or to uppercase the first alphanumeric character. You can, however, trim the strings manually using _.trim:

var _ = require('lodash');

lawCannotUse = lawCannotUse.map(function (cannotUse) {
  console.log(cannotUse, _.capitalize(_.trim(cannotUse)));
  // ...
});

You can also compose the two functions to produce a reusable function like so

var myCapitalize = _.compose(_.capitalize, _.trim);
// ...
myCapitalize("   abc");  // gives "Abc"
Igor Raush
  • 15,080
  • 1
  • 34
  • 55
  • Just a heads-up: As of version 4.0.0 'compose' was removed in favour of 'flowRight'. (https://github.com/lodash/lodash/wiki/Changelog). Another approach that worked for me would be to: `return _.startCase(_.camelCase(cannotUse));` – DavidP Apr 21 '16 at 05:00
-2

why dont use simply use JS .toUpperCase() ??????????

pure js, no need for any framework:

function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);

}

cpugourou
  • 775
  • 7
  • 11
  • Because `toUpperCase()` converts all characters in a string - not only the first one, so you would have to write your own capitalize-function anyway. ([Which isn't too hard](http://stackoverflow.com/questions/1026069/capitalize-the-first-letter-of-string-in-javascript)) But since this is a lodash question, why even go there? – ippi Nov 12 '15 at 00:18
  • sorry: function capitalizeFirstLetter(string) { return string.charAt(0).toUpperCase() + string.slice(1); } – cpugourou Nov 12 '15 at 00:26
  • I think too many are using frameworks for basic stuff. They should start to learn javascript before any framework. And when you know javascript you find all those framework totally useless. The only thing those frameworks do are slowing down your code. – cpugourou Nov 12 '15 at 00:30
  • 1
    Lodash is a utility library and can hardly be called a framework. It's very modular and consistent. The compatibility builds provides a simple way of assuring browser compatibility. Since you can choose per method what you want in your builds you can prevent any bloat. It helps you to be productive and stops you from reinventing the wheel over and over. – ippi Nov 12 '15 at 00:52
  • I agree. Nervetheless with pure js at the end you end up creating your very own framework. ;) avoiding tons of useless stuff. Dont know lodash and other similar. Never felt the need to but I understand the purpose. I am just seek meeting "supposedly" coders everyday that cant do without those and unable to answer simple js basic process... – cpugourou Nov 12 '15 at 01:00
  • 1
    Of course, everything can be done without the use of libraries/frameworks. However, since the question is tagged with lodash, we can assume that the project is already using it, in which case writing our own capitalization function is unnecessary. Besides, this answer does not address the issue in the question, which happened to be leading spaces in the strings. – Igor Raush Nov 12 '15 at 01:17
  • 1
    Ten question marks means (essentially) "WTF is wrong with you, you idiot!": https://dictionaryeyes.wordpress.com/2012/08/15/the-question-mark/ – random_user_name May 14 '16 at 22:05