From f0ec0aac3a66f2e377ceefa23f44e185713940ec Mon Sep 17 00:00:00 2001 From: Robert Reinhard Date: Tue, 31 May 2022 16:00:29 -0700 Subject: [PATCH] Adding formatMoney helper and making it vue-i18n aware --- helpers/formatting.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/helpers/formatting.js b/helpers/formatting.js index 04e683a..1262286 100644 --- a/helpers/formatting.js +++ b/helpers/formatting.js @@ -17,3 +17,14 @@ export function getShopifyId(id) { const matches = id.match(/\/([^\/?]+)(?:\?.+)?$/) if (matches) return matches[1] } + +// Format a string or number like money, using vue-i18n if it's installed +// or falling back to USD +export function formatMoney(val) { + if (this && this.$n) return this.$n(val, 'currency') + return parseFloat(val) + .toLocaleString(process.env.LOCALE_CODE || 'en-US', { + style: 'currency', + currency: process.env.CURRENCY_CODE || 'USD' + }) +}