stringzy
Version:
A versatile string manipulation library providing a range of text utilities for JavaScript and Node.js applications.
20 lines (19 loc) • 695 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.truncateText = truncateText;
function truncateText(text, maxLength, suffix = '...') {
if (typeof text !== 'string') {
throw new Error("Input text must be a string.");
}
if (typeof maxLength !== 'number' || maxLength < 0) {
throw new Error("maxLength must be a non-negative number.");
}
if (typeof suffix !== 'string') {
throw new Error("Suffix must be a string.");
}
if (text.length <= maxLength) {
return text;
}
const adjustedLength = maxLength - suffix.length;
return text.slice(0, adjustedLength > 0 ? adjustedLength : 0) + suffix;
}