Skip to content

Commit d4e5cd5

Browse files
Add evaluateDefaultTreatment function to handle default treatment evaluation when no target is provided
1 parent 12ee171 commit d4e5cd5

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

src/evaluator/index.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { engineParser } from './Engine';
22
import { thenable } from '../utils/promise/thenable';
3-
import { EXCEPTION, SPLIT_NOT_FOUND } from '../utils/labels';
3+
import { EXCEPTION, NO_CONDITION_MATCH, SPLIT_NOT_FOUND } from '../utils/labels';
44
import { CONTROL } from '../utils/constants';
55
import { ISplit, MaybeThenable } from '../dtos/types';
66
import { IStorageAsync, IStorageSync } from '../storages/types';
@@ -207,3 +207,40 @@ function getEvaluations(
207207

208208
return thenables.length > 0 ? Promise.all(thenables).then(() => result) : result;
209209
}
210+
211+
export function evaluateDefaultTreatment(
212+
splitName: string,
213+
storage: IStorageSync | IStorageAsync,
214+
): MaybeThenable<IEvaluationResult> {
215+
let parsedSplit;
216+
217+
try {
218+
parsedSplit = storage.splits.getSplit(splitName);
219+
} catch (e) {
220+
// Exception on sync `getSplit` storage. Not possible ATM with InMemory and InLocal storages.
221+
return treatmentException;
222+
}
223+
224+
return thenable(parsedSplit) ?
225+
parsedSplit.then(getDefaultTreatment).catch(() => treatmentException) :
226+
getDefaultTreatment(parsedSplit);
227+
}
228+
229+
function getDefaultTreatment(
230+
splitJSON: ISplit | null,
231+
): MaybeThenable<IEvaluationResult> {
232+
if (splitJSON) {
233+
return {
234+
treatment: splitJSON.defaultTreatment,
235+
label: NO_CONDITION_MATCH, // "default rule"
236+
config: splitJSON.configurations && splitJSON.configurations[splitJSON.defaultTreatment] || null,
237+
changeNumber: splitJSON.changeNumber
238+
};
239+
}
240+
241+
return {
242+
treatment: CONTROL,
243+
label: SPLIT_NOT_FOUND,
244+
config: null
245+
};
246+
}

0 commit comments

Comments
 (0)