@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) • 743 B
JavaScript
;
var _require = require('./utils'),
ensureString = _require.ensureString;
var _require2 = require('./constants'),
EMPTY_SPACES_REGEX = _require2.EMPTY_SPACES_REGEX;
/**
* Finds the shortest word in a string.
*
* @function
* @param {string} input - The string to analyze.
* @returns {string} Shortest word found.
*
* @throws {TypeError} Throws an error if the input is not a string.
*
* @example
* const result = shortestWord('this is a multiple word string');
* console.log(result); // 'a'
*/
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;
});
});