UNPKG

nhb-toolbox

Version:

A versatile collection of smart, efficient, and reusable utility functions and classes for everyday development needs.

115 lines (114 loc) 4.04 kB
import { isArray } from './non-primitives.js'; import { isString } from './primitives.js'; /** * * Type guard to check if a value is a valid email string. * @param value - The value to check. * @returns `true` if the value is a valid email, otherwise `false`. */ export function isEmail(value) { return (isString(value) && /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(value)); } /** * * Type guard to check if a value is an array of valid email strings. * @param value - The value to check. * @returns `true` if the value is an array of valid email strings, otherwise `false`. */ export function isEmailArray(value) { return isArray(value) && value?.every(isEmail); } /** * * Type guard to check if a value is a valid date string. * @param value - The value to check. * @returns `true` if the value is a valid date string, otherwise `false`. */ export function isDateString(value) { return isString(value) && !isNaN(Date.parse(value)); } /** * * Type guard to check if a value is a valid UUID (v4). * @param value - The value to check. * @returns `true` if the value is a valid UUID, otherwise `false`. */ export function isUUID(value) { return (isString(value) && /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(value)); } /** * * Type guard to check if the code is running in a browser environment. * @returns `true` if the code is running in a browser, otherwise `false`. */ export function isBrowser() { return typeof window !== 'undefined' && typeof document !== 'undefined'; } /** * * Type guard to check if the code is running in a Node.js environment. * @returns `true` if the code is running in Node.js, otherwise `false`. */ export function isNode() { return (typeof process !== 'undefined' && process.versions != null && process.versions.node != null); } /** * * Type guard to check if a value is a valid URL. * @param value - The value to check. * @returns `true` if the value is a valid URL, otherwise `false`. */ export function isURL(value) { try { new URL(isString(value) ? value : ''); return true; } catch { return false; } } /** * * Type guard to check if a value is a valid Base64 encoded string. * @param value - The value to check. * @returns `true` if the value is a valid Base64 string, otherwise `false`. */ export function isBase64(value) { return (isString(value) && /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(value)); } /** * * Type guard to check if a value is a valid phone number. * @param value - The value to check. * @returns `true` if the value is a valid phone number, otherwise `false`. */ export function isPhoneNumber(value) { return isString(value) && /^\+?[1-9]\d{1,14}$/.test(value); } /** * * Type guard to check if a value is a valid IP address (IPv4 or IPv6). * @param value - The value to check. * @returns `true` if the value is a valid IP address, otherwise `false`. */ export function isIPAddress(value) { return (isString(value) && /^(?:\d{1,3}\.){3}\d{1,3}$|^([a-f0-9:]+:+)+[a-f0-9]+$/i.test(value)); } /** * * Type guard to check if the current environment matches a given string. * @param env - The expected environment (e.g., "production", "development"). * @returns `true` if the value is a numeric string parsable by `Number()`, otherwise `false`. */ export function isEnvironment(env) { return process.env.NODE_ENV === env; } /** * * Type guard to check if a value is a string representing a finite number. * * Accepts strings like: `"42"`, `" -5.5 "`, `"0.123"`, `"-0"`, `"1e5"`. * Rejects strings like: `"NaN"`, `"Infinity"`, `"-Infinity"`, `"abc"`, `""`, `"42abc"`. * * @param value - The value to test. * @returns `true` if the value is a string that fully represents a finite number. */ export function isNumericString(value) { return (isString(value) && value?.trim() !== '' && Number.isFinite(Number(value))); }