Last active
March 17, 2019 06:26
-
-
Save jremi/59de9f87f4b98a0ad028f38f49d8185c to your computer and use it in GitHub Desktop.
Vue.js - Global Custom Filters Loader Library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// [main.js] | |
import Vue from 'vue' | |
import filters from './customFilters' | |
// Load all custom filters into Vue.js | |
filters._helper.loadAllCustomFilters(Vue); | |
// Load specific filter "a la carte" | |
// Example: below loads "letterspace" only | |
// Vue.filter('letterspace', filters['letterspace']) | |
//------------------------------------------------------------------------- | |
// [customFilters.js] | |
const filters = { | |
uppercase: (value)=> value.toString().toUpperCase(), | |
lowercase: (value)=> value.toString().toLowerCase(), | |
letterspace: (value)=> { | |
let str_spaced = ''; | |
for(let z = 0; z<value.length; z++) | |
str_spaced += value[z] + ' ' | |
return str_spaced.trim() | |
}, | |
_helper: { | |
loadAllCustomFilters: (Vue)=>{ | |
for(let filter in filters){ | |
Vue.filter(filter, filters[filter]) | |
} | |
} | |
} | |
} | |
export default filters; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment