Skip to content
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
23 changes: 21 additions & 2 deletions flagsmith-engine/segments/models.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import semver from 'semver';

import { FeatureStateModel } from '../features/models';
import { getCastingFunction as getCastingFunction } from '../utils';
import {
Expand All @@ -8,6 +10,7 @@ import {
REGEX,
CONDITION_OPERATORS
} from './constants';
import { isSemver } from './util';

export const all = (iterable: Array<any>) => iterable.filter(e => !!e).length === iterable.length;
export const any = (iterable: Array<any>) => iterable.filter(e => !!e).length > 0;
Expand All @@ -25,6 +28,19 @@ export const matchingFunctions = {
otherValue.includes(thisValue),
};

export const semverMatchingFunction = {
...matchingFunctions,
[CONDITION_OPERATORS.EQUAL]: (thisValue: any, otherValue: any) => semver.eq(thisValue, otherValue),
[CONDITION_OPERATORS.GREATER_THAN]: (thisValue: any, otherValue: any) => semver.gt(otherValue, thisValue),
[CONDITION_OPERATORS.GREATER_THAN_INCLUSIVE]: (thisValue: any, otherValue: any) =>
semver.gte(otherValue, thisValue),
[CONDITION_OPERATORS.LESS_THAN]: (thisValue: any, otherValue: any) => semver.gt(thisValue, otherValue),
[CONDITION_OPERATORS.LESS_THAN_INCLUSIVE]: (thisValue: any, otherValue: any) =>
semver.gte(thisValue, otherValue),
}

export const getMatchingFunctions = (semver: boolean) => (semver ? semverMatchingFunction : matchingFunctions);

export class SegmentConditionModel {
EXCEPTION_OPERATOR_METHODS: { [key: string]: string } = {
[NOT_CONTAINS]: 'evaluateNotContains',
Expand Down Expand Up @@ -59,9 +75,12 @@ export class SegmentConditionModel {

const defaultFunction = (x: any, y: any) => false;

const matchingFunction = matchingFunctions[this.operator] || defaultFunction;
const matchingFunctionSet = getMatchingFunctions(isSemver(this.value));
const matchingFunction = matchingFunctionSet[this.operator] || defaultFunction;

const traitType = isSemver(this.value) ? 'semver' : typeof traitValue;
const castToTypeOfTraitValue = getCastingFunction(traitType);

const castToTypeOfTraitValue = getCastingFunction(traitValue);
return matchingFunction(castToTypeOfTraitValue(this.value), traitValue);
}
}
Expand Down
8 changes: 8 additions & 0 deletions flagsmith-engine/segments/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,11 @@ export function buildSegmentModel(segmentModelJSON: any): SegmentModel {

return model;
}

export function isSemver(value: any) {
return typeof value == 'string' && value.endsWith(':semver');
}

export function removeSemverSuffix(value: string) {
return value.replace(':semver', '');
}
8 changes: 5 additions & 3 deletions flagsmith-engine/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { FeatureSegment } from "../features/models";
import { removeSemverSuffix } from "../segments/util";

export function getCastingFunction(input: any): CallableFunction {
switch (typeof input) {
export function getCastingFunction(traitType: 'boolean' | 'string' | 'number' | 'semver' | any): CallableFunction {
switch (traitType) {
case 'boolean':
return (x: any) => !['False', 'false'].includes(x);
case 'number':
return (x: any) => parseFloat(x);
case 'semver':
return (x: any) => removeSemverSuffix(x);
default:
return (x: any) => String(x);
}
Expand Down
165 changes: 99 additions & 66 deletions package-lock.json

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

Loading