A simple utility library for making the web more humane.
Humanize Plus is available via node package manager.
npm install humanize-plus
In your web page:
In your node package.json:
For recent changes, see the changelog.
Formats a number to a human-readable string. Localize by overriding the precision, thousand and decimal arguments.
Converts an integer to a string containing commas every three digits.
intcomma - DEPRECATED - This method will not be present in the next major version.
intword - DEPRECATED - This method will not be present in the next major version.
Converts a large integer to a friendly text representation. This method is now a thin wrapper around compactInteger
Humanize.intword(num, ch, de) === Humanize.compactInteger(num, de)
Humanize// "123.5M"Humanize// "123.457M"Humanize// "10"
Converts an integer into its most compact representation. Decimal precision is ignored for all integers, n, such that abs(n) < 1000.
Humanize// "123.5M"// Switch to scientific notation for trillons, because no one knows those abbreviations.Humanize// "-7.8322x10^18"Humanize// "-100"
Bounds a value from above. Modified values have customizable ending strings ('+' by default)
Humanize// "100+"Humanize// "50"
truncatenumber - DEPRECATED - This method will not be present in the next major version.
Converts an integer to its ordinal as a string.
Interprets numbers as occurences. Also accepts an optional array/map of overrides.
for i=0; i<5; i++Humanize;// Bonus!if i === 1Humanize;// never// once// 1.1 times// twice// 3 times// too many times
Matches a pace (value and interval) with a logical time frame. Very useful for slow paces.
second = 1000week = 6048e8decade = 3156e11Humanize// Approximately 2 heartbeats per secondHumanize// Approximately 4 times per weekHumanize// Less than 1 life crisis per week
Formats the value like a 'human-readable' file size (i.e. '13 KB', '4.1 MB', '102 bytes', etc).
Humanize// "20 Kb"Humanize// "1.95 Mb"Humanize// "931.32 Gb"
filesize - DEPRECATED - This method will not be present in the next major version.
Returns the plural version of a given word if the value is not 1. The default suffix is 's'.
Humanize// "duck"Humanize// "ducks"Humanize// "duckies"
Truncates a string if it is longer than the specified number of characters. Truncated strings will end with a translatable ellipsis sequence ("…").
Humanize// "long text is good for you"Humanize// "long text is goo..."Humanize// "long text is... etc"
Truncates a string after a certain number of words.
Humanize// "long text is good for ..."
truncatewords - DEPRECATED - This method will not be present in the next major version.
nl2br and br2nl
Flexible conversion of
<br/> tags to newlines and vice versa.
// Use your imagination
Capitalizes the first letter in a string, optionally downcasing the tail.
Humanize// "Some boring string"Humanize// "WHoOaA!"Humanize// "Whooaa!"
Captializes the first letter of every word in a string.
Humanize// "Some Boring String"
Intelligently capitalizes eligible words in a string and normalizes internal whitespace.
Humanize// "Some of a Boring String"Humanize// "Cool the iTunes Cake, O'Malley!"
titlecase - DEPRECATED - This method will not be present in the next major version.
Converts a list of items to a human readable string with an optional limit.
items = 'apple' 'orange' 'banana' 'pear' 'pineapple'Humanize// "apple, orange, banana, pear, and pineapple"Humanize// "apple, orange, banana, and 2 others"// Pluralizes properly too!Humanize// "apple, orange, banana, pear, and 1 other"Humanize// "apple, orange, banana, and some other fruits"
Describes how many times an item appears in a list
aznPics =''''''bigfootPics ="Asians " + Humanize// "Asians took pictures of food 3 times""Bigfoot " + Humanize// "Bigfoot never took pictures of food"
Fixes binary rounding issues (eg. (0.615).toFixed(2) === "0.61").
Ensures precision value is a positive integer.
Please don't edit files in the
dist subdirectory as they are generated through compilation. You'll find source code in the
npm run install && npm run build
And that's it!
The project will compile the CoffeeScript files into the
npm run test
Copyright (c) 2013-2016 HubSpotDev Licensed under the MIT license.