We can create a component filter by putting our filter in filters in the component script.
Here the filter checks if all characters are capital. Should the string be empty, we will get a return value of false, as we never enter the for loop.
Each time we get a capital character, the retV variable is set as true.
For a character not a capital letter, we immediately return a false.
Thus should we complete the entire for loop it means all characters are capital.
This is the App.vue file:
<template>
<div>
<label>Enter string:</label>
<input type="text" v-model="val">
<h2>Is string all capital?</h2>
<h2>{{val|isAllCap}}</h2>
</div>
</template>
<script>
export default {
data() {
return {
val: ""
}
},
filters: {
isAllCap: function(v) {
let retV = false;
for (let i = 0; i < v.length; i++) {
let c = v.charAt(i);
if (c >= 'A' && c <= 'Z') retV = true;
else return false;
}
return retV;
}
}
}
</script>
<style>
h2 {
color: blue;
background-color: orange;
width: 400px;
margin: 50px auto;
text-align: center;
}
</style>
This is the output:
No comments:
Post a Comment