Skip to content

Commit 46dcc21

Browse files
author
zhoraygevorgyan
committed
filters
1 parent 8cc5992 commit 46dcc21

File tree

8 files changed

+120
-0
lines changed

8 files changed

+120
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import filters from 'src/filters'
2+
3+
export default {
4+
install(vue) {
5+
// Register filters globally
6+
for(filters in filters){
7+
vue.filter(filter, filters[filter]);
8+
}
9+
}
10+
};

package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "vue-common-filters",
3+
"version": "1.0.0",
4+
"description": "Collection of common filters used in projects",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [
10+
"vue",
11+
"filters",
12+
"collection",
13+
"date",
14+
"time",
15+
"currency",
16+
"filter",
17+
"vue",
18+
"plugin",
19+
"plugin"
20+
],
21+
"author": "Zhoray Gevorgyan",
22+
"license": "MIT",
23+
"dependencies": {
24+
"moment": "^2.24.0"
25+
}
26+
}

src/filters.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { fromNow, format } from './filters/datetime'
2+
import { search } from './filters/array'
3+
import { truncate, att, aott } from './filters/text'
4+
5+
export default {
6+
// Date and time filters
7+
"formatDate": format,
8+
"fromNow": fromNow,
9+
10+
// Array filters
11+
"search": search,
12+
13+
// Text filters
14+
"truncate": truncate,
15+
"att": att,
16+
"aott": aott,
17+
18+
}

src/filters/array.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
export default search = (v, str) => {
2+
let result = v.filter(obj => {
3+
4+
let values_including_string = Object.values(obj).filter(val => {
5+
return val.includes(str)
6+
})
7+
8+
return values_including_string.length > 0
9+
})
10+
11+
return result
12+
}
13+
14+
export default att = (v, delimiter) => {
15+
let result = ''
16+
17+
for (const str in arr) {
18+
if(str == arr.length - 1){
19+
result += arr[str]
20+
} else {
21+
result += arr[str] + delimiter
22+
}
23+
24+
}
25+
26+
return result
27+
}
28+
29+
export default aott = (v, key, delimiter) => {
30+
let result = ''
31+
for (const i in obj) {
32+
if(obj[i][key]){
33+
if(i == obj.length - 1)
34+
result += obj[i][key]
35+
else
36+
result += obj[i][key] + delimiter
37+
}
38+
}
39+
return result
40+
}

src/filters/datetime.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import moment from 'moment'
2+
3+
export const format = (v, format) => {
4+
moment(v).format(format)
5+
}
6+
7+
export const fromNow = (v) => {
8+
moment(v).fromNow()
9+
}

src/filters/text.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const truncate = (v, stop, clamp) => {
2+
return v.slice(0, stop) + (stop < v.length ? clamp || '...' : '')
3+
}

0 commit comments

Comments
 (0)