UNPKG

everyutil

Version:

A comprehensive library of lightweight, reusable utility functions for JavaScript and TypeScript, designed to streamline common programming tasks such as string manipulation, array processing, date handling, and more.

18 lines (17 loc) 551 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.countWords = void 0; /** * Counts words in a string (by whitespace or punctuation). * @author @dailker * @param {string} str * @param {'whitespace'|'punctuation'} [mode='whitespace'] * @returns {number} */ function countWords(str, mode = 'whitespace') { if (mode === 'punctuation') { return (str.match(/\b\w+\b/g) || []).length; } return (str.trim().split(/\s+/).filter(Boolean)).length; } exports.countWords = countWords;