UNPKG

@oxog/string

Version:

Comprehensive string manipulation utilities with zero dependencies

118 lines (117 loc) 4.19 kB
export function isEmail(str) { const emailRegex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/; return emailRegex.test(str); } export function isUrl(str, options = {}) { const { requireProtocol = false, allowUnderscore = false, allowTrailingDot = false, allowProtocols = ['http', 'https', 'ftp'] } = options; if (!str || typeof str !== 'string') { return false; } try { const url = new URL(str); if (!allowProtocols.includes(url.protocol.slice(0, -1))) { return false; } if (!allowUnderscore && url.hostname.includes('_')) { return false; } if (!allowTrailingDot && url.hostname.endsWith('.')) { return false; } return true; } catch (_a) { if (!requireProtocol && !str.includes('://')) { try { const testUrl = new URL(`http://${str}`); // Additional validation for domain format if (!/^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(testUrl.hostname)) { return false; } return true; } catch (_b) { return false; } } return false; } } export function isUuid(str, version) { const uuidRegexes = { 1: /^[0-9a-f]{8}-[0-9a-f]{4}-1[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i, 2: /^[0-9a-f]{8}-[0-9a-f]{4}-2[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i, 3: /^[0-9a-f]{8}-[0-9a-f]{4}-3[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i, 4: /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i, 5: /^[0-9a-f]{8}-[0-9a-f]{4}-5[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i }; if (version && uuidRegexes[version]) { return uuidRegexes[version].test(str); } const generalUuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i; return generalUuidRegex.test(str); } export function isHexColor(str) { const hexColorRegex = /^#([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i; return hexColorRegex.test(str); } export function isBase64(str) { if (!str || str.length % 4 !== 0) { return false; } const base64Regex = /^[A-Za-z0-9+/]*={0,2}$/; return base64Regex.test(str); } export function isJson(str) { try { JSON.parse(str); return true; } catch (_a) { return false; } } export function isNumeric(str) { return !isNaN(Number(str)) && !isNaN(parseFloat(str)) && isFinite(Number(str)); } export function isAlpha(str, locale) { if (locale) { const localeRegexes = { 'en': /^[a-zA-Z]+$/, 'es': /^[a-zA-ZáéíóúüñÁÉÍÓÚÜÑ]+$/, 'fr': /^[a-zA-ZàâäéèêëïîôöùûüÿçÀÂÄÉÈÊËÏÎÔÖÙÛÜŸÇ]+$/, 'de': /^[a-zA-ZäöüßÄÖÜ]+$/, 'it': /^[a-zA-ZàèéìíîòóùúÀÈÉÌÍÎÒÓÙÚ]+$/, 'pt': /^[a-zA-ZáàâãéêíóôõúçÁÀÂÃÉÊÍÓÔÕÚÇ]+$/ }; const regex = localeRegexes[locale]; if (regex) { return regex.test(str); } } return /^[a-zA-Z]+$/.test(str); } export function isAlphanumeric(str, locale) { if (locale) { const localeRegexes = { 'en': /^[a-zA-Z0-9]+$/, 'es': /^[a-zA-Z0-9áéíóúüñÁÉÍÓÚÜÑ]+$/, 'fr': /^[a-zA-Z0-9àâäéèêëïîôöùûüÿçÀÂÄÉÈÊËÏÎÔÖÙÛÜŸÇ]+$/, 'de': /^[a-zA-Z0-9äöüßÄÖÜ]+$/, 'it': /^[a-zA-Z0-9àèéìíîòóùúÀÈÉÌÍÎÒÓÙÚ]+$/, 'pt': /^[a-zA-Z0-9áàâãéêíóôõúçÁÀÂÃÉÊÍÓÔÕÚÇ]+$/ }; const regex = localeRegexes[locale]; if (regex) { return regex.test(str); } } return /^[a-zA-Z0-9]+$/.test(str); } export function isEmpty(str, options = {}) { const { ignoreWhitespace = false } = options; if (ignoreWhitespace) { return str.trim().length === 0; } return str.length === 0; }