import { isEmptyObject, isEqual, isNil, isPlainObject } from "es-toolkit"; import { isArray } from "es-toolkit/compat"; import type { CheckFailure, JsonValue } from "../types"; import type { ExpectResult, ValueMatcher } from "./types"; import { mismatchFailure } from "./failure"; export const MatcherKeys = ["contains", "empty", "equals", "exists", "gt", "gte", "lt", "lte", "regex"] as const; const MATCHER_KEY_SET = new Set(MatcherKeys); export function applyMatcher( actual: unknown, matcher: ValueMatcher, options: { stringifyNonString?: boolean } = {}, ): boolean { for (const [key, expected] of Object.entries(matcher)) { if (expected === undefined) continue; switch (key) { case "contains": if (!stringValue(actual, options).includes(expected as string)) return false; break; case "empty": { const empty = isEmptyValue(actual); if (expected !== empty) return false; break; } case "equals": if (!isEqual(actual, expected)) return false; break; case "exists": if (expected) { if (actual === undefined) return false; } else { if (actual !== undefined) return false; } break; case "gt": if (!compareNumber(actual, expected as number, (left, right) => left > right)) return false; break; case "gte": if (!compareNumber(actual, expected as number, (left, right) => left >= right)) return false; break; case "lt": if (!compareNumber(actual, expected as number, (left, right) => left < right)) return false; break; case "lte": if (!compareNumber(actual, expected as number, (left, right) => left <= right)) return false; break; case "regex": if (!new RegExp(expected as string).test(stringValue(actual, options))) return false; break; } } return true; } export function checkExpectValue(actual: unknown, expected: JsonValue | ValueMatcher): boolean { if (isValueMatcherObject(expected)) { return applyMatcher(actual, expected); } return applyMatcher(actual, { equals: expected }); } export function checkValueMatcher( actual: unknown, matcher: undefined | ValueMatcher, options: { message?: string; path: string; phase: CheckFailure["phase"]; stringifyNonString?: boolean }, ): ExpectResult { if (matcher === undefined) return { failure: null, matched: true }; if (applyMatcher(actual, matcher, { stringifyNonString: options.stringifyNonString })) { return { failure: null, matched: true }; } return { failure: mismatchFailure( options.phase, options.path, matcher, actual, options.message ?? `${options.path} mismatch`, ), matched: false, }; } export function evaluateJsonPath(json: unknown, path: string): unknown { if (!path.startsWith("$.")) return undefined; const segments = path.slice(2).split("."); let current: unknown = json; for (const seg of segments) { const bracketMatch = /^(.+?)\[(\d+)\]$/.exec(seg); if (bracketMatch) { if (current === null || current === undefined) return undefined; current = (current as Record)[bracketMatch[1]!]; const idx = parseInt(bracketMatch[2]!, 10); if (!isArray(current) || idx >= current.length) return undefined; current = current[idx]; } else { if (current === null || current === undefined) return undefined; current = (current as Record)[seg]; } } return current; } export function isValueMatcherObject(value: unknown): value is ValueMatcher { return isPlainObject(value) && Object.keys(value).some((key) => MATCHER_KEY_SET.has(key)); } function compareNumber( actual: unknown, expected: number, compare: (actual: number, expected: number) => boolean, ): boolean { const value = Number(actual); return Number.isFinite(value) && compare(value, expected); } function isEmptyValue(value: unknown): boolean { return isNil(value) || value === "" || (isArray(value) && value.length === 0) || isEmptyObject(value); } function stringValue(actual: unknown, options: { stringifyNonString?: boolean }): string { if (!options.stringifyNonString || typeof actual === "string") return String(actual); if (actual !== null && typeof actual === "object") return JSON.stringify(actual); return String(actual); }