Skip to content

Instantly share code, notes, and snippets.

@emmanuelbarturen
Last active August 9, 2018 17:31
Show Gist options
  • Save emmanuelbarturen/7921f3e674d1ce95778ef66d1f33ea7f to your computer and use it in GitHub Desktop.
Save emmanuelbarturen/7921f3e674d1ce95778ef66d1f33ea7f to your computer and use it in GitHub Desktop.
Custom filters for Vuejs 2
// For add to vuejs
import {truncate,padZeros} from './customFilters';
Vue.filter('truncate', truncate);
Vue.filter('padZeros', padZeros);
/**
* Created by emmanuel <emmanuelbarturen@gmail.com> on 26/07/18.
* @return {string}
*/
const truncate = function (value, limit) {
if (value.length > limit) {
return value.substring(0, limit) + '...';
}
return value;
};
export {truncate};
const padZeros = function (value, size) {
let s = value + "";
while (s.length < size) s = "0" + s;
return s;
};
export {padZeros};
// Truncate
{{'associative'|truncate(3)}} // result: ass...
// padZeros
<select class="form-control">
<option v-for="month in 12" :value="month">{{month|padZeros(2)}}</option> // Result: 01,02,03,04,05,06,07,08,09,10,11,12
</select>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment