I've been building a simple tool to calculate the LIX value of a text (a standard measurement of the readability of a text).
My approach was to have every step of the calculation as seperate functions. In a larger and more complex tool the total words, sentences and others would be nice to have as independent functions.
Also the functions should be able to take in any string input and do their calculations. So that I would easily be able to use this in other contexts like TinyMCE editor and so forth.
So I've tried to find a way to make the code flexible. And my ambition was to write simple and pure functions.
I'm a little in doubt as to how readable and maintainable this approach is.
The click function triggering the calculation is an example. I'd really like your thoughts about this functions inside functions approach. I found it very nice to work in this way.
jQuery(document).ready(function() {
function getText() {
var text = jQuery('#inputLix').val();
return text;
}
function countWords(input) {
var wordCount;
var wordsArray;
var splitter = /\s|\n/g;
wordsArray = input
.replace(/(<([^>]+)>)/ig, "")
.replace(/,/g, "")
.replace(/-/g, "")
.replace(/"/g, "")
.replace(/'/g, "")
.split(splitter)
.filter(Boolean);
wordCount = wordsArray.length;
return wordCount;
}
function longWordCount(input) {
var longWordCounter = 0;
var splitter = /\s|\n/g;
var checkedText = input.replace(/<([^>]+)>|[\.\+\?,\(\)"'\-\!\;\:]/ig, "");
var wordsArray = checkedText
.split(splitter)
.filter(Boolean);
for (var i in wordsArray) {
if (wordsArray[i].charAt(0) > -1) {} else if (wordsArray[i].length > 6) {
longWordCounter++;
}
}
return longWordCounter;
}
function getSentences(input) {
var splitter = /\?|\!|\.|\n/g;
var checkedText = input
.replace(/(<([^>]+)>)/ig, "")
.replace(/,/g, "")
.replace(/-/g, "")
.replace(/"/g, "")
.replace(/'/g, "");
var arrayOfSentences = checkedText.split(splitter);
arrayOfSentences = arrayOfSentences.filter(Boolean);
return arrayOfSentences;
}
function countSentences(input) {
return input.length;
}
function calculateLix(wordCount, sentenceCount, longWordsCount) {
var lixScore = Math.round((wordCount / sentenceCount) + ((longWordsCount * 100) / wordCount));
return lixScore;
}
function outputContent(input, id) {
jQuery(id).html(input);
}
jQuery("#lixButton").click(function() {
var input = getText();
outputContent(calculateLix(countWords(input), countSentences(getSentences(input)), longWordCount(input)), "#notification");
});
});
.tool-wrapper {
background: #899398;
padding: 30px;
border-radius: 10px;
}
textarea {
width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="tool-wrapper">
<textarea id="inputLix" rows="15">This is just some basic text. Push the button to calculate the LIX value. And figure out how hard your text is to read.</textarea>
<div class="clearfix">
<button id="lixButton" class="alignright" type="submit">Calculate Lix</button>
<div id="notification" class="alignright">You haven't performed any calculations yet</div>
</div>
</div>