How to humanize text - cl-format
In the cljs slack channel (which you should join), someone asked if there is a cljs library for humanizing text. I pointed them to a new (to cljs) feature that’s already available via cljs.pprint
: cl-format
. (Note that this also applies to clj as it already supported pretty-printing thanks to Tom Faulhaber et al).
cl-format
is a cljs version of Common Lisp’s FORMAT
function. It provides a ton of functionality, so much so that there’s a full pdf describing it. For a gentler, yet still expansive description, check out a few FORMAT recipes.
Here are a few examples of humanizing text, taken from django.contrib.humanize and HubSpot/humanize.
From django:
-
apnumber
- For numbers 1-9, returns the number spelled out. Otherwise, returns the number. This follows Associated Press style. -
intcomma
- Converts an integer to a string containing commas every three digits. -
intword
- Converts a large integer to a friendly text representation.
From Hubspot:
-
capitalize
- Capitalizes the first letter in a string, downcasing the tail. -
capitalizeAll
- Capitalize the first letter of every word in a string.
cl-format
also provides support for pluralization, conditional formatting, and a whole host of other things. The pluralization only supports simple cases of adding “s” and “y” => “ies” and all printing is based on English, so it’s not useful in every case. But it’s very nice to have and it’s only a simple (require 'cljs.pprint)
away.