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) 547 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.stringTruncateByWords = void 0; /** * Truncates a string without cutting off words midway. * @author @dailker * @param {string} str * @param {number} maxWords * @returns {string} */ function stringTruncateByWords(str, maxWords) { const words = str.trim().split(/\s+/); if (words.length <= maxWords) return str; return words.slice(0, maxWords).join(' ') + '...'; } exports.stringTruncateByWords = stringTruncateByWords;