Skip to content

Commit

Permalink
fix getVariants
Browse files Browse the repository at this point in the history
  • Loading branch information
tyiuhc committed Feb 12, 2025
1 parent 9937e44 commit aa9df1a
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions packages/experiment-tag/src/experiment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,19 +455,28 @@ export class DefaultWebExperimentClient implements WebExperimentClient {
if (webExperimentContext) this.setContext(webExperimentContext);

const allVariants = this.experimentClient.all();
const variants = Object.fromEntries(
Object.entries(allVariants).filter(
([key]) =>
!flagKeys ||
flagKeys.includes(key) ||
this.localFlagKeys.includes(key) ||
this.remoteFlagKeys.includes(key),
),

const isRelevantKey = (key: string) =>
!flagKeys ||
flagKeys.includes(key) ||
this.localFlagKeys.includes(key) ||
this.remoteFlagKeys.includes(key);

const variants = Object.keys(allVariants).reduce<Record<string, any>>(
(acc, key) => {
if (isRelevantKey(key)) acc[key] = allVariants[key];
return acc;
},
{},
);

this.setContext(existingContext);
return flagKeys
? Object.fromEntries(flagKeys.map((key) => [key, variants[key]]))

return flagKeys?.length
? flagKeys.reduce<Record<string, any>>((acc, key) => {
if (key in variants) acc[key] = variants[key];
return acc;
}, {})
: variants;
}

Expand Down

0 comments on commit aa9df1a

Please sign in to comment.