1 | # 🍒 Vue Currency Filter
|
2 |
|
3 | <p align="center"><a href="https://mazipan.github.io/vue-currency-filter/" target="_blank" rel="noopener noreferrer"><img width="128" src="https://raw.githubusercontent.com/mazipan/vue-currency-filter/master/assets/VueJS-Currency-128px.png" alt="Vue Currency Logo"></a></p>
|
4 |
|
5 | <p align="center">
|
6 | <a href="https://www.npmjs.com/package/vue-currency-filter">
|
7 | <img src="https://img.shields.io/npm/v/vue-currency-filter.svg" alt="NPM Version">
|
8 | </a>
|
9 | <a href="https://bundlephobia.com/result?p=vue-currency-filter">
|
10 | <img src="https://badgen.net/bundlephobia/minzip/vue-currency-filter" alt="Bundlephobia Size">
|
11 | </a>
|
12 | <a href="https://www.npmjs.com/package/vue-currency-filter">
|
13 | <img src="https://img.shields.io/npm/dt/vue-currency-filter.svg" alt="Download All Time">
|
14 | </a>
|
15 | <a href="https://travis-ci.org/mazipan/vue-currency-filter">
|
16 | <img src="https://img.shields.io/travis/mazipan/vue-currency-filter.svg" alt="Travis Build">
|
17 | </a>
|
18 | <a href="https://codecov.io/gh/mazipan/vue-currency-filter">
|
19 | <img src="https://codecov.io/gh/mazipan/vue-currency-filter/branch/master/graph/badge.svg" alt="Code Coverage">
|
20 | </a>
|
21 | <a href="#contributors">
|
22 | <img src="https://img.shields.io/badge/all_contributors-4-orange.svg" alt="All Contributors">
|
23 | </a>
|
24 | <a href="https://greenkeeper.io/">
|
25 | <img src="https://badges.greenkeeper.io/mazipan/vue-currency-filter.svg" alt="Greenkeeper">
|
26 | </a>
|
27 | </p>
|
28 |
|
29 | > Lightweight vue currency filter based on accounting.js
|
30 |
|
31 | ## Demo
|
32 |
|
33 | [https://mazipan.github.io/vue-currency-filter/](https://mazipan.github.io/vue-currency-filter/)
|
34 |
|
35 | ## Download
|
36 |
|
37 | ```bash
|
38 | # NPM
|
39 | npm install vue-currency-filter
|
40 |
|
41 | # Yarn
|
42 | yarn add vue-currency-filter
|
43 | ```
|
44 |
|
45 | ## Sample Usage
|
46 |
|
47 | Step by step to using `vue-currency-filter`:
|
48 |
|
49 | ### Import in `main.js`
|
50 |
|
51 | ```javascript
|
52 | import VueCurrencyFilter from 'vue-currency-filter'
|
53 | ```
|
54 |
|
55 | ### Use Plugins
|
56 |
|
57 | ```javascript
|
58 | Vue.use(VueCurrencyFilter)
|
59 | ```
|
60 |
|
61 | ### Add Global Configuration
|
62 |
|
63 | ```javascript
|
64 | Vue.use(VueCurrencyFilter,
|
65 | {
|
66 | symbol : '$',
|
67 | thousandsSeparator: '.',
|
68 | fractionCount: 2,
|
69 | fractionSeparator: ',',
|
70 | symbolPosition: 'front',
|
71 | symbolSpacing: true
|
72 | })
|
73 | ```
|
74 |
|
75 | ### Use in View
|
76 |
|
77 | ```html
|
78 | <span>{{ 20000 | currency}}</span>
|
79 | ```
|
80 |
|
81 | ### Usage in Nuxt.js
|
82 |
|
83 | Add `vue-currency-filter/nuxt` to modules section of `nuxt.config.js`
|
84 |
|
85 | ```js
|
86 | {
|
87 | modules: [
|
88 | 'vue-currency-filter/nuxt',
|
89 |
|
90 | // Or if you have custom options...
|
91 | ['vue-currency-filter/nuxt', {
|
92 | symbol: '$',
|
93 | thousandsSeparator: ',',
|
94 | fractionCount: 2,
|
95 | fractionSeparator: '.',
|
96 | symbolPosition: 'front',
|
97 | symbolSpacing: true
|
98 | }],
|
99 | ]
|
100 | }
|
101 | ```
|
102 |
|
103 | ### Usage without NPM
|
104 |
|
105 | Add script dependencies
|
106 |
|
107 | ```html
|
108 | <!-- Vue Dependency -->
|
109 | <script src="https://cdn.jsdelivr.net/npm/vue"></script>
|
110 |
|
111 | <!-- Vue Currency Filter Dependency -->
|
112 | <script src="https://unpkg.com/vue-currency-filter@3.2.3/dist/vue-currency-filter.iife.js"></script>
|
113 | <!-- Change 3.2.3 with latest version -->
|
114 | ```
|
115 |
|
116 | Use filters in global
|
117 |
|
118 | ```js
|
119 | if (VueCurrencyFilter) {
|
120 | Vue.use(VueCurrencyFilter, {
|
121 | symbol: "£",
|
122 | thousandsSeparator: ",",
|
123 | fractionCount: 0,
|
124 | fractionSeparator: ".",
|
125 | symbolPosition: "front",
|
126 | symbolSpacing: false
|
127 | })
|
128 | }
|
129 |
|
130 | var app = new Vue({
|
131 | el: '#app',
|
132 | data: {
|
133 | curr: 1000
|
134 | }
|
135 | });
|
136 |
|
137 | ```
|
138 |
|
139 | See [https://codepen.io/mazipan/pen/YdmNMy](https://codepen.io/mazipan/pen/YdmNMy) for code sample.
|
140 |
|
141 | ### Add Configuration In Specific Place
|
142 |
|
143 | ```html
|
144 | <span>
|
145 | {{ textInput | currency(configSymbol, configSeparator, configFractionCount,
|
146 | configFractionSeparator, configSymbolPosition, configSymbolSpacing)}}
|
147 | </span>
|
148 | ```
|
149 |
|
150 | **Now configurations is also available as Object**, thanks to [sunhengzhe](https://github.com/sunhengzhe) in [PR #25](https://github.com/mazipan/vue-currency-filter/pull/25/commits/052a741644556f4d1140e7b7e1ba96a8e2c0d015):
|
151 |
|
152 | ```html
|
153 | <span>
|
154 | {{ textInput | currency({
|
155 | symbol: '',
|
156 | thousandsSeparator: '',
|
157 | fractionCount: '',
|
158 | fractionSeparator: '',
|
159 | symbolPosition: '',
|
160 | symbolSpacing: ''
|
161 | })}}
|
162 | </span>
|
163 | ```
|
164 |
|
165 | ## Available Options
|
166 |
|
167 | ```javascript
|
168 | {
|
169 | name: 'string (default: currency)', // using for multiple instance filters
|
170 | symbol: 'string (default : empty string)',
|
171 | thousandsSeparator: 'string (default : .)',
|
172 | fractionCount: 'number (default : 0)',
|
173 | fractionSeparator: 'string (default: ",")',
|
174 | symbolPosition: 'string (default: front)',
|
175 | symbolSpacing: 'boolean (default: true)'
|
176 | }
|
177 | ```
|
178 |
|
179 | ## Update Global Configs
|
180 |
|
181 | Since global config only can be setted from `Vue.use(VueCurrencyFilter, configs)`, but sometimes we need to update this global configs on runtime process.
|
182 |
|
183 | 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:
|
184 |
|
185 | ```js
|
186 | this.$CurrencyFilter.setConfig(newConfigs)
|
187 | ```
|
188 |
|
189 | 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.
|
190 |
|
191 | ## How to test in Unit Test
|
192 |
|
193 | Using `@vue/test-utils` we can create test for any Vue Plugins, like:
|
194 |
|
195 | ```js
|
196 | /* eslint-env jest */
|
197 | import { shallowMount, createLocalVue } from "@vue/test-utils";
|
198 | import VueCurrencyFilter from "vue-currency-filter";
|
199 |
|
200 | import Component from "../pages/myComponent.vue";
|
201 |
|
202 | describe("test myComponent", () => {
|
203 | it("vue-currency-filter should working correctly", () => {
|
204 | let localVue = createLocalVue();
|
205 | localVue.use(VueCurrencyFilter, {
|
206 | symbol: "$",
|
207 | thousandsSeparator: ",",
|
208 | fractionCount: 2,
|
209 | fractionSeparator: ".",
|
210 | symbolPosition: "front",
|
211 | symbolSpacing: true
|
212 | });
|
213 |
|
214 | let wrapper = shallowMount(Component, {
|
215 | localVue
|
216 | });
|
217 |
|
218 | let result = wrapper.find(".curr");
|
219 | expect(result.text()).toEqual("$ 1,000.00");
|
220 | });
|
221 | });
|
222 | ```
|
223 |
|
224 | See sample test here: [https://codesandbox.io/s/6xk1mv694n](https://codesandbox.io/s/6xk1mv694n)
|
225 |
|
226 | ## Contributing
|
227 |
|
228 | If you'd like to contribute, head to the [contributing guidelines](/CONTRIBUTING.md). Inside you'll find directions for opening issues, coding standards, and notes on development.
|
229 |
|
230 | ## Credit
|
231 |
|
232 | - [@iqbalhood](https://github.com/iqbalhood) as logo creator (see [#19](https://github.com/mazipan/vue-currency-filter/issues/19))
|
233 | - [Jetbrain](https://www.jetbrains.com/?from=vue-currency-filter) for amazing WebStorm IDE
|
234 |
|
235 | [<img src="https://raw.githubusercontent.com/mazipan/vue-currency-filter/master/jetbrains.png" width="100px;" />](https://www.jetbrains.com/?from=vue-currency-filter)
|
236 |
|
237 | ## Hope this will be useful for you all
|
238 |
|
239 | Copyright © 2017 Built with ❤️ by Irfan Maulana
|
240 |
|
241 | ## Contributors
|
242 |
|
243 | Thanks goes to these wonderful people ([emoji key](https://github.com/all-contributors/all-contributors#emoji-key)):
|
244 |
|
245 |
|
246 |
|
247 | <table><tr><td align="center"><a href="https://www.mazipan.xyz/"><img src="https://avatars0.githubusercontent.com/u/7221389?v=4" width="100px;" alt="Irfan Maulana"/><br /><sub><b>Irfan Maulana</b></sub></a><br /><a href="https://github.com/mazipan/vue-currency-filter/commits?author=mazipan" title="Code">💻</a></td><td align="center"><a href="https://about.me/iqbalhood"><img src="https://avatars3.githubusercontent.com/u/1563756?v=4" width="100px;" alt="iqbalhood"/><br /><sub><b>iqbalhood</b></sub></a><br /><a href="#design-iqbalhood" title="Design">🎨</a></td><td align="center"><a href="https://sunhengzhe.com"><img src="https://avatars3.githubusercontent.com/u/8614151?v=4" width="100px;" alt="孙恒哲"/><br /><sub><b>孙恒哲</b></sub></a><br /><a href="https://github.com/mazipan/vue-currency-filter/commits?author=sunhengzhe" title="Code">💻</a></td><td align="center"><a href="https://github.com/ricardogobbosouza"><img src="https://avatars3.githubusercontent.com/u/13064722?v=4" width="100px;" alt="Ricardo Gobbo de Souza"/><br /><sub><b>Ricardo Gobbo de Souza</b></sub></a><br /><a href="https://github.com/mazipan/vue-currency-filter/commits?author=ricardogobbosouza" title="Code">💻</a></td><td align="center"><a href="https://github.com/dsfx3d"><img src="https://avatars1.githubusercontent.com/u/14162837?v=4" width="100px;" alt="Yashodhan Singh Rathore"/><br /><sub><b>Yashodhan Singh Rathore</b></sub></a><br /><a href="https://github.com/mazipan/vue-currency-filter/commits?author=dsfx3d" title="Code">💻</a></td><td align="center"><a href="http://gijsroge.github.io"><img src="https://avatars0.githubusercontent.com/u/2242498?v=4" width="100px;" alt="Gijs Rogé"/><br /><sub><b>Gijs Rogé</b></sub></a><br /><a href="https://github.com/mazipan/vue-currency-filter/commits?author=gijsroge" title="Code">💻</a></td></tr></table>
|
248 |
|
249 |
|
250 |
|
251 | This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
|