|
| 1 | +/** |
| 2 | + * Collection of common validation helpers for strings, numbers, email, UUID, etc. |
| 3 | + */ |
| 4 | + |
| 5 | +/** |
| 6 | + * Checks if a string is a valid email address. |
| 7 | + * |
| 8 | + * @param {string} str - The input string. |
| 9 | + * @returns {boolean} True if valid email, else false. |
| 10 | + */ |
| 11 | +export function isEmail(str: string): boolean { |
| 12 | + // RFC 5322 official standard is more complex—this is practical. |
| 13 | + return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(str); |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * Checks if a string is a valid UUID (versions 1-5). |
| 18 | + * |
| 19 | + * @param {string} str - The input string. |
| 20 | + * @returns {boolean} True if valid UUID, else false. |
| 21 | + */ |
| 22 | +export function isUUID(str: string): boolean { |
| 23 | + return /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test( |
| 24 | + str, |
| 25 | + ); |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Checks if a string is a valid URL. |
| 30 | + * |
| 31 | + * @param {string} str - The input string. |
| 32 | + * @returns {boolean} True if valid URL, else false. |
| 33 | + */ |
| 34 | +export function isURL(str: string): boolean { |
| 35 | + try { |
| 36 | + new URL(str); |
| 37 | + return true; |
| 38 | + } catch { |
| 39 | + return false; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Checks if a string is a valid international phone number (E.164 or common patterns). |
| 45 | + * |
| 46 | + * @param {string} str - The input string. |
| 47 | + * @returns {boolean} True if looks like a phone number. |
| 48 | + */ |
| 49 | +export function isPhone(str: string): boolean { |
| 50 | + // E.164 or basic with spaces, dashes, parentheses |
| 51 | + return /^(\+?[1-9]\d{1,14}|(\+?\d{1,3}[-.\s]?)?\(?\d{2,4}\)?([-\s]?\d{3,4}){2,})$/.test( |
| 52 | + str, |
| 53 | + ); |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Checks if a string is strictly alphanumeric (letters/numbers only). |
| 58 | + * |
| 59 | + * @param {string} str - The input string. |
| 60 | + * @returns {boolean} True if alphanumeric. |
| 61 | + */ |
| 62 | +export function isAlphanumeric(str: string): boolean { |
| 63 | + return /^[a-z0-9]+$/i.test(str); |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Checks if a string or number can be safely parsed to a number. |
| 68 | + * |
| 69 | + * @param {string | number} value - The value to check. |
| 70 | + * @returns {boolean} True if the value is numeric. |
| 71 | + */ |
| 72 | +export function isNumeric(value: string | number): boolean { |
| 73 | + if (typeof value === "number") return !isNaN(value) && isFinite(value); |
| 74 | + return ( |
| 75 | + typeof value === "string" && value.trim() !== "" && !isNaN(Number(value)) |
| 76 | + ); |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * Checks if a string is a valid hex color code (e.g. #FFF or #FFFFFF). |
| 81 | + * |
| 82 | + * @param {string} str - Input string. |
| 83 | + * @returns {boolean} |
| 84 | + */ |
| 85 | +export function isHexColor(str: string): boolean { |
| 86 | + return /^#([a-f0-9]{6}|[a-f0-9]{3})$/i.test(str); |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * Checks if a string is a valid ISO8601 date string. |
| 91 | + * |
| 92 | + * @param {string} str - Input string. |
| 93 | + * @returns {boolean} |
| 94 | + */ |
| 95 | +export function isISODate(str: string): boolean { |
| 96 | + // YYYY-MM-DD, extended, or with time portion |
| 97 | + return /^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}:\d{2}(?:\.\d+)?(Z|[\+|-]\d{2}:\d{2})?)?$/.test( |
| 98 | + str, |
| 99 | + ); |
| 100 | +} |
0 commit comments