word-counting
Version:
A very powerful words counter that supports plain text and html.
33 lines (29 loc) • 1.15 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = global || self, global.wordCounting = factory());
}(this, function () { 'use strict';
var htmlToText = require('html-to-text');
var regex = require('word-regex');
/**
* The function that can count the words occurrence in text
* @param text The string that you want to count words
* @param config The config settings for the wordsCounter function
* @returns an object that has the words occurrence information
*/
var wordsCounter = function (text, config) {
var result = {
wordsCount: 0
};
var plainText = config && config.isHtml
? htmlToText.fromString(text, { ignoreHref: true, ignoreImage: true })
: text;
if (plainText.length > 0) {
var match = plainText.match(regex());
result.wordsCount = match ? match.length : 0;
}
return result;
};
return wordsCounter;
}));
//# sourceMappingURL=word-counting.umd.js.map