{"version":3,"file":"type-guard.cjs","names":[],"sources":["../src/type-guard/_exports.ts"],"sourcesContent":["/**\n * ---header-docs-section---\n * # yummies/type-guard\n *\n * ## Description\n *\n * Runtime **type guards** and narrowers for primitives, DOM nodes, `NaN`, and common app values.\n * Each helper is implemented once with consistent `typeof` / `Object.prototype.toString` logic so\n * `unknown` from JSON, events, or third-party scripts becomes safe typed branches without ad-hoc\n * checks scattered through the codebase.\n *\n * ## Usage\n *\n * ```ts\n * import { typeGuard } from \"yummies/type-guard\";\n * ```\n */\n\nimport type {\n  AnyFunction,\n  AnyObject,\n  FalsyValues,\n  ValueOf,\n} from 'yummies/types';\n\nconst TYPE = {\n  Null: 'null',\n  Undefined: 'undefined',\n  NaN: 'nan',\n  Object: '[object Object]',\n  Array: '[object Array]',\n  String: '[object String]',\n  Number: '[object Number]',\n  Boolean: '[object Boolean]',\n  Function: '[object Function]',\n  AsyncFunction: '[object AsyncFunction]',\n  RegExp: '[object RegExp]',\n  Symbol: '[object Symbol]',\n  Infinite: 'infinite',\n  Element: 'element',\n};\n\ntype Type = ValueOf<typeof TYPE>;\n\nfunction getType(value: unknown): Type {\n  if (value === undefined) {\n    return TYPE.Undefined;\n  }\n  if (value === null) {\n    return TYPE.Null;\n  }\n\n  // handle DOM elements\n  // @ts-expect-error\n  if (value && (value.nodeType === 1 || value.nodeType === 9)) {\n    return TYPE.Element;\n  }\n\n  const stringifiedValue = Object.prototype.toString.call(value);\n\n  // handle NaN and Infinity\n  if (stringifiedValue === TYPE.Number) {\n    if (Number.isNaN(value as number)) {\n      return TYPE.NaN;\n    }\n    if (!Number.isFinite(value as number)) {\n      return TYPE.Infinite;\n    }\n  }\n\n  return stringifiedValue as Type;\n}\n\nconst createTypeGuard =\n  <T>(...types: Type[]) =>\n  (value: unknown): value is T =>\n    types.includes(getType(value));\n\n/**\n * Checks that a value is neither `null` nor `undefined`.\n *\n * @template T Value type without nullish branches.\n * @param value Value to test.\n * @returns `true` when the value is defined.\n *\n * @example\n * ```ts\n * isDefined(0); // true\n * ```\n *\n * @example\n * ```ts\n * isDefined(null); // false\n * ```\n */\nexport const isDefined = <T>(value: T | undefined | null): value is T =>\n  value != null;\n\n/**\n * Checks whether a value is truthy (`!!value`).\n *\n * @param value Value to test.\n * @returns `true` when `!!value` is true.\n *\n * @example\n * ```ts\n * isTruthy(1); // true\n * ```\n *\n * @example\n * ```ts\n * isTruthy(0); // false\n * ```\n */\nexport const isTruthy = <T>(value: T): value is Exclude<T, FalsyValues> =>\n  !!value;\n\n/**\n * Checks whether a value is falsy (`!Boolean(value)`).\n *\n * @param value Value to test.\n * @returns `true` when `Boolean(value)` is false.\n *\n * @example\n * ```ts\n * isFalsy(null); // true\n * ```\n *\n * @example\n * ```ts\n * isFalsy(\"x\"); // false\n * ```\n */\nexport const isFalsy = <T>(value: T): value is Extract<T, FalsyValues> =>\n  !value;\n\n/**\n * Checks whether a value is exactly `null`.\n *\n * @param value Value to test.\n * @returns `true` when the value is `null`.\n *\n * @example\n * ```ts\n * isNull(null); // true\n * ```\n *\n * @example\n * ```ts\n * isNull(undefined); // false\n * ```\n */\nexport const isNull = createTypeGuard<null>(TYPE.Null);\n\n/**\n * Checks whether a value is exactly `undefined`.\n *\n * @param value Value to test.\n * @returns `true` when the value is `undefined`.\n *\n * @example\n * ```ts\n * isUndefined(undefined); // true\n * ```\n *\n * @example\n * ```ts\n * isUndefined('value'); // false\n * ```\n */\nexport const isUndefined = createTypeGuard<undefined>(TYPE.Undefined);\n\n/**\n * Checks whether a value is a plain object.\n *\n * @param value Value to test.\n * @returns `true` when the value matches `[object Object]`.\n *\n * @example\n * ```ts\n * isObject({ id: 1 }); // true\n * ```\n *\n * @example\n * ```ts\n * isObject([]); // false\n * ```\n */\nexport const isObject = createTypeGuard<AnyObject>(TYPE.Object);\n\n/**\n * Checks whether a value is object-like: non-null and `typeof value === 'object'`.\n *\n * This includes plain objects, arrays, `Date`, `RegExp`, boxed primitives, and other built-ins\n * whose `typeof` is `\"object\"`. It is broader than `isObject`, which only matches plain objects.\n *\n * @template T Input value type.\n * @param value Value to test.\n * @returns `true` when the value is a non-null object.\n *\n * @example\n * ```ts\n * isObjectLike({ a: 1 }); // true\n * ```\n *\n * @example\n * ```ts\n * isObjectLike([]); // true\n * ```\n *\n * @example\n * ```ts\n * isObjectLike(new Date()); // true\n * ```\n *\n * @example\n * ```ts\n * isObjectLike(null); // false\n * ```\n *\n * @example\n * ```ts\n * isObjectLike(() => {}); // false\n * ```\n */\nexport const isObjectLike = <T = never>(\n  value: unknown,\n): value is T extends never ? Extract<typeof value, object> : T =>\n  typeof value === 'object' && !!value;\n\n/**\n * Checks whether a value is an array.\n *\n * @param value Value to test.\n * @returns `true` when the value is an array.\n *\n * @example\n * ```ts\n * isArray([1, 2, 3]); // true\n * ```\n *\n * @example\n * ```ts\n * isArray({ length: 1 }); // false\n * ```\n */\nexport const isArray = createTypeGuard<unknown[]>(TYPE.Array);\n\n/**\n * Checks whether a value is a string object or primitive string.\n *\n * @param value Value to test.\n * @returns `true` when the value is a string.\n *\n * @example\n * ```ts\n * isString('hello'); // true\n * ```\n *\n * @example\n * ```ts\n * isString(123); // false\n * ```\n */\nexport const isString = createTypeGuard<string>(TYPE.String);\n\n/**\n * Checks whether a value is a finite number.\n *\n * Unlike `isNaN` and `isInfinite`, this guard only matches regular numeric values.\n *\n * @param value Value to test.\n * @returns `true` when the value is a non-NaN finite number.\n *\n * @example\n * ```ts\n * isNumber(123); // true\n * ```\n *\n * @example\n * ```ts\n * isNumber(Number.NaN); // false\n * ```\n */\nexport const isNumber = createTypeGuard<number>(TYPE.Number);\n\n/**\n * Checks whether a value is a boolean.\n *\n * @param value Value to test.\n * @returns `true` when the value is a boolean.\n *\n * @example\n * ```ts\n * isBoolean(true); // true\n * ```\n *\n * @example\n * ```ts\n * isBoolean('true'); // false\n * ```\n */\nexport const isBoolean = createTypeGuard<boolean>(TYPE.Boolean);\n\n/**\n * Checks whether a value is a synchronous or asynchronous function.\n *\n * @param value Value to test.\n * @returns `true` when the value is a function.\n *\n * @example\n * ```ts\n * isFunction(() => {}); // true\n * ```\n *\n * @example\n * ```ts\n * isFunction(async () => {}); // true\n * ```\n */\nexport const isFunction = createTypeGuard<AnyFunction>(\n  TYPE.Function,\n  TYPE.AsyncFunction,\n);\n\n/**\n * Checks whether a value is a regular expression.\n *\n * @param value Value to test.\n * @returns `true` when the value is a `RegExp`.\n *\n * @example\n * ```ts\n * isRegExp(/foo/); // true\n * ```\n *\n * @example\n * ```ts\n * isRegExp('foo'); // false\n * ```\n */\nexport const isRegExp = createTypeGuard<RegExp>(TYPE.RegExp);\n\n/**\n * Checks whether a value looks like a DOM element or document node.\n *\n * @param value Value to test.\n * @returns `true` when the value has an element-like node type.\n *\n * @example\n * ```ts\n * isElement(document.body); // true\n * ```\n *\n * @example\n * ```ts\n * isElement({ nodeType: 3 }); // false\n * ```\n */\nexport const isElement = createTypeGuard<HTMLElement>(TYPE.Element);\n\n/**\n * Checks whether a value is `NaN`.\n *\n * @param value Value to test.\n * @returns `true` when the value is `NaN`.\n *\n * @example\n * ```ts\n * isNaN(Number.NaN); // true\n * ```\n *\n * @example\n * ```ts\n * isNaN(5); // false\n * ```\n */\nexport const isNaN = createTypeGuard<number>(TYPE.NaN) as (\n  value: unknown,\n) => boolean;\n\n/**\n * Checks whether a value is positive or negative infinity.\n *\n * @param value Value to test.\n * @returns `true` when the value is not finite.\n *\n * @example\n * ```ts\n * isInfinite(Infinity); // true\n * ```\n *\n * @example\n * ```ts\n * isInfinite(10); // false\n * ```\n */\nexport const isInfinite = createTypeGuard<number>(TYPE.Infinite) as (\n  value: unknown,\n) => boolean;\n\n/**\n * Checks whether a value is a symbol.\n *\n * @param value Value to test.\n * @returns `true` when the value is a symbol.\n *\n * @example\n * ```ts\n * isSymbol(Symbol('id')); // true\n * ```\n *\n * @example\n * ```ts\n * isSymbol('id'); // false\n * ```\n */\nexport const isSymbol = createTypeGuard<symbol>(TYPE.Symbol);\n"],"mappings":";;;;;;;;;;;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