Text Statistics Counter
Counts, characters, words, sentences and paragraphs.
by plantface
JavaScript
var text = 'This is a paragraph. This is the 2nd sentence in the 1st paragraph.\nThis is another paragraph.';
var count = doCount(text);
document.body.innerHTML = '<pre>' + text + '</pre><hr>';
for (i in count) {
document.body.innerHTML += '<p>'+ i +': ' + count[i] + '</p>';
}
/* COUNTING LIBRARY */
/**
* Extracted from https://github.com/RadLikeWhoa/Countable/, which in
* turn uses `ucs2decode` function from the punycode.js library.
*/
function doCount(original) {
var trimmed = original.replace(/[\u200B]+/, '').trim();
return {
paragraphs: trimmed ? (trimmed.match(/\n+/g) || []).length + 1 : 0,
sentences: trimmed ? (trimmed.match(/[.?!…\n]+./g) || []).length + 1 : 0,
words: trimmed ? (trimmed.replace(/['";:,.?¿\-!¡]+/g, '').match(/\S+/g) || []).length : 0,
characters: trimmed ? _decode(trimmed.replace(/\s/g, '')).length : 0,
all: _decode(original).length
};
};
/**
* `ucs2decode` function from the punycode.js library.
*
* Creates an array containing the decimal code points of each Unicode
* character in the string. While JavaScript uses UCS-2 internally, this
* function will convert a pair of surrogate halves (each of which UCS-2
* exposes as separate characters) into a single code point, matching
* UTF-16.
*
* @see <http://goo.gl/8M09r>
* @see <http://goo.gl/u4UUC>
*
* @param {String} string The Unicode input string (UCS-2).
*
* @return {Array} The new array of code points.
*/
function _decode(string) {
var output = [],
counter = 0,
length = string.length,
value, extra;
while (counter < length) {
value = string.charCodeAt(counter++);
if (value >= 0xD800 && value <= 0xDBFF && counter < length) {
// High surrogate, and there is a next character.
extra = string.charCodeAt(counter++);
if ((extra & 0xFC00) === 0xDC00) {
// Low surrogate.
output.push(((value &...