Skip to content

Commit eef9862

Browse files
author
zhoraygevorgyan
committed
add 5 more filters
1 parent 2999e00 commit eef9862

File tree

8 files changed

+126
-9
lines changed

8 files changed

+126
-9
lines changed

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,25 @@ Download repo
1414
import Vue from 'vue'
1515
import VueCommonFilters from 'vue-common-filters'
1616

17-
Vue.use(VueCommonFilters)
17+
Vue.use(VueCommonFilters, {
18+
"currency": {
19+
"symbol": "$",
20+
"decimalDigits": 2,
21+
"symbolOnLeft": true,
22+
"spaceBetweenAmountAndSymbol": false
23+
},
24+
25+
"text": {
26+
"truncateClamp": "..."
27+
},
28+
29+
"dates": {
30+
"format": "YYYY-MM-DD HH:mm:ss"
31+
}
32+
})
1833
```
1934

20-
# Filters and usage
35+
# Useage
2136

2237
### truncate
2338
Set string max length

config.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"currency": {
3+
"symbol": "$",
4+
"decimalDigits": 2,
5+
"symbolOnLeft": true,
6+
"spaceBetweenAmountAndSymbol": false
7+
},
8+
9+
"text": {
10+
"truncateClamp": "..."
11+
},
12+
13+
"dates": {
14+
"format": "YYYY-MM-DD HH:mm:ss"
15+
}
16+
}

index.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
import filters from './src/filters'
2+
import config from './config.json'
3+
import {merge} from './src/helpers/helper.js'
24

35
export default {
4-
install(vue) {
6+
install(vue, settings) {
7+
8+
// overwrite config props to settings props
9+
window.__vcf_settings = merge(config, settings)
10+
511
// Register filters globally
612
for(let filter in filters){
713
vue.filter(filter, filters[filter]);
814
}
15+
},
16+
17+
get config(){
18+
return window.__vcf_settings
19+
},
20+
21+
set config(settings){
22+
window.__vcf_settings = merge(window.__vcf_settings, settings)
923
}
1024
};

src/filters.js

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

56
export default {
67
// Date and time filters
78
"formatDate": format,
89
"fromNow": fromNow,
10+
"to": to,
11+
"from": from,
12+
"add": add,
13+
"substract": substract,
914

1015
// Array filters
1116
"search": search,
1217
"ats": ats,
1318
"aots": aots,
1419

20+
// Numbers
21+
"currency": currency,
22+
"decimal": decimal,
23+
1524
// Text filters
1625
"truncate": truncate,
17-
"trim": trim
26+
"trim": trim,
27+
"capitalize": capitalize,
28+
"uppercase": uppercase,
29+
"lowercast": lowercase
1830

1931
}

src/filters/datetime.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
11
import moment from 'moment'
22

33
export const format = (v, format) => {
4-
moment(v).format(format)
4+
moment(v, window.__vcf_settings.dates.format).format(format)
55
}
66

77
export const fromNow = (v) => {
8-
moment(v).fromNow()
8+
moment(v, window.__vcf_settings.dates.format).fromNow()
9+
}
10+
11+
12+
export const from = (v, from) => {
13+
moment(v, window.__vcf_settings.dates.format).from(from)
14+
}
15+
16+
export const to = (v, to) => {
17+
moment(v, window.__vcf_settings.dates.format).to(to)
18+
}
19+
20+
21+
export const add = (v, number, type) => {
22+
moment(v, window.__vcf_settings.dates.format).add(number, type)
23+
}
24+
25+
export const substract = (v, number, type) => {
26+
moment(v, window.__vcf_settings.dates.format).substract(number, type)
927
}

src/filters/numbers.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export const currency = (v, symbol = null, decimalDigits = null, symbolOnLeft = null, spaceBetweenAmountAndSymbol = null) => {
2+
let settings = {...window.__vcf_settings.currency}
3+
4+
if(symbol)
5+
settings.symbol = symbol
6+
if(decimalDigits)
7+
settings.decimalDigits = decimalDigits
8+
if(symbolOnLeft)
9+
settings.symbolOnLeft = symbolOnLeft
10+
11+
let space = spaceBetweenAmountAndSymbol || settings.spaceBetweenAmountAndSymbol ? ' ' : ''
12+
let amount = Number(v).toFixed(settings.decimalDigits)
13+
14+
if(symbolOnLeft){
15+
return `${symbol}${space}${amount}`
16+
} else {
17+
return `${amount}${space}${symbol}`
18+
}
19+
}
20+
21+
export const decimal = (v, digits = null) => {
22+
let length = digits || window.__vcf_settings.numbers.decimalDigits
23+
return Number(v).toFixed(length)
24+
}

src/filters/text.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
export const truncate = (v, stop, clamp) => {
2-
return v.slice(0, stop) + (stop < v.length ? clamp || '...' : '')
2+
return v.slice(0, stop) + (stop < v.length ? clamp || window.__vcf_settings.text.truncateClamp : '')
33
}
44

55
export const trim = (v) => {
66
return v.trim()
7+
}
8+
9+
export const capitalize = (v) => {
10+
return v[0].toUpperCase() + v.slice(1);
11+
}
12+
13+
export const uppercase = (v) => {
14+
return v.toUpperCase();
15+
}
16+
17+
export const lowercase = (v) => {
18+
return v.toLowerCase();
719
}

src/helpers/helper.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export function merge(a, b){
2+
for(var idx in b) {
3+
a[idx] = b[idx];
4+
}
5+
return a
6+
}

0 commit comments

Comments
 (0)