UNPKG

utilyx

Version:

Modern utility helper library for cleaner, faster TypeScript/JavaScript development 🚀🔧

189 lines (188 loc) • 6.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.toTitleCase = toTitleCase; exports.camelToSnake = camelToSnake; exports.snakeToCamel = snakeToCamel; exports.slugify = slugify; exports.truncateText = truncateText; exports.reverseString = reverseString; exports.countWords = countWords; exports.isPalindrome = isPalindrome; exports.isEmail = isEmail; exports.isURL = isURL; exports.trimExtraSpaces = trimExtraSpaces; exports.maskEmail = maskEmail; exports.generateRandomString = generateRandomString; /** * Converts a string to title case (capitalizes first letter of each word) * @param str - Input string * @param lowercaseRest - Whether to lowercase the rest of each word (default: true) * @returns String in title case * @example * toTitleCase("hello world") → "Hello World" * toTitleCase("hElLo wOrLd", false) → "HElLo WOrLd" */ function toTitleCase(str, lowercaseRest = true) { return str.replace(/\w\S*/g, word => word.charAt(0).toUpperCase() + (lowercaseRest ? word.slice(1).toLowerCase() : word.slice(1))); } /** * Converts camelCase to snake_case * @param str - Input string in camelCase * @param uppercase - Whether to convert to UPPER_SNAKE_CASE (default: false) * @returns String in snake_case * @example * camelToSnake("myVarName") → "my_var_name" * camelToSnake("myVarName", true) → "MY_VAR_NAME" */ function camelToSnake(str, uppercase = false) { const result = str.replace(/([a-z])([A-Z])/g, '$1_$2'); return uppercase ? result.toUpperCase() : result.toLowerCase(); } /** * Converts snake_case to camelCase * @param str - Input string in snake_case * @param pascal - Whether to convert to PascalCase (default: false) * @returns String in camelCase or PascalCase * @example * snakeToCamel("my_var_name") → "myVarName" * snakeToCamel("my_var_name", true) → "MyVarName" */ function snakeToCamel(str, pascal = false) { const camel = str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase()); return pascal ? camel.charAt(0).toUpperCase() + camel.slice(1) : camel; } /** * Converts a string to a URL-friendly slug * @param str - Input string * @param separator - Character to use as separator (default: '-') * @param preserveCase - Whether to preserve original case (default: false) * @returns URL-friendly slug * @example * slugify("Hello World!") → "hello-world" * slugify("Hello World!", '_', true) → "Hello_World" */ function slugify(str, separator = '-', preserveCase = false) { let result = str.trim(); if (!preserveCase) result = result.toLowerCase(); return result .replace(/[^a-zA-Z0-9\s-]/g, '') .replace(/\s+/g, separator); } /** * Truncates text with ellipsis * @param str - Input string * @param maxLength - Maximum length before truncation * @param ellipsis - Custom ellipsis string (default: '...') * @param preserveWords - Whether to preserve whole words (default: true) * @returns Truncated string * @example * truncateText("Hello world", 8) → "Hello..." * truncateText("Hello world", 8, '..', false) → "Hello wo.." */ function truncateText(str, maxLength, ellipsis = '...', preserveWords = true) { if (str.length <= maxLength) return str; if (preserveWords) { const truncated = str.substr(0, maxLength); const lastSpace = truncated.lastIndexOf(' '); return (lastSpace > 0 ? truncated.substr(0, lastSpace) : truncated) + ellipsis; } return str.slice(0, maxLength).trimEnd() + ellipsis; } /** * Reverses a string. * @param str - Input string. * @returns Reversed string. */ function reverseString(str) { return str.split('').reverse().join(''); } /** * Counts words in a string (split by spaces). * @param str - Input string. * @returns Number of words. */ function countWords(str) { if (!str.trim()) return 0; return str.split(/\s+/).length; } /** * Checks if a string is a palindrome * @param str - Input string * @param caseSensitive - Whether comparison should be case-sensitive (default: false) * @param ignoreSpaces - Whether to ignore spaces (default: true) * @returns true if the string is a palindrome * @example * isPalindrome("Madam") → true * isPalindrome("A man a plan a canal Panama") → true * isPalindrome("Racecar", true) → false */ function isPalindrome(str, caseSensitive = false, ignoreSpaces = true) { let cleanStr = str; if (!caseSensitive) cleanStr = cleanStr.toLowerCase(); if (ignoreSpaces) cleanStr = cleanStr.replace(/\s+/g, ''); cleanStr = cleanStr.replace(/[^a-z0-9]/gi, ''); return cleanStr === cleanStr.split('').reverse().join(''); } function isEmail(str) { return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(str); } function isURL(str) { return /^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)$/.test(str); } /** * Trims extra spaces between words (e.g., "Hello world" → "Hello world"). * @param str - Input string. * @returns String with normalized spaces. */ function trimExtraSpaces(str) { return str.replace(/\s+/g, ' ').trim(); } /** * Masks the local part of an email (e.g., "user@domain.com" → "us**@domain.com"). * @param email - Email address. * @returns Masked email. * @throws Error if input is not a valid email. */ function maskEmail(email) { if (!isEmail(email)) throw new Error('Invalid email'); const [local, domain] = email.split('@'); const maskedLocal = local.slice(0, 2) + '**'; return `${maskedLocal}@${domain}`; } /** * Generates a random string * @param length - Length of the string (default: 10) * @param options - Configuration options * @param options.includeNumbers - Whether to include numbers (default: true) * @param options.includeUppercase - Whether to include uppercase letters (default: true) * @param options.includeLowercase - Whether to include lowercase letters (default: true) * @param options.customChars - Custom character set to use * @returns Random string * @example * generateRandomString(8) → "A3b7GhK9" * generateRandomString(6, { includeNumbers: false }) → "aBcDef" */ function generateRandomString(length = 10, options = {}) { const { includeNumbers = true, includeUppercase = true, includeLowercase = true, customChars = '' } = options; let chars = customChars; if (!chars) { chars += includeUppercase ? 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' : ''; chars += includeLowercase ? 'abcdefghijklmnopqrstuvwxyz' : ''; chars += includeNumbers ? '0123456789' : ''; } if (!chars.length) { throw new Error('No character set available for string generation'); } let result = ''; for (let i = 0; i < length; i++) { result += chars.charAt(Math.floor(Math.random() * chars.length)); } return result; }