* Evaluate a quantifier expression over a list.
(
path: TraversalPath<any, any, any>,
quantifier: {
quantifier: "ALL" | "ANY" | "NONE" | "SINGLE";
variable: string;
condition: Condition;
},
list: any[],
context?: QueryContext,
)
| 1993 | * Evaluate a quantifier expression over a list. |
| 1994 | */ |
| 1995 | function evaluateQuantifier( |
| 1996 | path: TraversalPath<any, any, any>, |
| 1997 | quantifier: { |
| 1998 | quantifier: "ALL" | "ANY" | "NONE" | "SINGLE"; |
| 1999 | variable: string; |
| 2000 | condition: Condition; |
| 2001 | }, |
| 2002 | list: any[], |
| 2003 | context?: QueryContext, |
| 2004 | ): boolean { |
| 2005 | let satisfyCount = 0; |
| 2006 | |
| 2007 | for (const element of list) { |
| 2008 | // Create a temporary path binding for the quantifier variable |
| 2009 | const quantifierBinding = { |
| 2010 | label: "ComprehensionElement", |
| 2011 | value: element, |
| 2012 | }; |
| 2013 | |
| 2014 | // Create a new path with the quantifier variable bound |
| 2015 | const boundPath = new TraversalPath(path, quantifierBinding as any, [quantifier.variable]); |
| 2016 | |
| 2017 | // Evaluate the condition for this element |
| 2018 | const satisfies = evaluateCondition(boundPath, quantifier.condition, context); |
| 2019 | |
| 2020 | if (satisfies) { |
| 2021 | satisfyCount++; |
| 2022 | } |
| 2023 | |
| 2024 | // Early exit optimizations |
| 2025 | switch (quantifier.quantifier) { |
| 2026 | case "ALL": |
| 2027 | // If any element doesn't satisfy, return false immediately |
| 2028 | if (!satisfies) { |
| 2029 | return false; |
| 2030 | } |
| 2031 | break; |
| 2032 | case "ANY": |
| 2033 | // If any element satisfies, return true immediately |
| 2034 | if (satisfies) { |
| 2035 | return true; |
| 2036 | } |
| 2037 | break; |
| 2038 | case "NONE": |
| 2039 | // If any element satisfies, return false immediately |
| 2040 | if (satisfies) { |
| 2041 | return false; |
| 2042 | } |
| 2043 | break; |
| 2044 | case "SINGLE": |
| 2045 | // If more than one element satisfies, return false immediately |
| 2046 | if (satisfyCount > 1) { |
| 2047 | return false; |
| 2048 | } |
| 2049 | break; |
| 2050 | } |
| 2051 | } |
| 2052 |
no test coverage detected