-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathflexible-rollout-strategy.ts
44 lines (38 loc) · 1.39 KB
/
flexible-rollout-strategy.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { Strategy } from './strategy';
import { Context } from '../context';
import { normalizedStrategyValue } from './util';
import { resolveContextValue } from '../helpers';
const STICKINESS = {
default: 'default',
random: 'random',
};
export default class FlexibleRolloutStrategy extends Strategy {
private randomGenerator: Function = () => `${Math.round(Math.random() * 10000) + 1}`;
constructor(randomGenerator?: Function) {
super('flexibleRollout');
if (randomGenerator) {
this.randomGenerator = randomGenerator;
}
}
resolveStickiness(stickiness: string, context: Context): any {
switch (stickiness) {
case STICKINESS.default:
return context.userId || context.sessionId || this.randomGenerator();
case STICKINESS.random:
return this.randomGenerator();
default:
return resolveContextValue(context, stickiness);
}
}
isEnabled(parameters: any, context: Context) {
const groupId = parameters.groupId || context.featureToggle || '';
const percentage = Number(parameters.rollout);
const stickiness: string = parameters.stickiness || STICKINESS.default;
const stickinessId = this.resolveStickiness(stickiness, context);
if (!stickinessId) {
return false;
}
const normalizedUserId = normalizedStrategyValue(stickinessId, groupId);
return percentage > 0 && normalizedUserId <= percentage;
}
}