Skip to content

Instantly share code, notes, and snippets.

@jremi
Last active March 17, 2019 06:26
Show Gist options
  • Save jremi/59de9f87f4b98a0ad028f38f49d8185c to your computer and use it in GitHub Desktop.
Save jremi/59de9f87f4b98a0ad028f38f49d8185c to your computer and use it in GitHub Desktop.
Vue.js - Global Custom Filters Loader Library
// [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