UNPKG

dev-utils-plus

Version:

Type-safe utility functions for JavaScript/TypeScript: string, array, object, date, validation, crypto, format, math

101 lines 2.65 kB
"use strict"; /** * String utility functions for common web development tasks */ Object.defineProperty(exports, "__esModule", { value: true }); exports.capitalize = capitalize; exports.toCamelCase = toCamelCase; exports.toKebabCase = toKebabCase; exports.toSnakeCase = toSnakeCase; exports.truncate = truncate; exports.stripHtml = stripHtml; exports.generateRandomString = generateRandomString; exports.isValidEmailString = isValidEmailString; exports.wordCount = wordCount; exports.reverse = reverse; exports.isPalindrome = isPalindrome; /** * Capitalizes the first letter of a string */ function capitalize(str) { if (!str) return str; return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase(); } /** * Converts a string to camelCase */ function toCamelCase(str) { return str .replace(/[-_\s]+(.)?/g, (_, c) => (c ? c.toUpperCase() : '')) .replace(/^(.)/, (_, c) => c.toLowerCase()); } /** * Converts a string to kebab-case */ function toKebabCase(str) { return str .replace(/([a-z])([A-Z])/g, '$1-$2') .replace(/[\s_]+/g, '-') .toLowerCase(); } /** * Converts a string to snake_case */ function toSnakeCase(str) { return str .replace(/([a-z])([A-Z])/g, '$1_$2') .replace(/[\s-]+/g, '_') .toLowerCase(); } /** * Truncates a string to a specified length and adds ellipsis */ function truncate(str, length, suffix = '...') { if (str.length <= length) return str; return str.substring(0, length - suffix.length) + suffix; } /** * Removes HTML tags from a string */ function stripHtml(html) { return html.replace(/<[^>]*>/g, ''); } /** * Generates a random string of specified length */ function generateRandomString(length, charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') { let result = ''; for (let i = 0; i < length; i++) { result += charset.charAt(Math.floor(Math.random() * charset.length)); } return result; } /** * Checks if a string is a valid email address */ function isValidEmailString(email) { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return emailRegex.test(email); } /** * Counts the number of words in a string */ function wordCount(str) { return str.trim().split(/\s+/).filter(word => word.length > 0).length; } /** * Reverses a string */ function reverse(str) { return str.split('').reverse().join(''); } /** * Checks if a string is a palindrome */ function isPalindrome(str) { const cleaned = str.toLowerCase().replace(/[^a-z0-9]/g, ''); return cleaned === reverse(cleaned); } //# sourceMappingURL=index.js.map