Lightweight vue currency filter based on accounting.js
https://mazipan.github.io/vue-currency-filter/
# NPM
npm install vue-currency-filter
# Yarn
yarn add vue-currency-filter
Step by step to using vue-currency-filter
:
import VueCurrencyFilter from 'vue-currency-filter'
Vue.use(VueCurrencyFilter)
Vue.use(VueCurrencyFilter,
{
symbol : '$',
thousandsSeparator: '.',
fractionCount: 2,
fractionSeparator: ',',
symbolPosition: 'front',
symbolSpacing: true
})
<span>{{ 20000 | currency}}</span>
Create file plugins/currency.js
, with code :
import VueCurrencyFilter from 'vue-currency-filter'
import Vue from 'vue'
Vue.use(VueCurrencyFilter, {
symbol: '$',
thousandsSeparator: ',',
fractionCount: 2,
fractionSeparator: '.',
symbolPosition: 'front',
symbolSpacing: true
})
Then update your nuxt.config.js, with code :
module.exports = {
plugins: [
{ src: '~/plugins/currency', ssr: true } // need to set ssr flag as true
]
}
See https://codesandbox.io/s/6xk1mv694n for Nuxt.js sample.
Add script dependencies
<!-- Vue Dependency -->
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<!-- Vue Currency Filter Dependency -->
<script src="https://unpkg.com/[email protected]/dist/vue-currency-filter.iife.js"></script>
<!-- Change 3.2.3 with latest version -->
Use filters in global
if (VueCurrencyFilter) {
Vue.use(VueCurrencyFilter, {
symbol: "£",
thousandsSeparator: ",",
fractionCount: 0,
fractionSeparator: ".",
symbolPosition: "front",
symbolSpacing: false
})
}
var app = new Vue({
el: '#app',
data: {
curr: 1000
}
});
See https://codepen.io/mazipan/pen/YdmNMy for code sample.
<span>
{{ textInput | currency(configSymbol, configSeparator, configFractionCount,
configFractionSeparator, configSymbolPosition, configSymbolSpacing)}}
</span>
Now configurations is also available as Object, thanks to sunhengzhe in PR #25:
<span>
{{ textInput | currency({
symbol: '',
thousandsSeparator: '',
fractionCount: '',
fractionSeparator: '',
symbolPosition: '',
symbolSpacing: ''
})}}
</span>
{
symbol: 'string (default : empty string)',
thousandsSeparator: 'string (default : .)',
fractionCount: 'number (default : 0)',
fractionSeparator: 'string (default: ",")',
symbolPosition: 'string (default: front)',
symbolSpacing: 'boolean (default: true)'
}
Since global config only can be setted from Vue.use(VueCurrencyFilter, configs)
, but sometimes we need to update this global configs on runtime process.
from v3.1.0 we intoduce prototype method that can be update this global configs. You can use anywhere in your components like this code below:
this.$CurrencyFilter.setConfig(newConfigs)
But please be aware, this method is only update global configs without trigger to re-run filter function. So maybe you will face not sync data from your configs and your view. You need to update some value to trigger this new configs applied.
Using @vue/test-utils
we can create test for any Vue Plugins, like:
/* eslint-env jest */
import { shallowMount, createLocalVue } from "@vue/test-utils";
import VueCurrencyFilter from "vue-currency-filter";
import Component from "../pages/myComponent.vue";
describe("test myComponent", () => {
it("vue-currency-filter should working correctly", () => {
let localVue = createLocalVue();
localVue.use(VueCurrencyFilter, {
symbol: "$",
thousandsSeparator: ",",
fractionCount: 2,
fractionSeparator: ".",
symbolPosition: "front",
symbolSpacing: true
});
let wrapper = shallowMount(Component, {
localVue
});
let result = wrapper.find(".curr");
expect(result.text()).toEqual("$ 1,000.00");
});
});
See sample test here: https://codesandbox.io/s/6xk1mv694n
If you'd like to contribute, head to the contributing guidelines. Inside you'll find directions for opening issues, coding standards, and notes on development.
- @iqbalhood: VueCurrencyFilter logo creator (see #19)
Copyright © 2017 Built with ❤️ by Irfan Maulana