Last active
August 3, 2017 22:46
-
-
Save naepalm/fe54545613d117b5386c9c14789ab3a4 to your computer and use it in GitHub Desktop.
Helper functions for react, such as price currency formatting.
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
var Helpers = { | |
/** | |
* @function formatMoney | |
* @param {number} n - The number to be formatted | |
* @param {string} d - The character to be used for the decimal. Defaults to ".". | |
* @param {string} t - the character to be used for the thousands separator. Defaults to ",". | |
* @param {int} c - the number of decimal places. Defaults to 2. | |
* @returns {string} | |
* @description Formats a number to a price | |
*/ | |
formatMoney: function(n, d, t, c){ | |
var c = isNaN(c = Math.abs(c)) ? 2 : c, | |
d = d == undefined ? "." : d, | |
t = t == undefined ? "," : t, | |
s = n < 0 ? "-" : "", | |
i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", | |
j = (j = i.length) > 3 ? j % 3 : 0; | |
return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : ""); | |
} | |
/** | |
* @function getQueryStringParameter | |
* @param {string} name - The query string key | |
* @param {string} url - An optional url, defaults to the current page | |
* @returns {string} | |
* @description Returns a value passed in via a querystring parameter | |
*/ | |
getQueryStringParameter: function(name, url) { | |
if (!url) url = window.location.href; | |
name = name.replace(/[\[\]]/g, "\\$&"); | |
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), | |
results = regex.exec(url); | |
if (!results) return null; | |
if (!results[2]) return ''; | |
return decodeURIComponent(results[2].replace(/\+/g, " ")); | |
}, | |
}; | |
module.exports = Helpers; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment