@polgubau/utils
Version:
A collection of utility functions for TypeScript
62 lines • 1.73 kB
JavaScript
// src/texts/text-transform/text-transform.ts
function toCamelCase(str) {
return str.replace(/[-_](.)/g, (_, c) => c.toUpperCase());
}
function toTitleCase(str) {
return str.replace(/\w\S*/g, (txt) => txt.charAt(0).toUpperCase() + txt.slice(1).toLowerCase());
}
function toKebabCase(str) {
return str.replace(/([a-z])([A-Z])/g, "$1-$2").replace(/\s+/g, "-").toLowerCase();
}
function toUpperCase(str) {
return str.toUpperCase();
}
function toLowerCase(str) {
return str.toLowerCase();
}
function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
function removeWhitespace(str) {
return str.replace(/\s/g, "");
}
function reverseString(str) {
return str.split("").reverse().join("");
}
function truncateString(str, length = 50, suffix = "...") {
if (str.length === 0) {
return str;
}
return str.length > length ? str.substring(0, length - suffix.length) + suffix : str;
}
function formatString(str) {
const result = str.replace(/([a-z])([A-Z])/g, "$1 $2");
return capitalize(result.replace(/([A-Z])([A-Z][a-z])/g, "$1 $2").trim());
}
var randomString = (length = 4) => {
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
let result = "";
for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * characters.length);
result += characters.charAt(randomIndex);
}
return result;
};
var lowerAndNoSpace = (str) => {
return str.toLowerCase().replace(/\s/g, "");
};
export {
capitalize,
formatString,
lowerAndNoSpace,
randomString,
removeWhitespace,
reverseString,
toCamelCase,
toKebabCase,
toLowerCase,
toTitleCase,
toUpperCase,
truncateString
};
//# sourceMappingURL=text-transform.mjs.map