@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
24 lines (22 loc) • 903 B
JavaScript
;
var _require = require('./utils'),
ensureString = _require.ensureString;
/**
* Truncates a string to a specified length, appending a suffix if necessary.
*
* @function
* @param {string} input - The string to truncate.
* @param {number} length - The maximum length for the truncated string.
* @param {string} [suffix='...'] - The string to append to the truncated string.
* @returns {string} Exctacted substring.
*
* @throws {TypeError} Throws an error if the input is not a string.
*
* @example
* const result = truncate('this is some very long string string that will be truncated', 20);
* console.log(result); // 'this is some very lo...'
*/
module.exports = ensureString(function (input, length) {
var suffix = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '...';
return "".concat(input.substring(0, length)).concat(suffix);
});