From 3b8b21f400e25a12d0aec7ba10cf7e76f9586ab5 Mon Sep 17 00:00:00 2001 From: Grzegorz Leoniec Date: Thu, 18 Oct 2018 14:13:21 +0200 Subject: [PATCH] added accounting package and number formatter --- package.json | 1 + plugins/vue-filters.js | 16 +++++++++++++++- yarn.lock | 4 ++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 8b738f630..b0b0d3e49 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "dependencies": { "@nuxtjs/apollo": "^4.0.0-rc3", "@nuxtjs/dotenv": "^1.3.0", + "accounting": "^0.4.1", "cross-env": "^5.2.0", "date-fns": "^1.29.0", "express": "^4.16.3", diff --git a/plugins/vue-filters.js b/plugins/vue-filters.js index 36aa0bf17..7f93ab098 100644 --- a/plugins/vue-filters.js +++ b/plugins/vue-filters.js @@ -3,6 +3,8 @@ import Vue from 'vue' import format from 'date-fns/format' import addSeconds from 'date-fns/add_seconds' +import accounting from 'accounting' + export default ({ app }) => { app.$filters = Object.assign(app.$filters || {}, { date: (value, fmt = 'DD. MMM YYYY') => { @@ -13,6 +15,18 @@ export default ({ app }) => { if (!value) return '' return format(new Date(value), fmt) }, + number: ( + value, + precision = 2, + thousands = '.', + decimals = ',', + fallback = null + ) => { + if (isNaN(value) && fallback) { + return fallback + } + return accounting.formatNumber(value || 0, precision, thousands, decimals) + }, // format seconds or milliseconds to durations HH:mm:ss duration: (value, unit = 's') => { if (unit === 'ms') { @@ -46,7 +60,7 @@ export default ({ app }) => { } return value.join(glue) }, - listByKey(values, key, glue, truncate) { + listByKey: (values, key, glue, truncate) => { return app.$filters.list(values.map(item => item[key]), glue, truncate) }, camelCase: (value = '') => { diff --git a/yarn.lock b/yarn.lock index 42c3fca1b..9696094bb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1024,6 +1024,10 @@ accepts@^1.3.5, accepts@~1.3.5: mime-types "~2.1.18" negotiator "0.6.1" +accounting@^0.4.1: + version "0.4.1" + resolved "https://registry.yarnpkg.com/accounting/-/accounting-0.4.1.tgz#87dd4103eff7f4460f1e186f5c677ed6cf566883" + acorn-dynamic-import@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/acorn-dynamic-import/-/acorn-dynamic-import-3.0.0.tgz#901ceee4c7faaef7e07ad2a47e890675da50a278"