Skip to content

Commit 4677bdd

Browse files
author
zhoraygevorgyan
committed
add more filters, improve readme
1 parent eef9862 commit 4677bdd

File tree

8 files changed

+272
-37
lines changed

8 files changed

+272
-37
lines changed

README.md

Lines changed: 236 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
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
1417
import Vue from 'vue'
1518
import 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+

config.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,16 @@
1010
"truncateClamp": "..."
1111
},
1212

13+
"numbers": {
14+
"decimalDigits": 2
15+
},
16+
17+
"array": {
18+
"implodeDelimiter": ", "
19+
},
20+
1321
"dates": {
14-
"format": "YYYY-MM-DD HH:mm:ss"
22+
"defaultFormat": "YYYY-MM-DD HH:mm:ss",
23+
"filterConvertFormat": "DD MMMM YYYY"
1524
}
1625
}

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import config from './config.json'
33
import {merge} from './src/helpers/helper.js'
44

55
export default {
6-
install(vue, settings) {
6+
install(vue, settings = {}) {
77

88
// overwrite config props to settings props
99
window.__vcf_settings = merge(config, settings)

src/filters.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { fromNow, format, to, from, add, substract } from './filters/datetime'
22
import { search, ats, aots } from './filters/array'
3-
import { truncate, trim, capitalize, uppercase, lowercase} from './filters/text'
3+
import { truncate, trim, capitalize, uppercase, lowercase, placeholder} from './filters/text'
44
import { currency, decimal } from './filters/numbers'
55

66
export default {
7+
78
// Date and time filters
89
"formatDate": format,
910
"fromNow": fromNow,
@@ -14,8 +15,8 @@ export default {
1415

1516
// Array filters
1617
"search": search,
17-
"ats": ats,
18-
"aots": aots,
18+
"implode": ats,
19+
"implodeObject": aots,
1920

2021
// Numbers
2122
"currency": currency,
@@ -26,6 +27,7 @@ export default {
2627
"trim": trim,
2728
"capitalize": capitalize,
2829
"uppercase": uppercase,
29-
"lowercast": lowercase
30+
"lowercast": lowercase,
31+
"placeholder": placeholder
3032

3133
}

src/filters/array.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ export function search(v, str) {
1111
return result
1212
}
1313

14-
export function ats(v, delimiter) {
14+
export function ats(v, delimiter = null) {
15+
16+
delimiter = delimiter || window.__vcf_settings.dates.implodeDelimiter
17+
1518
let result = ''
1619

1720
for (const str in v) {
@@ -26,7 +29,8 @@ export function ats(v, delimiter) {
2629
return result
2730
}
2831

29-
export function aots(v, key, delimiter) {
32+
export function aots(v, key, delimiter = null) {
33+
delimiter = delimiter || window.__vcf_settings.dates.implodeDelimiter
3034
let result = ''
3135
for (const i in v) {
3236
if(v[i][key]){

0 commit comments

Comments
 (0)