UNPKG

guardz

Version:

A simple and lightweight TypeScript type guard library for runtime type validation.

58 lines (57 loc) 1.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isOneOf = isOneOf; const stringify_1 = require("../stringify"); /** * Creates a type guard that checks if a value matches one of several specific values. * * This function creates a type guard that validates against a list of exact values * using strict equality (===). Useful for union types of literal values. * * @template T - The union type of all possible values * @param values - The list of valid values to check against * @returns A type guard function that validates against the provided values * * @example * ```typescript * import { isOneOf } from 'guardz'; * * const isColor = isOneOf("red", "green", "blue"); * const isStatus = isOneOf(200, 404, 500); * const isBooleanOrNull = isOneOf(true, false, null); * * console.log(isColor("red")); // true * console.log(isColor("yellow")); // false * console.log(isStatus(404)); // true * console.log(isStatus(201)); // false * * // With type narrowing * type Theme = "light" | "dark" | "auto"; * const isTheme = isOneOf("light", "dark", "auto"); * * const data: unknown = getUserInput(); * if (isTheme(data)) { * // data is now typed as "light" | "dark" | "auto" * console.log(`Theme selected: ${data}`); * } * * // Mixed types * const isMixed = isOneOf("none", 0, false); * if (isMixed(data)) { * // data is typed as "none" | 0 | false * } * ``` */ function isOneOf(...acceptableValues) { return function (value, config) { const isValid = acceptableValues.some(function (acceptableValue) { return value === acceptableValue; }); if (!isValid && config) { config.callbackOnError(`${config.identifier} (${(0, stringify_1.stringify)(value)}) must be one of following values ${acceptableValues .map(stringify_1.stringify) .join(' | ')}`); } return isValid; }; }