Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/1145/is set is not set segment operators #75

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions flagsmith-engine/segments/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export const NOT_CONTAINS = 'NOT_CONTAINS';
export const NOT_EQUAL = 'NOT_EQUAL';
export const REGEX = 'REGEX';
export const PERCENTAGE_SPLIT = 'PERCENTAGE_SPLIT';
export const IS_SET = 'IS_SET';
export const IS_NOT_SET = 'IS_NOT_SET';
export const MODULO = 'MODULO';

export const CONDITION_OPERATORS = {
Expand All @@ -29,5 +31,7 @@ export const CONDITION_OPERATORS = {
NOT_EQUAL,
REGEX,
PERCENTAGE_SPLIT,
IS_SET,
IS_NOT_SET,
MODULO
};
16 changes: 10 additions & 6 deletions flagsmith-engine/segments/evaluators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { EnvironmentModel } from '../environments/models';
import { IdentityModel } from '../identities/models';
import { TraitModel } from '../identities/traits/models';
import { getHashedPercentateForObjIds } from '../utils/hashing';
import { PERCENTAGE_SPLIT } from './constants';
import { PERCENTAGE_SPLIT, IS_SET, IS_NOT_SET } from './constants';
import { SegmentConditionModel, SegmentModel, SegmentRuleModel } from './models';

export function getIdentitySegments(
Expand Down Expand Up @@ -55,18 +55,22 @@ function traitsMatchSegmentRule(
);
}

function traitsMatchSegmentCondition(
export function traitsMatchSegmentCondition(
identityTraits: TraitModel[],
condition: SegmentConditionModel,
segmentId: number | string,
identityId: number | string
): boolean {
if (condition.operator == PERCENTAGE_SPLIT) {
return getHashedPercentateForObjIds([segmentId, identityId]) <= parseFloat(condition.value);
return getHashedPercentateForObjIds([segmentId, identityId]) <= parseFloat(String(condition.value));
}

const traits = identityTraits.filter(t => t.traitKey === condition.property_);
const trait = traits.length > 0 ? traits[0] : undefined;

EdsnLoor marked this conversation as resolved.
Show resolved Hide resolved
if (condition.operator === IS_SET ) {
return !!trait;
} else if (condition.operator === IS_NOT_SET){
return trait == undefined;
}
return trait ? condition.matchesTraitValue(trait.traitValue) : false;
}

}
14 changes: 7 additions & 7 deletions flagsmith-engine/segments/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ export class SegmentConditionModel {
};

operator: string;
value: string;
property_: string | undefined;
value: string | null | undefined;
property_: string | null | undefined;

constructor(operator: string, value: string, property?: string) {
constructor(operator: string, value?: string | null | undefined, property?: string | null | undefined) {
this.operator = operator;
this.value = value;
this.property_ = property;
Expand All @@ -65,14 +65,14 @@ export class SegmentConditionModel {
return !traitValue.includes(this.value);
},
evaluateRegex: (traitValue: any) => {
return !!traitValue.match(new RegExp(this.value));
return !!this.value && !!traitValue.match(new RegExp(this.value));
},
evaluateModulo: (traitValue: any) => {
if (isNaN(parseFloat(traitValue))) {
if (isNaN(parseFloat(traitValue)) || !this.value) {
return false
}
const myArray = (this.value).split("|");
let [divisor, reminder] = [parseFloat(myArray[0]), parseFloat(myArray[1])];
const parts = (this.value).split("|");
const [divisor, reminder] = [parseFloat(parts[0]), parseFloat(parts[1])];
return traitValue % divisor === reminder
}
};
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions tests/engine/unit/segments/segment_evaluators.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {
CONDITION_OPERATORS,
} from '../../../../flagsmith-engine/segments/constants';
import {
SegmentConditionModel,
} from '../../../../flagsmith-engine/segments/models';
import {traitsMatchSegmentCondition} from "../../../../flagsmith-engine/segments/evaluators";
import {TraitModel} from "../../../../flagsmith-engine";

let traitExistenceTestCases: [string, string | null | undefined, string | null | undefined, TraitModel [],boolean][] = [
[CONDITION_OPERATORS.IS_SET,'foo', null,[] , false],
[CONDITION_OPERATORS.IS_SET, 'foo',undefined , [new TraitModel('foo','bar')], true],
[CONDITION_OPERATORS.IS_SET, 'foo',undefined , [new TraitModel('foo','bar'), new TraitModel('fooBaz','baz')], true],
[CONDITION_OPERATORS.IS_NOT_SET, 'foo', undefined, [], true],
[CONDITION_OPERATORS.IS_NOT_SET, 'foo', null, [new TraitModel('foo','bar')], false],
[CONDITION_OPERATORS.IS_NOT_SET, 'foo', null, [new TraitModel('foo','bar'), new TraitModel('fooBaz','baz')], false]
];

test('test_traits_match_segment_condition_for_trait_existence_operators', () => {
for (const testCase of traitExistenceTestCases) {
const [operator, conditionProperty, conditionValue, traits, expectedResult] = testCase
let segmentModel = new SegmentConditionModel(operator, conditionValue, conditionProperty)
expect(
traitsMatchSegmentCondition (traits, segmentModel, 'any','any')
).toBe(expectedResult);
}
});