Last active
November 21, 2016 05:00
-
-
Save Uysim/9cc3f532957fc00c7d11d4e3cd0a9c10 to your computer and use it in GitHub Desktop.
Javasript 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: function(number, paramsOptions) { | |
var defaultOptions, delimited, delimiter, fixed, options, precision, unit; | |
if (paramsOptions == null) { | |
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); | |
return "" + unit + delimited; | |
}, | |
// Humanize.toFixed(0.123456) => "0.123" | |
// Humanize.toFixed(0.123456, 3) => "0.123" | |
toFixed: function(number, precision) { | |
if (precision == null) { | |
precision = 2; | |
} | |
return number.toFixed(precision).replace(/\.?0*$/, ''); | |
}, | |
// Humanize.capitalize("hello") => "Hello" | |
// Humanize.capitalize("hello world") => "Hello World" | |
// Humanize.capitalize("hello-world") => "Hello-world" | |
capitalize: function(value) { | |
return value.replace(/(^|\s)([a-z])/g, function(m, p1, p2) { | |
return p1 + p2.toUpperCase(); | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment