stringzy
Version:
A versatile string manipulation library providing a range of text utilities for JavaScript and Node.js applications.
20 lines (19 loc) • 780 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.initials = initials;
function initials(text, limit) {
if (typeof text !== "string") {
throw new Error("Input must be a string");
}
if (limit !== undefined && (typeof limit !== "number" || isNaN(limit))) {
throw new Error("Limit must be a valid number");
}
if (limit !== undefined && limit < 0) {
throw new Error("Limit must be a non-negative number");
}
const words = text.trim().split(/\s+/).filter((word) => word.length > 0);
if (words.length === 0)
return "";
const initialsArray = words.map((word) => word.charAt(0)).slice(0, limit !== null && limit !== void 0 ? limit : words.length);
return initialsArray.join("");
}