@simongrasinovski/str-utils
Version:
A lightweight utility library for effortless string manipulation. Simplify common tasks like case validation, conversion and formatting with easy-to-use functions that enhance your JavaScript applications
26 lines (24 loc) • 751 B
JavaScript
;
var _require = require('./utils'),
ensureString = _require.ensureString;
var _require2 = require('./constants'),
EMPTY_SPACES_REGEX = _require2.EMPTY_SPACES_REGEX;
/**
* Finds the longest word in a string.
*
* @function
* @param {string} input - The string to analyze.
* @returns {string} Longest word found.
*
* @throws {TypeError} Throws an error if the input is not a string.
*
* @example
* const result = longestWord('this is a multiple word string');
* console.log(result); // 'multiple'
*/
module.exports = ensureString(function (input) {
var words = input.trim().split(EMPTY_SPACES_REGEX);
return words.reduce(function (acc, curr) {
return curr.length > acc.length ? curr : acc;
}, '');
});