This Gist and the associated files were created to support an article on Feature Flags (to be linked)
Last active
January 11, 2021 22:21
-
-
Save JoshuaTheMiller/4c9b21ecfa6bd7efe3acbbfaf4c20c11 to your computer and use it in GitHub Desktop.
Poor Man's Feature Toggle Article
This file contains hidden or 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
function buildForecast(location) { | |
return `The forecast for ${location} is "cloudy"`; | |
} | |
function appendAmountOfRain(forecast) { | |
return `${forecast} with 3cm of rain`; | |
} | |
class weatherForecasterService{ | |
generateWeatherForecast(location) { | |
const forecast = buildForecast(location); | |
// We have been asked to insert appendAmountOfRain(...) here | |
return forecast; | |
} | |
} | |
function useWeatherForecaster(weatherForecaster) { | |
console.log(weatherForecaster.generateWeatherForecast("Minnesota")) | |
} | |
const weatherForecaster = new weatherForecasterService(); | |
useWeatherForecaster(weatherForecaster); |
This file contains hidden or 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
const weatherForecasterService = { | |
generateWeatherForecast: function (location) { | |
let forecast = buildForecast(location); | |
if(process.env.CURRENT_ENVIRONMENT === "nonprod") { | |
forecast = appendAmountOfRain(forecast) | |
} | |
return forecast; | |
} | |
} |
This file contains hidden or 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
const features = getFeatureFlagServiceFromSomewhere() | |
class weatherForecasterService{ | |
generateWeatherForecast(location) { | |
let forecast = buildForecast(location); | |
if(features.isEnabled("amount-of-rain")) { | |
forecast = appendAmountOfRain(forecast) | |
} | |
return forecast; | |
} | |
} |
This file contains hidden or 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
function environemntIsNonProduction() { | |
return process.env.CURRENT_ENVIRONMENT === "nonprod"; | |
} | |
const featureServiceObject = { | |
isEnabled: function(flagName) { | |
return environemntIsNonProduction() | |
} | |
} | |
function getFeatureFlagServiceFromSomewhere() { | |
return featureServiceObject; | |
} |
This file contains hidden or 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
const featureDecisions = getFeatureDecisionServiceFromSomewhere() | |
class weatherForecasterService{ | |
generateWeatherForecast(location) { | |
let forecast = buildForecast(location); | |
if(featureDecisions.appendAmountOfRainToForecast()) { | |
forecast = appendAmountOfRain(forecast) | |
} | |
return forecast; | |
} | |
} |
This file contains hidden or 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
const features = getFeatureFlagServiceFromSomewhere() | |
const featureDecisionObject = { | |
appendAmountOfRainToForecast: function() { | |
return features.isEnabled("amount-of-rain") | |
} | |
} | |
function getFeatureDecisionServiceFromSomewhere() { | |
return featureServiceObject | |
} |
This file contains hidden or 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
class weatherForecasterService{ | |
constructor(options) { | |
this.options = options ?? { | |
appendAmountOfRainToForecast: false | |
} | |
} | |
generateWeatherForecast(location) { | |
let forecast = buildForecast(location); | |
if(this.options.appendAmountOfRainToForecast) { | |
forecast = appendAmountOfRain(forecast) | |
} | |
return forecast; | |
} | |
} | |
class featureAwareWeatherForecasterServiceFactory{ | |
build() { | |
const options = { | |
appendAmountOfRainToForecast: featureDecisions.appendAmountOfRainToForecast() | |
} | |
return new weatherForecasterService(options); | |
} | |
} | |
const weatherForecaster = new featureAwareWeatherForecasterServiceFactory().build(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment