@@ -259,7 +259,7 @@ export function unflattenJSON(data: Record<string, any>): Record<string, any> {
259259 * @param {string } hashInput - The input string used to generate the hash.
260260 * @return {number } A number between 0 and 100000 derived from the hash of the input string.
261261 */
262- export async function hashInt ( hashInput : string ) : Promise < number > {
262+ export function hashInt ( hashInput : string ) : number {
263263 // 1. hash the key and the partial rollout attribute
264264 // 2. take 20 bits from the hash and divide by 2^20 - 1 to get a number between 0 and 1
265265 // 3. multiply by 100000 to get a number between 0 and 100000 and compare it to the threshold
@@ -344,11 +344,11 @@ export function evaluate(
344344 }
345345}
346346
347- async function evaluateRecursively (
347+ function evaluateRecursively (
348348 filter : RuleFilter ,
349349 context : Record < string , string > ,
350350 missingContextFieldsSet : Set < string > ,
351- ) : Promise < boolean > {
351+ ) : boolean {
352352 switch ( filter . type ) {
353353 case "constant" :
354354 return filter . value ;
@@ -369,38 +369,30 @@ async function evaluateRecursively(
369369 return false ;
370370 }
371371
372- const hashVal = await hashInt (
372+ const hashVal = hashInt (
373373 `${ filter . key } .${ context [ filter . partialRolloutAttribute ] } ` ,
374374 ) ;
375375
376376 return hashVal < filter . partialRolloutThreshold ;
377377 }
378- case "group" : {
379- const isAnd = filter . operator === "and" ;
380- let result = isAnd ;
381- for ( const current of filter . filters ) {
382- // short-circuit if we know the result already
383- // could be simplified to isAnd !== result, but this is more readable
384- if ( ( isAnd && ! result ) || ( ! isAnd && result ) ) {
385- return result ;
378+ case "group" :
379+ return filter . filters . reduce ( ( acc , current ) => {
380+ if ( filter . operator === "and" ) {
381+ return (
382+ acc &&
383+ evaluateRecursively ( current , context , missingContextFieldsSet )
384+ ) ;
386385 }
387-
388- const newRes = await evaluateRecursively (
389- current ,
390- context ,
391- missingContextFieldsSet ,
386+ return (
387+ acc || evaluateRecursively ( current , context , missingContextFieldsSet )
392388 ) ;
393-
394- result = isAnd ? result && newRes : result || newRes ;
395- }
396- return result ;
397- }
389+ } , filter . operator === "and" ) ;
398390 case "negation" :
399- return ! ( await evaluateRecursively (
391+ return ! evaluateRecursively (
400392 filter . filter ,
401393 context ,
402394 missingContextFieldsSet ,
403- ) ) ;
395+ ) ;
404396 default :
405397 return false ;
406398 }
@@ -442,18 +434,16 @@ export interface EvaluationResult<T extends RuleValue> {
442434 missingContextFields ?: string [ ] ;
443435}
444436
445- export async function evaluateFeatureRules < T extends RuleValue > ( {
437+ export function evaluateFeatureRules < T extends RuleValue > ( {
446438 context,
447439 featureKey,
448440 rules,
449- } : EvaluationParams < T > ) : Promise < EvaluationResult < T > > {
441+ } : EvaluationParams < T > ) : EvaluationResult < T > {
450442 const flatContext = flattenJSON ( context ) ;
451443 const missingContextFieldsSet = new Set < string > ( ) ;
452444
453- const ruleEvaluationResults = await Promise . all (
454- rules . map ( ( rule ) =>
455- evaluateRecursively ( rule . filter , flatContext , missingContextFieldsSet ) ,
456- ) ,
445+ const ruleEvaluationResults = rules . map ( ( rule ) =>
446+ evaluateRecursively ( rule . filter , flatContext , missingContextFieldsSet ) ,
457447 ) ;
458448
459449 const missingContextFields = Array . from ( missingContextFieldsSet ) ;
0 commit comments