The last stats we can display are more "writer-oriented"--the lines, words, and characters count:
Let's create three new computed properties for each counter, with some Regular Expressions to get the job done:
computed: {
linesCount () {
if (this.selectedNote) {
// Count the number of new line characters
return this.selectedNote.content.split(/\r\n|\r|\n/).length
}
},
wordsCount () {
if (this.selectedNote) {
var s = this.selectedNote.content
// Turn new line cahracters into white-spaces
s = s.replace(/\n/g, ' ')
// Exclude start and end white-spaces
s = s.replace(/(^\s*)|(\s*$)/gi, '')
// Turn 2 or more duplicate white-spaces into 1
s = s.replace(/\s\s+/gi, ' ')
// Return the number of spaces
return s.split(' ').length
}
},
charactersCount () {
if (this.selectedNote) {
return this.selectedNote.content.split('').length
}
},
}
Here, we added some conditions to prevent the code from running if no note is currently selected. This will avoid crashes if you use the Vue devtools to inspect the app in this case, because it will try to compute all the properties.
You can now add three new stat span elements with the corresponding computed properties: