gmx-word-counter
Version:
Fast, GMX-V compliant word and character counter. Can count both logographic and non-logographic languages correctly. Also supports generic counting for cases when language is unknown.
57 lines • 2.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.countCharacters = countCharacters;
const punctuationUtils_1 = require("./punctuationUtils");
const UnicodeAlphanumeric = /^[\p{L}\p{N}]*$/u;
// Does not need caching, as just computing it each time is actually faster
function isCpUnicodeAlphanumeric(codePoint) {
return UnicodeAlphanumeric.test(String.fromCodePoint(codePoint));
}
/**
*
* @param {string} text - text to count characters in
*/
function countCharacters(text) {
if (!text) {
return {
whiteSpace: 0,
characters: 0,
punctuation: 0,
};
}
const normalizedText = text.normalize('NFC');
let totalCharacters = 0;
let whiteSpace = 0;
let punctuation = 0;
for (var i = 0; i < normalizedText.length; i++) {
const cp = normalizedText.codePointAt(i);
// Check for surrogate pair and increment `i` if found
if (cp > 0xffff) {
i++;
}
// GMX TotalCharacterCount excludes whitespace.
if ((0, punctuationUtils_1.isWhitespaceCp)(cp)) {
whiteSpace++;
continue;
}
let isInWord = false;
if (i > 0 && i < normalizedText.length - 1) {
const prev = normalizedText.codePointAt(i - 1);
const next = normalizedText.codePointAt(i + 1);
isInWord = isCpUnicodeAlphanumeric(prev) && isCpUnicodeAlphanumeric(next);
}
// Punctuation characters are excluded, but hyphens and apostrophes are included
// if they appear inside of a word.
if ((0, punctuationUtils_1.isPunctuationCp)(cp) && !(isInWord && ((0, punctuationUtils_1.isHyphenCp)(cp) || (0, punctuationUtils_1.isApostropheCp)(cp)))) {
punctuation++;
continue;
}
totalCharacters++;
}
return {
characters: totalCharacters,
whiteSpace,
punctuation,
};
}
//# sourceMappingURL=characterCounter.js.map