|
| 1 | +/** |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +/** |
| 7 | + * Checks if the actual argument matches the expected argument, supporting both exact matching and constraints. |
| 8 | + * |
| 9 | + * If the expected argument is a constraint object (all keys start with `$`), it evaluates the constraints. |
| 10 | + * Otherwise, it performs a recursive deep equality check, allowing nested constraints. |
| 11 | + * |
| 12 | + * @param expected The expected value or constraint object. |
| 13 | + * @param actual The actual value to check. |
| 14 | + * @returns True if the actual value matches the expected value or satisfies the constraints. |
| 15 | + */ |
| 16 | +export function matchesArgument(expected: any, actual: any): boolean { |
| 17 | + if (isConstraintObject(expected)) { |
| 18 | + return matchesConstraint(expected, actual); |
| 19 | + } |
| 20 | + |
| 21 | + return matchesRecursive(expected, actual); |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Evaluates a constraint object against an actual value. |
| 26 | + * Supports operators: |
| 27 | + * - `$pattern`: Regex match (string) |
| 28 | + * - `$contains`: Substring match (string) |
| 29 | + * - `$gt`, `$gte`, `$lt`, `$lte`: Numeric comparisons |
| 30 | + * - `$type`: Type assertion ("string", "number", "boolean", "array", "object", "null") |
| 31 | + * - `$any`: Presence check (always true if key exists) |
| 32 | + * |
| 33 | + * @param constraint The constraint object (e.g., { "$gt": 10 }). |
| 34 | + * @param actual The value to test. |
| 35 | + * @returns True if all constraints in the object are satisfied. |
| 36 | + */ |
| 37 | +function matchesConstraint(constraint: any, actual: any): boolean { |
| 38 | + for (const key of Object.keys(constraint)) { |
| 39 | + if (key === "$pattern") { |
| 40 | + if (typeof actual !== "string") { |
| 41 | + return false; |
| 42 | + } |
| 43 | + const pattern = new RegExp(constraint[key]); |
| 44 | + if (!pattern.test(actual)) { |
| 45 | + return false; |
| 46 | + } |
| 47 | + } else if (key === "$contains") { |
| 48 | + if (typeof actual !== "string") { |
| 49 | + return false; |
| 50 | + } |
| 51 | + if (!actual.includes(constraint[key])) { |
| 52 | + return false; |
| 53 | + } |
| 54 | + } else if (["$gt", "$gte", "$lt", "$lte"].includes(key)) { |
| 55 | + if (typeof actual !== "number") { |
| 56 | + return false; |
| 57 | + } |
| 58 | + const val = constraint[key]; |
| 59 | + if (key === "$gt" && !(actual > val)) return false; |
| 60 | + if (key === "$gte" && !(actual >= val)) return false; |
| 61 | + if (key === "$lt" && !(actual < val)) return false; |
| 62 | + if (key === "$lte" && !(actual <= val)) return false; |
| 63 | + } else if (key === "$type") { |
| 64 | + const type = constraint[key]; |
| 65 | + if (type === "array") { |
| 66 | + if (!Array.isArray(actual)) return false; |
| 67 | + } else if (type === "null") { |
| 68 | + if (actual !== null) return false; |
| 69 | + } else if (type === "object") { |
| 70 | + if ( |
| 71 | + typeof actual !== "object" || |
| 72 | + actual === null || |
| 73 | + Array.isArray(actual) |
| 74 | + ) |
| 75 | + return false; |
| 76 | + } else { |
| 77 | + if (typeof actual !== type) return false; |
| 78 | + } |
| 79 | + } else if (key === "$any") { |
| 80 | + // Always matches if present |
| 81 | + } |
| 82 | + // Future constraints will go here |
| 83 | + } |
| 84 | + return true; |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * Determines if an object is a constraint object. |
| 89 | + * An object is a constraint object if it is non-null, has at least one key, |
| 90 | + * and ALL its keys start with `$`. |
| 91 | + * |
| 92 | + * @param obj The object to check. |
| 93 | + * @returns True if strictly a constraint object. |
| 94 | + */ |
| 95 | +function isConstraintObject(obj: any): boolean { |
| 96 | + if (typeof obj !== "object" || obj === null) { |
| 97 | + return false; |
| 98 | + } |
| 99 | + const keys = Object.keys(obj); |
| 100 | + if (keys.length === 0) { |
| 101 | + return false; |
| 102 | + } |
| 103 | + return keys.every((key) => key.startsWith("$")); |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * Recursively checks equality between two values. |
| 108 | + * If values are objects or arrays, it recurses into them. |
| 109 | + * Crucially, it calls `matchesArgument` for children, enabling nested constraints. |
| 110 | + * |
| 111 | + * @param expected The expected structure. |
| 112 | + * @param actual The actual structure. |
| 113 | + * @returns True if structures match recursively. |
| 114 | + */ |
| 115 | +function matchesRecursive(expected: any, actual: any): boolean { |
| 116 | + if (expected === actual) { |
| 117 | + return true; |
| 118 | + } |
| 119 | + |
| 120 | + if ( |
| 121 | + expected === null || |
| 122 | + actual === null || |
| 123 | + typeof expected !== "object" || |
| 124 | + typeof actual !== "object" |
| 125 | + ) { |
| 126 | + return false; |
| 127 | + } |
| 128 | + |
| 129 | + const keys1 = Object.keys(expected); |
| 130 | + const keys2 = Object.keys(actual); |
| 131 | + |
| 132 | + if (keys1.length !== keys2.length) { |
| 133 | + return false; |
| 134 | + } |
| 135 | + |
| 136 | + for (const key of keys1) { |
| 137 | + if (!keys2.includes(key) || !matchesArgument(expected[key], actual[key])) { |
| 138 | + return false; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + return true; |
| 143 | +} |
0 commit comments