mirror of
https://github.com/sbrow/strings.git
synced 2025-12-29 23:17:39 -05:00
22 lines
621 B
JavaScript
22 lines
621 B
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.wordCount = exports.words = exports.wordsLazy = void 0;
|
|
const matchWord = /\S+/g;
|
|
function* wordsLazy(str, pattern = matchWord) {
|
|
for (const match of str.matchAll(pattern)) {
|
|
yield match[0];
|
|
}
|
|
}
|
|
exports.wordsLazy = wordsLazy;
|
|
function words(str, pattern = matchWord) {
|
|
return Array.from(wordsLazy(str, pattern));
|
|
}
|
|
exports.words = words;
|
|
function wordCount(str, pattern = matchWord) {
|
|
let count = 0;
|
|
for (const {} of str.matchAll(pattern))
|
|
count++;
|
|
return count;
|
|
}
|
|
exports.wordCount = wordCount;
|