UNPKG

@nasriya/atomix

Version:

Composable helper functions for building reliable systems

87 lines (86 loc) 3.08 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const strings_utils_1 = __importDefault(require("../data-types/string/strings-utils")); const mimes_1 = __importDefault(require("./mimes/mimes")); class HTTPGuard { /** * Checks if the provided value is a valid HTML string. * * @param value - The value to check. * @returns True if the value is a valid HTML string, otherwise false. * @since v1.0.0 * @description * A valid HTML string is a string that contains at least one HTML tag. * The HTML tags are matched using a regex pattern. */ isHTML(value) { const htmlRegex = /<\/?[a-z][\s\S]*>/i; return strings_utils_1.default.guard.isString(value) && htmlRegex.test(value); } /** * Checks if the provided value is a valid MIME type. * * @param value - The value to check. * @returns True if the value is a valid MIME type, otherwise false. * @since v1.0.0 * @description * A valid MIME type is a string that matches one of the predefined MIME types. */ isMimeType(value) { return mimes_1.default.isValid.mime(value); } /** * Checks if the provided value is a valid file extension. * * @param value - The value to check. * @returns True if the value is a valid file extension, otherwise false. * @since v1.0.0 * @description * A valid file extension is a string that matches one of the predefined file extensions. */ isExtension(value) { return mimes_1.default.isValid.extension(value); } /** * Checks if the provided value is a valid URL. * * @param url - The value to check. * @returns True if the value is a valid URL, otherwise false. * @since v1.0.0 * @description * The URL is validated by attempting to create a new URL object * with the provided value. If the value is not a valid URL, the * constructor will throw an error. */ isValidURL(url) { try { if (!strings_utils_1.default.guard.isString(url)) { return false; } new URL(url); return true; } catch (error) { return false; } } /** * Checks if the provided value is a valid email address. * * @param value - The value to check. * @returns True if the value is a valid email address, otherwise false. * @since v1.0.0 * @description * The email address is validated by attempting to match it against a regular * expression. The regular expression is in the format of `/^[^\s@]+@[^\s@]+\.[^\s@]+$/i`. */ isEmail(value) { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/i; return strings_utils_1.default.guard.isString(value) && emailRegex.test(value); } } const httpGuard = new HTTPGuard; exports.default = httpGuard;