• Stars
    star
    907
  • Rank 48,238 (Top 1.0 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 11 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple utility library for making the web more humane. #hubspot-open-source

Humanize Plus

npm version build status npm downloads

A simple utility library for making the web more humane.

Getting Started

Humanize Plus is available via node package manager.

npm install humanize-plus

Or download the minified version or the full version.

In your web page:

<script src="public/humanize.min.js"></script>
<script>
var capitalized = Humanize.capitalize("ten tiny ducklings.")
// "Ten tiny ducklings."
</script>

In your node package.json:

"dependencies": {
  "humanize-plus": "^1.7.0"
}

For recent changes, see the changelog.

API Methods

Numbers

formatNumber

Formats a number to a human-readable string. Localize by overriding the precision, thousand and decimal arguments.

Humanize.formatNumber(123456789, 2)
// "123,456,789.00"
intComma

Converts an integer to a string containing commas every three digits.

Humanize.intComma(123456789)
// "123,456,789"
intcomma - DEPRECATED - This method will not be present in the next major version.

Alias for intComma

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.intword(123456789, 'nopnopnopnop', 1)
// "123.5M"

Humanize.intword(123456789, 'this is a nop', 3)
// "123.457M"

Humanize.intword(10, 'still a nop', 1)
// "10"
compactInteger

Converts an integer into its most compact representation. Decimal precision is ignored for all integers, n, such that abs(n) < 1000.

Humanize.compactInteger(123456789, 1)
// "123.5M"

// Switch to scientific notation for trillons, because no one knows those abbreviations.
Humanize.compactInteger(-7832186132456328967, 4)
// "-7.8322x10^18"

Humanize.compactInteger(-100, 2)
// "-100"
boundedNumber

Bounds a value from above. Modified values have customizable ending strings ('+' by default)

Humanize.boundedNumber(110, 100)
// "100+"

Humanize.boundedNumber(50, 100)
// "50"
truncatenumber - DEPRECATED - This method will not be present in the next major version.

Alias for boundedNumber

ordinal

Converts an integer to its ordinal as a string.

Humanize.ordinal(22)
// "22nd"
times

Interprets numbers as occurences. Also accepts an optional array/map of overrides.

for (i=0; i<5; i++) {
  Humanize.times(i, {"4": "too many"});
  // Bonus!
  if (i === 1) {
    Humanize.times(1.1);
  }
}
// never
// once
// 1.1 times
// twice
// 3 times
// too many times
pace

Matches a pace (value and interval) with a logical time frame. Very useful for slow paces.

second = 1000
week = 6.048e8
decade = 3.156e11

Humanize.pace(1.5, second, "heartbeat")
// Approximately 2 heartbeats per second

Humanize.pace(4, week)
// Approximately 4 times per week

Humanize.pace(1, decade, "life crisis")
// Less than 1 life crisis per week
fileSize

Formats the value like a 'human-readable' file size (i.e. '13 KB', '4.1 MB', '102 bytes', etc).

Humanize.fileSize(1024 * 20)
// "20 Kb"

Humanize.fileSize(1024 * 2000)
// "1.95 Mb"

Humanize.fileSize(Math.pow(1000, 4))
// "931.32 Gb"
filesize - DEPRECATED - This method will not be present in the next major version.

Alias for fileSize

pluralize

Returns the plural version of a given word if the value is not 1. The default suffix is 's'.

Humanize.pluralize(1, "duck")
// "duck"

Humanize.pluralize(3, "duck")
// "ducks"

Humanize.pluralize(3, "duck", "duckies")
// "duckies"

Strings

truncate

Truncates a string if it is longer than the specified number of characters. Truncated strings will end with a translatable ellipsis sequence ("…").

Humanize.truncate('long text is good for you')
// "long text is good for you"

Humanize.truncate('long text is good for you', 19)
// "long text is goo..."

Humanize.truncate('long text is good for you', 19, '... etc')
// "long text is... etc"
truncateWords

Truncates a string after a certain number of words.

Humanize.truncateWords('long text is good for you', 5)
// "long text is good for ..."
truncatewords - DEPRECATED - This method will not be present in the next major version.

Alias for truncateWords

nl2br and br2nl

Flexible conversion of <br/> tags to newlines and vice versa.

// Use your imagination
capitalize

Capitalizes the first letter in a string, optionally downcasing the tail.

Humanize.capitalize("some boring string")
// "Some boring string"

Humanize.capitalize("wHoOaA!")
// "WHoOaA!"

Humanize.capitalize("wHoOaA!", true)
// "Whooaa!"
capitalizeAll

Captializes the first letter of every word in a string.

Humanize.capitalizeAll("some boring string")
// "Some Boring String"
titleCase

Intelligently capitalizes eligible words in a string and normalizes internal whitespace.

Humanize.titleCase("some of a boring string")
// "Some of a Boring String"

Humanize.titleCase("cool the          iTunes cake, O'Malley!")
// "Cool the iTunes Cake, O'Malley!"
titlecase - DEPRECATED - This method will not be present in the next major version.

Alias for titleCase

Arrays

oxford

Converts a list of items to a human readable string with an optional limit.

items = ['apple', 'orange', 'banana', 'pear', 'pineapple']

Humanize.oxford(items)
// "apple, orange, banana, pear, and pineapple"

Humanize.oxford(items, 3)
// "apple, orange, banana, and 2 others"

// Pluralizes properly too!
Humanize.oxford(items, 4)
// "apple, orange, banana, pear, and 1 other"

Humanize.oxford(items, 3, "and some other fruits")
// "apple, orange, banana, and some other fruits"
frequency

Describes how many times an item appears in a list

catPics = [
  'https://media2.giphy.com/media/JIX9t2j0ZTN9S/giphy.gif',
  'https://media3.giphy.com/media/uzglgIsyY1Cgg/giphy.gif'
]
dogPics = []

"Cats " + Humanize.frequency(catPics, "typed on keyboards")
// "Cats typed on keyboards 3 times"

"Dogs " + Humanize.frequency(docPics, "typed on keyboards")
// "Dogs never typed on keyboards"

Utility methods

toFixed

Fixes binary rounding issues (eg. (0.615).toFixed(2) === "0.61").

Humanize.toFixed(0.615, 2)
// "0.62"
normalizePrecision

Ensures precision value is a positive integer.

Humanize.normalizePrecision(-232.231)
// 232

Important notes

Please don't edit files in the dist subdirectory as they are generated through compilation. You'll find source code in the src subdirectory!

Compiling

npm run install && npm run build

And that's it!

The project will compile the CoffeeScript files into the dist subdirectory.

Testing

npm run test

License

Copyright (c) 2013-2016 HubSpotDev Licensed under the MIT license.

More Repositories

1

youmightnotneedjquery

Astro
14,118
star
2

offline

Automatically display online/offline indication to your users
CSS
8,679
star
3

odometer

Smoothly transitions numbers with ease. #hubspot-open-source
CSS
7,259
star
4

vex

A modern dialog library which is highly configurable and easy to style. #hubspot-open-source
CSS
6,932
star
5

messenger

Growl-style alerts and messages for your app. #hubspot-open-source
JavaScript
4,035
star
6

drop

A library for creating dropdowns and other floating elements. #hubspot-open-source
CSS
2,360
star
7

BuckyClient

Collect performance data from the client
CoffeeScript
1,738
star
8

sortable

Drop-in script to make tables sortable
CSS
1,322
star
9

select

Styleable select elements built on Tether. #hubspot-open-source
JavaScript
1,197
star
10

Singularity

Scheduler (HTTP API and webapp) for running Mesos tasksβ€”long running processes, one-off tasks, and scheduled jobs. #hubspot-open-source
Java
816
star
11

tooltip

CSS Tooltips built on Tether. #hubspot-open-source
CSS
711
star
12

jinjava

Jinja template engine for Java
Java
653
star
13

signet

Display a unique seal in the developer console of your page
CoffeeScript
564
star
14

draft-convert

Extensibly serialize & deserialize Draft.js ContentState with HTML.
JavaScript
483
star
15

hubspot-php

HubSpot PHP API Client
PHP
342
star
16

cms-theme-boilerplate

A straight-forward starting point for building a great website on the HubSpot CMS
HTML
320
star
17

react-select-plus

Fork of https://github.com/JedWatson/react-select with option group support
JavaScript
281
star
18

hubspot-api-python

HubSpot API Python Client Libraries for V3 version of the API
Python
278
star
19

hubspot-api-nodejs

HubSpot API NodeJS Client Libraries for V3 version of the API
TypeScript
278
star
20

SlimFast

Slimming down jars since 2016
Java
270
star
21

dropwizard-guice

Adds support for Guice to Dropwizard
Java
268
star
22

gc_log_visualizer

Generate multiple gnuplot graphs from java gc log data
Python
200
star
23

BuckyServer

Node server that receives metric data over HTTP & forwards to your service of choice
CoffeeScript
195
star
24

hubspot-api-php

HubSpot API PHP Client Libraries for V3 version of the API
PHP
176
star
25

general-store

Simple, flexible store implementation for Flux. #hubspot-open-source
JavaScript
173
star
26

hubspot-cli

A CLI for HubSpot
JavaScript
142
star
27

facewall

Grid visualization of Gravatars for an organization
CoffeeScript
138
star
28

jackson-datatype-protobuf

Java
116
star
29

draft-extend

Build extensible Draft.js editors with configurable plugins and integrated serialization.
JavaScript
115
star
30

slack-client

An asynchronous HTTP client for Slack's web API
Java
112
star
31

prettier-maven-plugin

Java
112
star
32

Rosetta

Java library that leverages Jackson to take the pain out of mapping objects to/from the DB, designed to integrate seamlessly with jDBI
Java
110
star
33

hubspot-api-ruby

HubSpot API Ruby Client Libraries for V3 version of the API
Ruby
107
star
34

Baragon

Load balancer API
Java
105
star
35

mixen

Combine Javascript classes on the fly
CoffeeScript
85
star
36

jquery-zoomer

Zoom up your iFrames
JavaScript
81
star
37

hapipy

A Python wrapper for the HubSpot APIs #hubspot-open-source
Python
78
star
38

oauth-quickstart-nodejs

A Node JS app to get up and running with the HubSpot API using OAuth 2.0
JavaScript
74
star
39

oneforty-data

Open data on 4,000+ social media apps from oneforty.com #hubspot-open-source
Ruby
70
star
40

cms-react-boilerplate

JavaScript
65
star
41

Blazar-Archive

An out-of-this world build system!
Java
62
star
42

haPiHP

An updated PHP client for the HubSpot API
PHP
61
star
43

sanetime

A sane date/time python interface #hubspot-open-source
Python
60
star
44

sample-workflow-custom-code

Sample code snippets for the custom code workflow action.
60
star
45

hubspot-cms-vscode

A HubL language extension for the Visual Studio Code IDE, allowing for πŸš€ fast local HubSpot CMS Platform development.
TypeScript
56
star
46

ui-extensions-examples

This repository contains code examples of UI extensions built with HubSpot CRM development tools beta
TypeScript
56
star
47

BidHub-CloudCode

The Parse-based brains behind BidHub, our open-source silent auction app.
JavaScript
50
star
48

teeble

A tiny table plugin
JavaScript
49
star
49

hubspot.github.com

HubSpot Open Source projects.
JavaScript
46
star
50

BidHub-iOS

iOS client for BidHub, our open-source silent auction app.
Objective-C
44
star
51

calling-extensions-sdk

A JavaScript SDK for integrating calling apps into HubSpot.
JavaScript
43
star
52

dropwizard-guicier

Java
42
star
53

live-config

Live configuration for Java applications #hubspot-open-source
Java
37
star
54

hubspot-cms-deploy-action

GitHub Action to deploy HubSpot CMS projects
HTML
36
star
55

executr

Let your users execute the CoffeeScript in your documentation
JavaScript
35
star
56

transmute

kind of like lodash but works with Immutable
JavaScript
35
star
57

NioImapClient

High performance, async IMAP client implementation
Java
33
star
58

colorshare

Style up your social share buttons
CSS
32
star
59

cms-event-registration

JavaScript
32
star
60

ChromeDevToolsClient

A java websocket client for the Chrome DevTools Protocol
Java
31
star
61

integration-examples-php

PHP
31
star
62

canvas

HubSpot Canvas is the design system that we at HubSpot use to build our products.
JavaScript
29
star
63

recruiting-agency-graphql-theme

A theme based off of the HubSpot CMS Boilerplate. This theme includes modules and templates that demonstrate how to utilize GraphQL as part of a website built with HubSpot CMS and Custom CRM Objects.
HTML
29
star
64

vee

A personal proxy server for web developers
CoffeeScript
28
star
65

cms-js-building-block-examples

Several example projects to introduce developers to JS building blocks on the HubSpot CMS
JavaScript
28
star
66

integration-examples-nodejs

JavaScript
27
star
67

NioSmtpClient

Smtp Client based on Netty
Java
26
star
68

sample-apps-list

The list of Sample applications using HubSpot Public API
25
star
69

jackson-jaxrs-propertyfiltering

Java
25
star
70

local-cms-server-cli

Command line tools for local cms development
CSS
22
star
71

astack

Simple stacktrace analysis tool for the JVM
Python
22
star
72

prettier-plugin-hubl

JavaScript
20
star
73

Horizon

Java
20
star
74

rHAPI

Ruby wrapper for the HubSpot API (HAPI)
Ruby
20
star
75

integrate

Confirm that your application works.
CoffeeScript
20
star
76

moxie

A TCP proxy guaranteed to make you smile. #hubspot-open-source
Python
18
star
77

BidHub-WebAdmin

Keep an eye on BidHub while it's doing its thing.
HTML
18
star
78

hbase-support

Supporting configs and tools for HBase at HubSpot
Java
17
star
79

sprocket

A better REST API framework for django
Python
17
star
80

react-decorate

Build composable, stateful decorators for React.
JavaScript
16
star
81

cms-custom-objects-example

CSS
16
star
82

hubspot-immutables

Java
16
star
83

hubspot-academy-tutorials

JavaScript
16
star
84

cms-vue-boilerplate

Boilerplate Vue project for creating apps using modules on the HubSpot CMS
JavaScript
16
star
85

maven-snapshot-accelerator

System to speed up dependency resolution when using Maven snapshots #hubspot-open-source
Java
16
star
86

httpQL

A small library for converting URL query arguments into SQL queries.
Java
15
star
87

algebra

Simple abstract data types (wrapping derive4j) in Java
Java
14
star
88

sample-apps-webhooks

Sample code and reference for processing HubSpot webhooks
JavaScript
13
star
89

sample-apps-oauth

Sample application demonstrating OAuth 2.0 flow with HubSpot API
Ruby
13
star
90

cms-webpack-serverless-boilerplate

Boilerplate for bundling serverless functions with webpack locally, prior to uploading to the CMS.
JavaScript
13
star
91

sample-apps-manage-crm-objects

Sample application in PHP, Python, Ruby and JavaScript demonstrating HubSpot API to manage CRM Objects
JavaScript
12
star
92

HubspotEmailTemplate

How to convert a regular html coded email template into ones that use HubSpot jinja tags.
12
star
93

hubstar

CSS
12
star
94

virtualenvchdir

Easily chdir into different virtualenvs #hubspot-open-source
Shell
12
star
95

cos_uploader

A python script for syncing a file tree to the HubSpot COS
Python
11
star
96

chrome_extension_workshop

Chrome extension workshop
JavaScript
10
star
97

collectd-gcmetrics

Python
10
star
98

guice-transactional

Java
10
star
99

private-app-starter

Boilerplates apps using HubSpot API
PHP
10
star
100

BaseWebApp

HTML
9
star