UNPKG

@jlhv/string-helper

Version:

A simple utility library for string manipulation.

112 lines (111 loc) 3.64 kB
"use strict"; /** * String Helper Library * Provides various string manipulation functions */ Object.defineProperty(exports, "__esModule", { value: true }); exports.capitalize = capitalize; exports.toLowerCase = toLowerCase; exports.toUpperCase = toUpperCase; exports.trim = trim; exports.reverse = reverse; exports.toCamelCase = toCamelCase; exports.toKebabCase = toKebabCase; exports.toSnakeCase = toSnakeCase; exports.slugify = slugify; exports.isValidEmail = isValidEmail; exports.isValidURL = isValidURL; exports.contains = contains; exports.repeat = repeat; exports.replaceAll = replaceAll; exports.truncate = truncate; exports.countOccurrences = countOccurrences; exports.countWords = countWords; exports.maskString = maskString; exports.removeAllWhitespace = removeAllWhitespace; exports.removeSpecialCharacters = removeSpecialCharacters; exports.extractNumbers = extractNumbers; exports.obfuscateEmail = obfuscateEmail; exports.generateRandomString = generateRandomString; exports.swapCase = swapCase; exports.isAlphanumeric = isAlphanumeric; exports.isTitleCase = isTitleCase; function capitalize(str) { return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase(); } function toLowerCase(str) { return str.toLowerCase(); } function toUpperCase(str) { return str.toUpperCase(); } function trim(str) { return str.trim(); } function reverse(str) { return str.split('').reverse().join(''); } function toCamelCase(str) { return str.replace(/(?:^|\s)(\w)/g, (_, c) => c.toUpperCase()).replace(/\s+/g, ''); } function toKebabCase(str) { return str.replace(/([a-z])([A-Z])/g, '$1-$2').replace(/\s+/g, '-').toLowerCase(); } function toSnakeCase(str) { return str.replace(/([a-z])([A-Z])/g, '$1_$2').replace(/\s+/g, '_').toLowerCase(); } function slugify(str) { return str.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, ''); } function isValidEmail(email) { return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email); } function isValidURL(url) { return /^(https?:\/\/)?([\w-]+\.)+[\w-]{2,}(\/.*)?$/.test(url); } function contains(str, substring) { return str.includes(substring); } function repeat(str, count) { return str.repeat(count); } function replaceAll(str, search, replacement) { return str.split(search).join(replacement); } function truncate(str, maxLength) { return str.length > maxLength ? str.slice(0, maxLength) + '...' : str; } function countOccurrences(str, substring) { return (str.match(new RegExp(substring, 'g')) || []).length; } function countWords(str) { return str.trim().split(/\s+/).length; } function maskString(str, visibleChars = 4, maskChar = '*') { return str.length > visibleChars ? maskChar.repeat(str.length - visibleChars) + str.slice(-visibleChars) : str; } function removeAllWhitespace(str) { return str.replace(/\s+/g, ''); } function removeSpecialCharacters(str) { return str.replace(/[^a-zA-Z0-9 ]/g, ''); } function extractNumbers(str) { return str.replace(/\D+/g, ''); } function obfuscateEmail(email) { return email.replace(/(\w{3})[^@]+(@.*)/, '$1***$2'); } function generateRandomString(length) { const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; return Array.from({ length }, () => chars[Math.floor(Math.random() * chars.length)]).join(''); } function swapCase(str) { return str.replace(/([a-z])|([A-Z])/g, (_, lower, upper) => lower ? lower.toUpperCase() : upper.toLowerCase()); } function isAlphanumeric(str) { return /^[a-zA-Z0-9]+$/.test(str); } function isTitleCase(str) { return str.split(' ').every(word => /^[A-Z][a-z]*$/.test(word)); }