From 24acfb501004e797b8cd05202bd3bc72bf612a2c Mon Sep 17 00:00:00 2001 From: Andrew Croce Date: Mon, 17 Sep 2018 15:08:45 -0400 Subject: [PATCH] Add type checking on formatDollars add param to hide cents --- js/lib/dollars.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/js/lib/dollars.js b/js/lib/dollars.js index 65fc69ff..6555c2f9 100644 --- a/js/lib/dollars.js +++ b/js/lib/dollars.js @@ -1,4 +1,11 @@ -export const formatDollars = value => `$${value.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,')}` +export const formatDollars = (value, cents = true) => { + if (typeof value === 'number') { + return cents + ? `$${value.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,')}` + : `$${value.toFixed(0).replace(/\d(?=(\d{3}))/g, '$&,')}` + } + return '' +} export const abbreviateDollars = (value, decimals = 1) => { if (value === null) { return null } // terminate early