Skip to content

Instantly share code, notes, and snippets.

@lydemann
Last active August 26, 2018 10:10
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Feature toggling service that enables split testing
import seedrandom from 'seedrandom';
export type featureTypes = {
'killer-feature': number | boolean;
};
@Injectable({
providedIn: 'root'
})
export class FeatureToggleService {
public features: featureTypes;
public userId: string = 'some-user-id';
constructor() {}
public isFeatureEnabled(feautureName: keyof featureTypes): boolean {
const featureValue = this.features[feautureName];
if (Number.isInteger(featureValue as number)) {
seedrandom(this.userId);
const isEnabled = Math.floor(Math.random() * 100) <= featureValue;
return isEnabled;
} else {
return featureValue as boolean;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment