advanced-js-kit
Version:
Modern TypeScript utility library with tree-shaking support - Array, String, Number, Network, Sleep, and JWT utilities for JavaScript and TypeScript projects
51 lines (47 loc) • 1.58 kB
JavaScript
;
// src/universal/string/capitalize.ts
function capitalize(str) {
if (typeof str !== "string") {
throw new Error("Input must be a string");
}
if (str.length === 0) {
return str;
}
return str.charAt(0).toUpperCase() + str.slice(1);
}
function capitalizeWords(str) {
if (typeof str !== "string") {
throw new Error("Input must be a string");
}
return str.replace(/\b\w/g, (char) => char.toUpperCase());
}
function convertCamelToNormalCapitalized(camelCaseString) {
const words = camelCaseString.replace(/([a-z])([A-Z])/g, "$1 $2").split(/[\s_]+/);
const capitalizedWords = words.map(
(word) => word.charAt(0).toUpperCase() + word.slice(1)
);
const normalCapitalizedString = capitalizedWords.join(" ");
return normalCapitalizedString;
}
// src/universal/string/truncate.ts
function truncateText({
text,
maxLength = 10,
suffix = "..."
}) {
return text.length > maxLength ? text.substring(0, maxLength) + suffix : text;
}
// src/universal/string/index.ts
function randomStringWithFixedLength(length) {
if (!Number.isInteger(length) || length <= 0) {
throw new Error("Length must be a positive integer.");
}
return Array.from({ length }, () => Math.random().toString(36)[2]).join("");
}
exports.capitalize = capitalize;
exports.capitalizeWords = capitalizeWords;
exports.convertCamelToNormalCapitalized = convertCamelToNormalCapitalized;
exports.randomStringWithFixedLength = randomStringWithFixedLength;
exports.truncateText = truncateText;
//# sourceMappingURL=index.cjs.map
//# sourceMappingURL=index.cjs.map