As front-end developers, we're always looking to make the software we create as friendly as possible for actual humans to use.

We're frequently called on to transform all kinds of data — currencies, dates, times, arrays, and objects — into human-friendly versions for display. We'll often end up writing one-off functions for these small tasks. Which can be deceptively simple when you start out, so it doesn't feel like you're creating a maintenance burden for yourself, but in fact, you are.

Sometimes we can get away with using built-in methods, like Date.parse() or some fancy type conversion. But, that often doesn't go far enough. Simple string concatentation just doesn't cut it.

So here at HubSpot we improved on the best parts of current formatting libraries to create Humanize. This library has been helpful in creating a consistent, human voice throughout our applications. It has a simple API, and it's both performant and well tested.

What Is It?

Humanize is an "object to string" formatting library. Unlike other libraries that only let you format strings, Humanize helps you convert different types of data to human-readable text, like turning an array into a comma-separated list (oxford comma optional).

Examples:

Humanize.dictionary({apples: 'red', banana: 'yellow'}) // 'apples are red, bananas are yellow'
Humanize.oxford(items, 3) // "apple, orange, banana, and 2 others"
Humanize.formatNumber(123456789, 2) // "123,456,789.00"
Humanize.ordinal(22) // "22nd"
Humanize.filesize(Math.pow(1000, 4)) // "931.32 Gb"
Humanize.truncate('long text is good for you', 19) // "long text is goo..."

Naturally, Humanize also includes optimized versions of basic string formatting methods (truncate, pluralize, etc.), but there are (intentionally) no date formatting methods. For that, we highly recommend checking out moment.js.

Humanize (github) is written in CoffeeScript (because we love CoffeeScript) and compiled using Grunt, complete with Jasmine specs. As always, send us a pull request if you have any ideas for improvement. Or if you're just a regular human (that is, not a developer), please leave a comment on this post and let us know other ways we can make our output more human.

Image via masterdata.se

This is part of a series of posts on a bunch of new open source software released by HubSpot developers.

 


Recommended Articles

Join our subscribers

Sign up here and we'll keep you updated on the latest in product, UX, and engineering from HubSpot.

Subscribe to the newsletter