Skip to content

Instantly share code, notes, and snippets.

@gaurangrshah
Last active August 13, 2021 05:10
Show Gist options
  • Save gaurangrshah/e450fdbc6fb357be0624910562ce7082 to your computer and use it in GitHub Desktop.
Save gaurangrshah/e450fdbc6fb357be0624910562ce7082 to your computer and use it in GitHub Desktop.
Vanilla JS Utils
export function debounce(func, wait = 20, immediate = true) {
var timeout;
return function() {
var context = this,
args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
}
/*
usage: debounce(anyFunction)
*/
function formatCurrency(amount) {
return new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 2,
}).format(amount);
}
// const zeroCurrency = formatCurrency(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment