Last active
August 9, 2018 17:31
-
-
Save emmanuelbarturen/7921f3e674d1ce95778ef66d1f33ea7f to your computer and use it in GitHub Desktop.
Custom filters for Vuejs 2
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
// For add to vuejs | |
import {truncate,padZeros} from './customFilters'; | |
Vue.filter('truncate', truncate); | |
Vue.filter('padZeros', padZeros); |
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
/** | |
* 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}; |
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
// 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