generator-martinlabs
Version:
Generates a fullstack CRUD using Java and Vue.js, check the features list
57 lines (54 loc) • 1.67 kB
JavaScript
export default {
install(Vue) {
const filter = {
truncate(string, value) {
if (string && string.length > value) {
return `${string.substring(0, value)}...`;
}
return string;
},
removeDelimiters(string) {
if (!string) return string;
return string.replace(/[. ,:\-/]+/g, '');
},
rg(string) {
if (!string) return string;
let v = string.replace(/\D/g, '');
v = v.replace(/(\d{2})(\d{3})(\d{3})(\d{1})$/, '$1.$2.$3-$4');
return v;
},
cpf(string) {
if (!string) return string;
let v = string.replace(/\D/g, '');
v = v.replace(/(\d{3})(\d{3})(\d{3})(\d{2})$/, '$1.$2.$3-$4');
return v;
},
cnpj(string) {
if (!string) return string;
let v = string.replace(/\D/g, '');
v = v.replace(/(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})$/, '$1.$2.$3/$4-$5');
return v;
},
phone(string) {
if (!string) return string;
let v = string.replace(/\D/g, '');
v = v.replace(/(\d{2})(\d{5})(\d{4})$/, '($1) $2-$3');
return v;
},
cep(string) {
if (!string) return string;
let v = string.replace(/\D/g, '');
v = v.replace(/(\d{5})(\d{3})$/, '$1-$2');
return v;
},
};
Vue.filter('truncate', filter.truncate);
Vue.filter('rg', filter.rg);
Vue.filter('cpf', filter.cpf);
Vue.filter('cnpj', filter.cnpj);
Vue.filter('phone', filter.phone);
Vue.filter('cep', filter.cep);
Object.assign(Vue.prototype, { $filters: filter });
Object.assign(Vue, { $filters: filter });
},
};