Last active
November 21, 2016 04:58
-
-
Save Uysim/436bc33b27875e70d4626d58579a1068 to your computer and use it in GitHub Desktop.
Coffeescript helper convert object into human readable (under development)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.Humanize = | |
# Humanize.numberToCurrency(100) => "$100" | |
# Humanize.numberToCurrency(10000) => "$10,000" | |
# Humanize.numberToCurrency(50.0501) => "$50.0501" | |
# Humanize.numberToCurrency(50.0501, { precision: 2 }) => "$50.05" | |
# Humanize.numberToCurrency(50, { unit: '£' }) => "£50" | |
# Humanize.numberToCurrency(10000, { delimiter: ';' }) => "$10;000" | |
numberToCurrency: (number, paramsOptions={})-> | |
defaultOptions = | |
unit: '$' | |
precision: 2 | |
delimiter: ',' | |
options = $.extend({}, defaultOptions, paramsOptions) | |
delimiter = options.delimiter | |
precision = options.precision | |
unit = options.unit | |
fixed = Humanize.toFixed(number, precision) | |
delimited = fixed.replace(/\B(?=(\d{3})+(?!\d))/g, delimiter); | |
"#{unit}#{delimited}" | |
# Humanize.toFixed(0.123456) => "0.123" | |
# Humanize.toFixed(0.123456, 3) => "0.123" | |
toFixed: (number, precision=2)-> | |
number.toFixed(precision).replace(/\.?0*$/,'') | |
# Humanize.capitalize("hello") => "Hello" | |
# Humanize.capitalize("hello world") => "Hello World" | |
# Humanize.capitalize("hello-world") => "Hello-world" | |
capitalize: (value) -> | |
value.replace /(^|\s)([a-z])/g, (m, p1, p2) -> | |
p1 + p2.toUpperCase() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment