Last active
August 26, 2018 10:10
-
-
Save lydemann/2b54f431a3a1f1614519fceaaeb3a628 to your computer and use it in GitHub Desktop.
Feature toggling service that enables split testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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