curslo-string-utilities
Version:
A simple utility library for string manipulation
39 lines (38 loc) • 1.21 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.capitalize = capitalize;
exports.toCamelCase = toCamelCase;
exports.toSnakeCase = toSnakeCase;
exports.toKebabCase = toKebabCase;
exports.reverseString = reverseString;
exports.truncateString = truncateString;
exports.isPalindrome = isPalindrome;
function capitalize(word) {
if (typeof word !== "string")
return "";
return word.charAt(0).toUpperCase() + word.slice(1);
}
function toCamelCase(str) {
return str
.toLowerCase()
.replace(/(?:^\w|[A-Z]|\b\w)/g, (match, index) => index === 0 ? match.toLowerCase() : match.toUpperCase())
.replace(/\s+/g, "");
}
function toSnakeCase(str) {
return str.toLowerCase().replace(/\s+/g, "_");
}
function toKebabCase(str) {
return str.toLowerCase().replace(/\s+/g, "-");
}
function reverseString(str) {
return str.split("").reverse().join("");
}
function truncateString(str, length) {
if (str.length <= length)
return str;
return str.slice(0, length) + "...";
}
function isPalindrome(str) {
const cleaned = str.toLowerCase().replace(/[^a-z0-9]/g, "");
return cleaned === cleaned.split("").reverse().join("");
}
;