@catbee/utils
Version:
A modular, production-grade utility toolkit for Node.js and TypeScript, designed for robust, scalable applications (including Express-based services). All utilities are tree-shakable and can be imported independently.
156 lines • 5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.capitalize = capitalize;
exports.toKebabCase = toKebabCase;
exports.toCamelCase = toCamelCase;
exports.slugify = slugify;
exports.truncate = truncate;
exports.toPascalCase = toPascalCase;
exports.toSnakeCase = toSnakeCase;
exports.mask = mask;
exports.stripHtml = stripHtml;
exports.equalsIgnoreCase = equalsIgnoreCase;
exports.reverse = reverse;
exports.countOccurrences = countOccurrences;
/**
* Capitalizes the first character of a string.
*
* @param {string} str - The input string.
* @returns {string} The string with the first character in uppercase.
*/
function capitalize(str) {
return str.length === 0 ? "" : str.charAt(0).toUpperCase() + str.slice(1);
}
/**
* Converts a string to kebab-case (e.g., "FooBar test" → "foo-bar-test").
*
* @param {string} str - The input string.
* @returns {string} The kebab-cased string.
*/
function toKebabCase(str) {
return str
.replace(/([a-z])([A-Z])/g, "$1-$2") // camelCase -> camel-Case
.replace(/\s+/g, "-") // spaces to dash
.replace(/_+/g, "-") // underscores to dash
.toLowerCase();
}
/**
* Converts a kebab-case or snake_case string to camelCase.
*
* @param {string} str - The input string.
* @returns {string} The camelCased string.
*/
function toCamelCase(str) {
return str.replace(/[-_](.)/g, (_, c) => c.toUpperCase());
}
/**
* Converts a string to a URL-friendly slug (lowercase, dashes, alphanumeric).
*
* @param {string} str - The input string.
* @returns {string} The slugified string.
*/
function slugify(str) {
return str
.toLowerCase()
.replace(/[^\w\s-]/g, "") // remove non-word
.replace(/\s+/g, "-") // spaces to dash
.replace(/-+/g, "-") // multiple dashes to one
.replace(/^-+|-+$/g, ""); // trim leading/trailing dashes
}
/**
* Truncates a string to a specific length, appending '...' if truncated.
*
* @param {string} str - The input string.
* @param {number} len - The maximum length.
* @returns {string} The truncated string.
*/
function truncate(str, len) {
return str.length > len ? str.slice(0, len) + "..." : str;
}
/**
* Converts a string to PascalCase (e.g., "foo-bar" → "FooBar").
*
* @param {string} str - The input string.
* @returns {string} The PascalCased string.
*/
function toPascalCase(str) {
return capitalize(toCamelCase(str));
}
/**
* Converts a string to snake_case (e.g., "FooBar test" → "foo_bar_test").
*
* @param {string} str - The input string.
* @returns {string} The snake_cased string.
*/
function toSnakeCase(str) {
return str
.trim()
.replace(/([a-z])([A-Z])/g, "$1_$2") // camelCase -> camel_Case
.replace(/\s+/g, "_") // spaces to underscore
.replace(/-+/g, "_") // dashes to underscore
.toLowerCase();
}
/**
* Masks a string by replacing characters with a mask character.
* Useful for hiding sensitive information like credit cards or passwords.
*
* @param {string} str - The string to mask.
* @param {number} [visibleStart=0] - Number of characters to show at start.
* @param {number} [visibleEnd=0] - Number of characters to show at end.
* @param {string} [maskChar="*"] - Character to use for masking.
* @returns {string} The masked string.
*/
function mask(str, visibleStart = 0, visibleEnd = 0, maskChar = "*") {
if (!str || str.length === 0)
return "";
if (visibleStart >= str.length)
return str;
const start = str.slice(0, visibleStart);
const end = visibleEnd > 0 ? str.slice(-visibleEnd) : "";
const masked = maskChar.repeat(Math.max(0, str.length - visibleStart - visibleEnd));
return start + masked + end;
}
/**
* Removes all HTML tags from a string.
*
* @param {string} str - The HTML string to process.
* @returns {string} The string with HTML tags removed.
*/
function stripHtml(str) {
return str.replace(/<[^>]*>/g, "");
}
/**
* Performs case-insensitive string comparison.
*
* @param {string} a - First string.
* @param {string} b - Second string.
* @returns {boolean} True if the strings are equal ignoring case.
*/
function equalsIgnoreCase(a, b) {
return a.toLowerCase() === b.toLowerCase();
}
/**
* Reverses a string.
*
* @param {string} str - The string to reverse.
* @returns {string} The reversed string.
*/
function reverse(str) {
return str.split("").reverse().join("");
}
/**
* Counts occurrences of a substring within a string.
*
* @param {string} str - The source string.
* @param {string} substring - The substring to count.
* @param {boolean} [caseSensitive=true] - Whether to perform case-sensitive counting.
* @returns {number} Number of occurrences.
*/
function countOccurrences(str, substring, caseSensitive = true) {
if (!caseSensitive) {
str = str.toLowerCase();
substring = substring.toLowerCase();
}
return str.split(substring).length - 1;
}
//# sourceMappingURL=string.utils.js.map