Add lesser-known lodash gems to your toolbox - Strings

This is a part one of lodash series.

  1. Strings
  2. Objects

lodash has a lot of hidden gems and by using them you can avoid copy-pasting untested snippets from StackOverflow!

If you're not familiar with lodash or Underscore then I highly suggest to checkout out commonly used functions such as find, take, map, reduce, where, filter, sortBy etc.

In the first post of the series we'll look at useful string manipulation functions.

_.kebabCase, _.snakeCase, _.camelCase and _.deburr

We often need to convert a text given by a user to a technical identifier. This might be the case when the user inputs name for an object and we need to represent that in a human-readable form in the URL. In the case of the URL we want to avoid letters that require encoding as that would ruin readability. For example: empty space would be %20.

Kebab case fits perfectly to the URL case. Let's take Oatmeal's great comic title as an example:

_.kebabCase('Why Nikola Tesla was the greatest geek who ever lived')

-> "why-nikola-tesla-was-the-greatest-geek-who-ever-lived"

How about an array?

_.kebabCase(['Jean-luc', 'Picard'])

-> "jean-luc-picard"


Internally kebabCase, snakeCase and camelCase use a function called deburr.

Let me quote the documentation of the deburr:

Deburrs string by converting latin-1 supplementary letters to basic latin letters and removing combining diacritical marks.

This basically means that if you have a word like Ääliö (which is moron in Finnish) it would be converted like this:


-> "aalio"

Be sure to check classic functions such as trim (has a bit more options than native) and pad (looks really bad hand-written).

More (hopefully) helpful tips coming in the next post. Stay tuned!