Humanizer meets all your NodeJs needs for manipulating and displaying strings, dates, times, timespans, numbers and quantities.
Humanizer meets all your NodeJs needs for manipulating and displaying strings, dates, times, timespans, numbers and quantities.
npm i humanizer.node
Import the Extensions:
require('humanizer.node')
//Your imports here
let result = (10).megabytes().toString()
console.log(result) // -> 10.00 MB
Humanizer includes a port of the brilliant ByteSize library.
Quite a few changes and additions are made on ByteSize
to make the interaction with ByteSize
easier and more consistent with the Humanizer API.
Here is a few examples of how you can convert from numbers to byte sizes and between size magnitudes:
require('humanizer.node')
let fileSize = (10).kilobytes()
console.log(fileSize.bits) // -> 81920
console.log(fileSize.bytes) // -> 10240
console.log(fileSize.kilobytes) // -> 10
console.log(fileSize.megabytes) // -> 0.009765625
console.log(fileSize.gigabytes) // -> 0.0000095367431640625
console.log(fileSize.terabytes) // -> 9.313225746154785e-9
There are a few extension methods that allow you to turn a number into a ByteSize instance:
(3).bits();
(5).bytes();
(10.5).kilobytes();
(2.5).megabytes();
(10.2).gigabytes();
(4.7).terabytes();
You can also add/subtract the values
let f = (4).gigabytes().add((22).megabytes()).subtract((980).kilobytes()).addGigabytes(1)
console.log(f.toString()) // -> 5.020549774169922 GB
Pluralize
pluralizes the provided input while taking irregular and uncountable words into consideration:
require('humanizer.node')
"Man".pluralize() // -> Men
"string".pluralize() // -> "strings"
Singularize
singularizes the provided input while taking irregular and uncountable words into consideration:
"Men".singularize() //-> "Man"
"strings".singularize() //-> "string"
require('humanizer.node')
(1).ordinalize() => "1st"
(5).ordinalize() => "5th"
require('humanizer.node')
"case".toQuantity(0) => "0 cases"
"case".toQuantity(1) => "1 case"
"case".toQuantity(5) => "5 cases"
"man".toQuantity(0) => "0 men"
"man".toQuantity(1) => "1 man"
"man".toQuantity(2) => "2 men"
ToQuantity can figure out whether the input word is singular or plural and will singularize or pluralize as necessary:
"men".toQuantity(2) => "2 men"
"process".toQuantity(2) => "2 processes"
"process".toQuantity(1) => "1 process"
"processes".toQuantity(2) => "2 processes"
"processes".toQuantity(1) => "1 process"
You can also pass a second argument, ShowQuantityAs
, to toQuantity
to specify how you want the provided quantity to be outputted. The default value is ShowQuantityAs.Numeric
which is what we saw above. The other two values are ShowQuantityAs.Words
and ShowQuantityAs.None
.
"case".toQuantity(5, ShowQuantityAs.Words) => "five cases"
"case".toQuantity(5, ShowQuantityAs.None) => "cases"
Humanizer can change numbers to words using the toWords
extension:
require('humanizer.node')
(1).toWords() => "one"
(10).toWords() => "ten"
(11).toWords() => "eleven"
(122).toWords() => "one hundred and twenty-two"
(3501).toWords() => "three thousand five hundred and one"
require('humanizer.node')
(0).toOrdinalWords() => "zeroth"
(1).toOrdinalWords() => "first"
(2).toOrdinalWords() => "second"
(8).toOrdinalWords() => "eighth"
(10).toOrdinalWords() => "tenth"
(11).toOrdinalWords() => "eleventh"
(12).toOrdinalWords() => "twelfth"
(20).toOrdinalWords() => "twentieth"
(21).toOrdinalWords() => "twenty first"
(121).toOrdinalWords() => "hundred and twenty-first"
Humanizer can change numbers to Roman numerals using the toRoman
extension. The numbers 1 to 10 can be expressed in Roman numerals as follows:
(1).toRoman() => "I"
(2).toRoman() => "II"
(3).toRoman() => "III"
(4).toRoman() => "IV"
(5).toRoman() => "V"
(6).toRoman() => "VI"
(7).toRoman() => "VII"
(8).toRoman() => "VIII"
(9).toRoman() => "IX"
(10).toRoman() => "X"
Also the reverse operation using the fromRoman
extension.
"I".fromRoman() => 1
"II".fromRoman() => 2
"III".fromRoman() => 3
"IV".fromRoman() => 4
"V".fromRoman() => 5
Humanizer can change numbers to Metric numerals using the toMetric
extension. The numbers 1, 1230 and 0.1 can be expressed in Metric numerals as follows:
(1).toMetric() => "1"
(1230).toMetric() => "1.23k"
(0.1).toMetric() => "100m"
Also the reverse operation using the fromMetric
extension.
"1".fromMetric() => 1
"1.23k".fromMetric() => 1230
"100m".fromMetric() => 0.1