Skip to content

Instantly share code, notes, and snippets.

@lydemann
Last active August 26, 2018 10:10
Show Gist options
  • Save lydemann/2b54f431a3a1f1614519fceaaeb3a628 to your computer and use it in GitHub Desktop.
Save lydemann/2b54f431a3a1f1614519fceaaeb3a628 to your computer and use it in GitHub Desktop.
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