diff --git a/README.md b/README.md index d48dcfa..b54c5c2 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,8 @@ Vue.use(VueCurrencyFilter, fractionCount: 2, fractionSeparator: ',', symbolPosition: 'front', - symbolSpacing: true + symbolSpacing: true, + avoidEmptyDecimals: undefined, }) ``` @@ -85,7 +86,8 @@ Vue.use(VueCurrencyFilter, [ fractionCount: 2, fractionSeparator: '.', symbolPosition: 'front', - symbolSpacing: true + symbolSpacing: true, + avoidEmptyDecimals: '', }, { // default name 'currency_2' name: 'currency_2', @@ -94,7 +96,8 @@ Vue.use(VueCurrencyFilter, [ fractionCount: 2, fractionSeparator: '.', symbolPosition: 'front', - symbolSpacing: false + symbolSpacing: false, + avoidEmptyDecimals: '--', } ]) ``` @@ -121,7 +124,8 @@ Add `vue-currency-filter/nuxt` to modules section of `nuxt.config.js` fractionCount: 2, fractionSeparator: '.', symbolPosition: 'front', - symbolSpacing: true + symbolSpacing: true, + avoidEmptyDecimals: undefined, }], // for multiple instance @@ -132,7 +136,8 @@ Add `vue-currency-filter/nuxt` to modules section of `nuxt.config.js` fractionCount: 2, fractionSeparator: '.', symbolPosition: 'front', - symbolSpacing: true + symbolSpacing: true, + avoidEmptyDecimals: '##', }, { // default name 'currency_2' name: 'currency_2', @@ -141,7 +146,8 @@ Add `vue-currency-filter/nuxt` to modules section of `nuxt.config.js` fractionCount: 2, fractionSeparator: '.', symbolPosition: 'front', - symbolSpacing: false + symbolSpacing: false, + avoidEmptyDecimals: '', } ]], ] @@ -160,7 +166,8 @@ or using external options fractionCount: 2, fractionSeparator: '.', symbolPosition: 'front', - symbolSpacing: true + symbolSpacing: true, + avoidEmptyDecimals: '', }, { // default name 'currency_2' name: 'currency_2', @@ -169,7 +176,8 @@ or using external options fractionCount: 2, fractionSeparator: '.', symbolPosition: 'front', - symbolSpacing: false + symbolSpacing: false, + avoidEmptyDecimals: '##', } ] // or for one filter @@ -179,7 +187,8 @@ or using external options fractionCount: 2, fractionSeparator: '.', symbolPosition: 'front', - symbolSpacing: true + symbolSpacing: true, + avoidEmptyDecimals: undefined, } } ``` @@ -225,7 +234,8 @@ if (VueCurrencyFilter) { fractionCount: 0, fractionSeparator: ".", symbolPosition: "front", - symbolSpacing: false + symbolSpacing: false, + avoidEmptyDecimals: '', }) } @@ -259,7 +269,8 @@ configFractionSeparator, configSymbolPosition, configSymbolSpacing)}} fractionCount: '', fractionSeparator: '', symbolPosition: '', - symbolSpacing: '' + symbolSpacing: '', + avoidEmptyDecimals: undefined, })}} ``` @@ -274,7 +285,8 @@ configFractionSeparator, configSymbolPosition, configSymbolSpacing)}} fractionCount: 'number (default : 0)', fractionSeparator: 'string (default: ",")', symbolPosition: 'string (default: front)', - symbolSpacing: 'boolean (default: true)' + symbolSpacing: 'boolean (default: true)', + avoidEmptyDecimals: 'string (default: undefined)', } ``` @@ -299,15 +311,50 @@ describe("test myComponent", () => { fractionCount: 2, fractionSeparator: ".", symbolPosition: "front", - symbolSpacing: true + symbolSpacing: true, + avoidEmptyDecimals: undefined, }); - const wrapper = shallowMount(Component, { + let wrapper = shallowMount(Component, { localVue }); const result = wrapper.find(".curr"); expect(result.text()).toEqual("$ 1,000.00"); + + localVue.use(VueCurrencyFilter, { + symbol: "$", + thousandsSeparator: ",", + fractionCount: 2, + fractionSeparator: ".", + symbolPosition: "front", + symbolSpacing: true, + avoidEmptyDecimals: '', + }); + + wrapper = shallowMount(Component, { + localVue + }); + + const result = wrapper.find(".curr"); + expect(result.text()).toEqual("$ 1,000"); + + localVue.use(VueCurrencyFilter, { + symbol: "$", + thousandsSeparator: ",", + fractionCount: 2, + fractionSeparator: ".", + symbolPosition: "front", + symbolSpacing: true, + avoidEmptyDecimals: '##', + }); + + wrapper = shallowMount(Component, { + localVue + }); + + const result = wrapper.find(".curr"); + expect(result.text()).toEqual("$ 1,000.##"); }); }); ``` diff --git a/packages/demo/src/views/Home.vue b/packages/demo/src/views/Home.vue index e86605d..5307576 100644 --- a/packages/demo/src/views/Home.vue +++ b/packages/demo/src/views/Home.vue @@ -192,6 +192,59 @@ +
+
+ +
+
+
+
+ + +
+
+
+
+ +
+
+ +
+
+
+
+ +
+
+
+
+
@@ -356,6 +409,8 @@ export default { configFractionSeparator: ',', configSymbolPosition: 'front', configSymbolSpacing: true, + configUseAvoidEmptyDecimals: false, + configAvoidEmptyDecimals: '', templateDownload: ` # NPM npm install vue-currency-filter diff --git a/packages/vue-currency-filter/src/accounting.ts b/packages/vue-currency-filter/src/accounting.ts index 13d5d6d..c06d8c1 100644 --- a/packages/vue-currency-filter/src/accounting.ts +++ b/packages/vue-currency-filter/src/accounting.ts @@ -124,12 +124,13 @@ export const formatNumber = function ( number, precision, thousand, - decimal + decimal, + avoidEmptyDecimals ) { // Resursively format arrays: if (__isArray(number)) { return __map(number, function (val) { - return formatNumber(val, precision, thousand, decimal) + return formatNumber(val, precision, thousand, decimal, avoidEmptyDecimals) }) } @@ -154,14 +155,26 @@ export const formatNumber = function ( var base = parseInt(toFixed(Math.abs(number || 0), usePrecision), 10) + '' var mod = base.length > 3 ? base.length % 3 : 0 + var precisionString = '' + if (usePrecision) { + // default behaviour + // 1234.56 and avoidEmptyDecimals whatever => 1234.56 + // 1234.00 and avoidEmptyDecimals undefined => 1234.00 + precisionString = opts.decimal + toFixed(Math.abs(number), usePrecision).split('.')[1] + + // 1234.00 and avoidEmptyDecimals == '' => 1234 + // 1234.00 and avoidEmptyDecimals == '##' => 1234.## + if (avoidEmptyDecimals !== undefined && parseInt(toFixed(Math.abs(number || 0), 1), 10) == number){ + precisionString = avoidEmptyDecimals === '' ? '' : opts.decimal + avoidEmptyDecimals + } + } + // Format the number: return ( negative + (mod ? base.substr(0, mod) + opts.thousand : '') + base.substr(mod).replace(/(\d{3})(?=\d)/g, '$1' + opts.thousand) + - (usePrecision - ? opts.decimal + toFixed(Math.abs(number), usePrecision).split('.')[1] - : '') + precisionString ) } @@ -182,12 +195,13 @@ export const formatMoney = function ( precision, thousand, decimal, - format + format, + avoidEmptyDecimals ) { // Resursively format arrays: if (__isArray(number)) { return __map(number, function (val) { - return formatMoney(val, symbol, precision, thousand, decimal, format) + return formatMoney(val, symbol, precision, thousand, decimal, format, avoidEmptyDecimals) }) } @@ -203,7 +217,8 @@ export const formatMoney = function ( precision: precision, thousand: thousand, decimal: decimal, - format: format + format: format, + avoidEmptyDecimals: avoidEmptyDecimals, }, lib.settings.currency ) @@ -222,7 +237,8 @@ export const formatMoney = function ( Math.abs(number), checkPrecision(opts.precision), opts.thousand, - opts.decimal + opts.decimal, + opts.avoidEmptyDecimals ) ) } diff --git a/packages/vue-currency-filter/src/index.ts b/packages/vue-currency-filter/src/index.ts index e1284fe..49b6e84 100644 --- a/packages/vue-currency-filter/src/index.ts +++ b/packages/vue-currency-filter/src/index.ts @@ -10,7 +10,8 @@ const defaultConfig: currencyOptions = { fractionCount: 0, fractionSeparator: ',', symbolPosition: 'front', - symbolSpacing: true + symbolSpacing: true, + avoidEmptyDecimals: undefined, } const VueCurrencyFilter: PluginObject = { @@ -26,7 +27,8 @@ const VueCurrencyFilter: PluginObject = { _fractionCount?: number, _fractionSeparator?: string, _symbolPosition?: string, - _symbolSpacing?: boolean): string | number { + _symbolSpacing?: boolean, + _avoidEmptyDecimals?: string): string | number { let runtimeConfig = __defaults({ symbol: _symbol, @@ -34,7 +36,8 @@ const VueCurrencyFilter: PluginObject = { fractionCount: _fractionCount, fractionSeparator: _fractionSeparator, symbolPosition: _symbolPosition, - symbolSpacing: _symbolSpacing + symbolSpacing: _symbolSpacing, + avoidEmptyDecimals: _avoidEmptyDecimals }, configs) if (typeof _symbol === 'object') { @@ -70,7 +73,8 @@ const VueCurrencyFilter: PluginObject = { symbol: runtimeConfig.symbol, precision: runtimeConfig.fractionCount, thousand: runtimeConfig.thousandsSeparator, - decimal: runtimeConfig.fractionSeparator + decimal: runtimeConfig.fractionSeparator, + avoidEmptyDecimals: runtimeConfig.avoidEmptyDecimals, }) if (isNegative) { diff --git a/packages/vue-currency-filter/src/types/index.ts b/packages/vue-currency-filter/src/types/index.ts index 5be44c0..9903287 100644 --- a/packages/vue-currency-filter/src/types/index.ts +++ b/packages/vue-currency-filter/src/types/index.ts @@ -5,7 +5,8 @@ export interface currencyOptions { fractionCount?: number, fractionSeparator?: string, symbolPosition?: string, - symbolSpacing?: boolean + symbolSpacing?: boolean, + avoidEmptyDecimals?: string, } export interface CurrencyFilterMethodInstance { @@ -17,5 +18,6 @@ export interface CurrencyFilterMethodInstance { _fractionCount?: number, _fractionSeparator?: string, _symbolPosition?: string, - _symbolSpacing?: boolean): string + _symbolSpacing?: boolean, + avoidEmptyDecimals?: string): string }