HubSpot Engineering

Latest posts

Writing lately

Building More Humane Software with humanize.js

In this post

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

Hand-lettered code: a String.format call whose format string is an unreadable jumble of braces, zeros and hash signs.

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.

Newsletter

For Agents

View Markdown

Share

More posts

Moving Mountains: How We Migrated from Enzyme to React Testing Library

How we migrated 76,000 tests from Enzyme to React Testing Library, modernizing our stack and allowing teams to ship changes with more confidence.

A Tech Guide to Web Accessibility: Why It Matters and How to Improve It

Web accessibility is a crucial aspect of building modern websites and applications, which helps ensure that people with disabilities have access to the same information and services as people without disabilities.

Immutability and Its (Potential) Impact on Performance

We recently encountered a performance bug that challenged the use of immutability in our frontend applications.