UNPKG

@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.

143 lines (138 loc) 5.41 kB
/* * The MIT License * * Copyright (c) 2026 Catbee Technologies. https://catbee.in/license * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ 'use strict'; var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); // src/string/string.utils.ts function capitalize(str) { if (typeof str !== "string") throw new TypeError("Expected a string"); return str.length === 0 ? "" : str.charAt(0).toUpperCase() + str.slice(1); } __name(capitalize, "capitalize"); function toKebabCase(str) { if (typeof str !== "string") throw new TypeError("Expected a string"); return str.replace(/([a-z])([A-Z])/g, "$1-$2").replace(/\s+/g, "-").replace(/_+/g, "-").toLowerCase(); } __name(toKebabCase, "toKebabCase"); function toCamelCase(str) { if (typeof str !== "string") throw new TypeError("Expected a string"); return str.replace(/[-_](.)/g, (_, c) => c.toUpperCase()); } __name(toCamelCase, "toCamelCase"); function slugify(str) { return str.toLowerCase().replace(/[^\w\s-]/g, "").replace(/\s+/g, "-").replace(/-+/g, "-").replace(/^-+|-+$/g, ""); } __name(slugify, "slugify"); function truncate(str, len) { return str.length > len ? str.slice(0, len) + "..." : str; } __name(truncate, "truncate"); function toPascalCase(str) { return capitalize(toCamelCase(str)); } __name(toPascalCase, "toPascalCase"); function toSnakeCase(str) { return str.trim().replace(/([a-z])([A-Z])/g, "$1_$2").replace(/\s+/g, "_").replace(/-+/g, "_").toLowerCase(); } __name(toSnakeCase, "toSnakeCase"); 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; } __name(mask, "mask"); function stripHtml(str) { return str.replace(/<[^>]*>/g, ""); } __name(stripHtml, "stripHtml"); function equalsIgnoreCase(a, b) { return a.toLowerCase() === b.toLowerCase(); } __name(equalsIgnoreCase, "equalsIgnoreCase"); function reverse(str) { return str.split("").reverse().join(""); } __name(reverse, "reverse"); function countOccurrences(str, substring, caseSensitive = true) { if (!caseSensitive) { str = str.toLowerCase(); substring = substring.toLowerCase(); } return str.split(substring).length - 1; } __name(countOccurrences, "countOccurrences"); function toTitleCase(str) { if (typeof str !== "string") throw new TypeError("Expected a string"); return str.split(/(\s+)/).map((part) => part.trim().length === 0 ? part : part.charAt(0).toUpperCase() + part.slice(1).toLowerCase()).join(""); } __name(toTitleCase, "toTitleCase"); function escapeRegex(str) { if (typeof str !== "string") throw new TypeError("Expected a string"); return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); } __name(escapeRegex, "escapeRegex"); function unescapeHtml(str) { if (typeof str !== "string") return ""; const entities = { "&amp;": "&", "&lt;": "<", "&gt;": ">", "&quot;": '"', "&#39;": "'", "&#x27;": "'" }; return str.replace(/&(?:amp|lt|gt|quot|#39|#x27);/g, (match) => entities[match] || match); } __name(unescapeHtml, "unescapeHtml"); function isBlank(str) { return typeof str === "string" && str.trim().length === 0; } __name(isBlank, "isBlank"); function ellipsis(str, maxLength, suffix = "...") { if (typeof str !== "string" || str.length <= maxLength) return str; const truncated = str.slice(0, maxLength - suffix.length); const lastSpace = truncated.lastIndexOf(" "); return (lastSpace > 0 ? truncated.slice(0, lastSpace) : truncated) + suffix; } __name(ellipsis, "ellipsis"); exports.capitalize = capitalize; exports.countOccurrences = countOccurrences; exports.ellipsis = ellipsis; exports.equalsIgnoreCase = equalsIgnoreCase; exports.escapeRegex = escapeRegex; exports.isBlank = isBlank; exports.mask = mask; exports.reverse = reverse; exports.slugify = slugify; exports.stripHtml = stripHtml; exports.toCamelCase = toCamelCase; exports.toKebabCase = toKebabCase; exports.toPascalCase = toPascalCase; exports.toSnakeCase = toSnakeCase; exports.toTitleCase = toTitleCase; exports.truncate = truncate; exports.unescapeHtml = unescapeHtml;