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.
23 lines (22 loc) • 713 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.chunkWords = void 0;
/**
* Splits a string into fixed-size word chunks.
*
* Example: chunkWords("This is a test string", 2) → [["This", "is"], ["a", "test"], ["string"]]
*
* @author @dailker
* @param {string} str - The input string.
* @param {number} count - Number of words per chunk.
* @returns {string[][]} Array of word chunks.
*/
function chunkWords(str, count) {
const words = str.trim().split(/\s+/);
const result = [];
for (let i = 0; i < words.length; i += count) {
result.push(words.slice(i, i + count));
}
return result;
}
exports.chunkWords = chunkWords;