Skip to content

Commit d8e469a

Browse files
authored
fix: Allow for negation of segment match clauses. (#237)
1 parent e57716f commit d8e469a

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

packages/shared/sdk-server/__tests__/evaluation/Evaluator.segments.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,20 @@ describe('when evaluating user equivalent contexts for segments', () => {
110110
expect(res.detail.value).toBe(false);
111111
});
112112

113+
it('can negate a segment match op', async () => {
114+
const segment = {
115+
key: 'test',
116+
included: ['foo'],
117+
version: 1,
118+
};
119+
const evaluator = new Evaluator(basicPlatform, new TestQueries({ segments: [segment] }));
120+
const flag = makeFlagWithSegmentMatch(segment);
121+
flag.rules[0].clauses![0].negate = true;
122+
const user = { key: 'bar' };
123+
const res = await evaluator.evaluate(flag, Context.fromLDContext(user));
124+
expect(res.detail.value).toBe(true);
125+
});
126+
113127
it.each([basicUser, basicSingleKindUser, basicMultiKindUser])(
114128
'matches segment with user who is both included and excluded',
115129
async (context) => {

packages/shared/sdk-server/src/evaluation/Evaluator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import ErrorKinds from './ErrorKinds';
1717
import EvalResult from './EvalResult';
1818
import evalTargets from './evalTargets';
1919
import makeBigSegmentRef from './makeBigSegmentRef';
20-
import matchClauseWithoutSegmentOperations from './matchClause';
20+
import matchClauseWithoutSegmentOperations, { maybeNegate } from './matchClause';
2121
import matchSegmentTargets from './matchSegmentTargets';
2222
import { Queries } from './Queries';
2323
import Reasons from './Reasons';
@@ -296,7 +296,7 @@ export default class Evaluator {
296296
return new MatchError(errorResult);
297297
}
298298

299-
return new Match(match);
299+
return new Match(maybeNegate(clause, match));
300300
}
301301
// This is after segment matching, which does not use the reference.
302302
if (!clause.attributeReference.isValid) {

packages/shared/sdk-server/src/evaluation/matchClause.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Context } from '@launchdarkly/js-sdk-common';
33
import { Clause } from './data/Clause';
44
import Operators from './Operations';
55

6-
function maybeNegate(clause: Clause, value: boolean): boolean {
6+
export function maybeNegate(clause: Clause, value: boolean): boolean {
77
if (clause.negate) {
88
return !value;
99
}

0 commit comments

Comments
 (0)