nhb-toolbox
Version:
A versatile collection of smart, efficient, and reusable utility functions and classes for everyday development needs.
129 lines (128 loc) • 4.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isEmail = isEmail;
exports.isEmailArray = isEmailArray;
exports.isDateString = isDateString;
exports.isUUID = isUUID;
exports.isBrowser = isBrowser;
exports.isNode = isNode;
exports.isURL = isURL;
exports.isBase64 = isBase64;
exports.isPhoneNumber = isPhoneNumber;
exports.isIPAddress = isIPAddress;
exports.isEnvironment = isEnvironment;
exports.isNumericString = isNumericString;
const non_primitives_1 = require("./non-primitives");
const primitives_1 = require("./primitives");
/**
* * 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`.
*/
function isEmail(value) {
return ((0, primitives_1.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`.
*/
function isEmailArray(value) {
return (0, non_primitives_1.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`.
*/
function isDateString(value) {
return (0, primitives_1.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`.
*/
function isUUID(value) {
return ((0, primitives_1.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`.
*/
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`.
*/
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`.
*/
function isURL(value) {
try {
new URL((0, primitives_1.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`.
*/
function isBase64(value) {
return ((0, primitives_1.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`.
*/
function isPhoneNumber(value) {
return (0, primitives_1.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`.
*/
function isIPAddress(value) {
return ((0, primitives_1.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`.
*/
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.
*/
function isNumericString(value) {
return ((0, primitives_1.isString)(value) &&
value?.trim() !== '' &&
Number.isFinite(Number(value)));
}