* `count` trims an element's value, optionally strips HTML tags and counts * paragraphs, sentences, words, characters and characters plus spaces. * * @private * * @param {Node|String} target The target for the count. * * @param {Object} options The options to use f
(target, options)
| 116 | */ |
| 117 | |
| 118 | function count (target, options) { |
| 119 | let original = '' + (typeof target === 'string' ? target : ('value' in target ? target.value : target.textContent)) |
| 120 | options = options || {} |
| 121 | |
| 122 | /** |
| 123 | * The initial implementation to allow for HTML tags stripping was created |
| 124 | * @craniumslows while the current one was created by @Rob--W. |
| 125 | * |
| 126 | * @see <http://goo.gl/Exmlr> |
| 127 | * @see <http://goo.gl/gFQQh> |
| 128 | */ |
| 129 | |
| 130 | if (options.stripTags) original = original.replace(/<\/?[a-z][^>]*>/gi, '') |
| 131 | |
| 132 | if (options.ignore) { |
| 133 | each.call(options.ignore, function (i) { |
| 134 | original = original.replace(i, '') |
| 135 | }) |
| 136 | } |
| 137 | |
| 138 | const trimmed = original.trim() |
| 139 | |
| 140 | /** |
| 141 | * Most of the performance improvements are based on the works of @epmatsw. |
| 142 | * |
| 143 | * @see <http://goo.gl/SWOLB> |
| 144 | */ |
| 145 | |
| 146 | return { |
| 147 | paragraphs: trimmed ? (trimmed.match(options.hardReturns ? /\n{2,}/g : /\n+/g) || []).length + 1 : 0, |
| 148 | sentences: trimmed ? (trimmed.match(/[.?!…]+./g) || []).length + 1 : 0, |
| 149 | words: trimmed ? (trimmed.replace(/['";:,.?¿\-!¡]+/g, '').match(/\S+/g) || []).length : 0, |
| 150 | characters: trimmed ? decode(trimmed.replace(/\s/g, '')).length : 0, |
| 151 | all: decode(original).length |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * This is the main object that will later be exposed to other scripts. It |
no test coverage detected
searching dependent graphs…