11# vue-common-filters
2- Collection of common filters used in projects
2+ Collection of most used filter in projects.
3+
4+ # Contribution
5+ If you found a bug, want to request a feature or want to contribute, create <a href =" https://github.com/epicbg/vue-common-filters/issues " >issue</a > or submit <a href =" https://github.com/epicbg/vue-common-filters/pulls " >pull request</a >
36
47# Installation
58
@@ -14,7 +17,11 @@ Download repo
1417import Vue from ' vue'
1518import VueCommonFilters from ' vue-common-filters'
1619
17- Vue .use (VueCommonFilters, {
20+ // THESE ARE ALL OPTIONS YOU CAN CUSTOMIZE
21+ // YOU ARE NOT REQUIRED TO COPY ALL THESE OPTIONS
22+ // YOU CAN PASS EMPTY OR NO OPTIONS AT ALL
23+
24+ let config = {
1825 " currency" : {
1926 " symbol" : " $" ,
2027 " decimalDigits" : 2 ,
@@ -25,37 +32,245 @@ Vue.use(VueCommonFilters, {
2532 " text" : {
2633 " truncateClamp" : " ..."
2734 },
28-
35+
36+ " numbers" : {
37+ " decimalDigits" : 2
38+ },
39+
40+ " array" : {
41+ " implodeDelimiter" : " , "
42+ },
43+
2944 " dates" : {
30- " format" : " YYYY-MM-DD HH:mm:ss"
45+ " defaultFormat" : " YYYY-MM-DD HH:mm:ss" ,
46+ " filterConvertFormat" : " DD MMMM YYYY"
3147 }
32- })
48+ }
49+
50+ Vue .use (VueCommonFilters, config)
3351```
3452
35- # Useage
53+ # Usage
54+
55+ ## Text filters
3656
3757### truncate
38- Set string max length
39- ` {{ 'hello world' | truncate('2', '..') }} ` result ` hel... `
58+
59+ Arguments:
60+
61+ {Number} [ length]
62+ {string} [ clamp] - default: '...'
63+
64+ Example:
65+
66+ ``` javascript
67+ {{ string | truncate (3 ) }} // 'hello world' => 'hel...'
68+ ```
4069
4170### trim
42- Remove spaces from start and end of string
43- ` {{ ' hello world ' | trim }} ` result ` hello world `
4471
45- ### ats (array to string)
46- Convert array of strings to single string with delimiter
47- ` {{ ['h', 'e', 'l', 'o'] | ats(' ') }} ` result ` h e l l o `
72+ Example:
73+
74+ ``` javascript
75+ {{ string | trim }} // removes spaces from string
76+ ```
77+ ### capitalize
78+
79+ Example:
80+
81+ ``` javascript
82+ {{ string | capitalize }} // 'george' => 'George'
83+ ```
84+ ### uppercase / lowercase
85+
86+ Example:
87+
88+ ``` javascript
89+ {{ string | uppercase }} // 'hey' => 'HEY'
90+ ```
91+
92+ ``` javascript
93+ {{ string | lowercase }} // 'HEY' => 'hey'
94+ ```
95+
96+ ### placeholder
4897
49- ### aots (array of objects to string)
50- Reduce array of objects to string. First param key and second param delimiter
51- ` {{ [{name: 'Boby'}, {name: 'Jonny'}] | aots('name', ', ') }} ` result ` Boby, Jonny `
98+ Example:
99+
100+ ``` javascript
101+ {{ string | placeholder (' no data yet' ) }} // null || false => 'no data yet'
102+ ```
103+
104+ ## Array / object filters
105+
106+ ### implode
107+
108+ Arguments:
109+
110+ {string} [ delimiter] - default: ', '
111+
112+ Example:
113+
114+ ``` javascript
115+ {{ array | implode }} // ['Danny', 'Bobby', 'Mima'] => Danny, Boby, Mima
116+ {{ array | implode (' and' ) }} // ['Danny', 'Bobby', 'Mima'] => Danny and Boby and Mima
117+ ```
118+ ### implodeObjects
119+
120+ Arguments:
121+
122+ {string} [ key]
123+ {string} [ delimiter] - default: ', '
124+
125+ Example:
126+
127+ ``` javascript
128+ {{ users | implode (' name' ) }} // [{id: 1, name:'Danny'}, {id: 2, name:'Bobby'}] => Danny, Bobby
129+ {{ users | implode (' name' , ' and ' ) }} // [{id: 1, name:'Danny'}, {id: 2, name:'Bobby'}] => Danny and Bobby
130+ ```
52131
53132### search
54- Searches and filters array of object for matching keyword.
55- ` <div v-for"product in products | search('tshirt')"></div> `
133+ Searches all fields (inside array of objects) for a match with passed [ key]
134+
135+ Arguments:
136+
137+ {string} [ key]
138+
139+ Example:
140+
141+ ``` javascript
142+ {{ users | search (' Danny' ) | implodeObjects (' name' ) }}
143+ // [{name:'Danny Miller'}, {name:'Danny Grande'}, {name:'Bobby Miller'}]
144+ // => Danny Miller, Bobby Miller
145+ ```
146+
147+ ## Date filters
148+
149+ Examples are shown in default ` YYYY-MM-DD HH:mm:ss ` . If you have different format you can change it from the config as shown in installation section
150+
151+ This packages uses moment and if you want to change the locale you can do it like so:
152+
153+ ``` javascript
154+ moment .locale (' en' )
155+ ```
156+
157+ ### format
158+
159+ Arguments:
160+
161+ {string} [ format] - default: DD MMMM YYYY
162+
163+ Example:
164+
165+ ``` javascript
166+ {{ date | format (' DD MMMM' ) }} // '2018-02-01' => '1 February'
167+ {{ date | format }} // '2018-02-01' => '1 February 2019'
168+ ```
56169
57170### fromNow
58- ` {{ '1970-01-01 00:59:59' | fromNow }} ` result ` 40 years ago `
59171
60- ### format (format datetime)
61- ` {{ '1970-01-01 00:59:59' | format('d MMMM') }} ` result ` 1 january `
172+ ``` javascript
173+ {{ date | fromNow }} // '2019-02-01' => '8 months ago'
174+ ```
175+
176+ ### from
177+
178+ Arguments:
179+
180+ {string} [ from]
181+
182+ Example:
183+
184+ ``` javascript
185+ {{ date | from (' 2019-07-02' ) }} // '2019-07-01' => '1 year ago'
186+ ```
187+
188+ ### to
189+
190+ Arguments:
191+
192+ {string} [ to]
193+
194+ Example:
195+
196+ ``` javascript
197+ {{ date | to (' 2019-07-01' ) }} // '2019-06-01' => 'in 1 month'
198+ ```
199+
200+ ### add / substract
201+
202+ Arguments:
203+
204+ {number} [ number]
205+ {string} [ type]
206+
207+ Example:
208+
209+ ``` javascript
210+ {{ date | add (2 , ' days' ) }} // '2019-06-01' => '2019-06-03'
211+ {{ date | substract (2 , ' days' ) }} // '2019-06-03' => '2019-06-01'
212+ ```
213+
214+ ## Number filters
215+
216+ ### decimal
217+
218+ Arguments:
219+
220+ {Number} [ length] - default: 2
221+
222+ Example:
223+
224+ ``` javascript
225+ // '1' => '1.00'
226+ // '1.2' => '1.20'
227+ {{ coefficient | decimal }}
228+ ```
229+
230+ ### currency
231+
232+ Arguments:
233+
234+ {string} [ symbol] - default: $
235+ {Number} [ decimalDigits] - default: 2
236+ {Boolean} [ symbolOnLeft] - default: true
237+ {Number} [ spaceBetweenAmountAndSymbol] - default: true
238+
239+ Example:
240+
241+ ``` javascript
242+ {{ amount | currency }} // 12 => '$ 12.00'
243+ {{ amount | currency (' EUR' , 2 , false ) }} // 12 => 12.00 EUR
244+ ```
245+
246+ You can change the defaults from settings, as shown in installation section
247+
248+ # Programatic Usage
249+
250+ ## using functions
251+ ``` javascript
252+ this .$options .filters .filterName (value)
253+ ```
254+ ``` javascript
255+ this .$options .filters .currency (10 ) => ' $ 10.00'
256+ this .$options .filters .search ([{}, {}..])
257+ ```
258+
259+ ## changing settings
260+ ``` javascript
261+ import vueCommonFilters from ' vue-common-filters'
262+
263+ // Pass options which u want to customize, no need to pass the whole settings object
264+ vueCommonFilters .config = {
265+ currency: {
266+ symbol: ' EUR'
267+ }
268+ }
269+ ```
270+
271+
272+ # License
273+ MIT License
274+
275+ Copyright (c) 2019 EpicWeb
276+
0 commit comments