A simple feature flag implementation
A hash is provided for the current environment in the application setup.
type Features = { [FLAG_NAME]: Boolean | Function };
const features: Features = (NODE_ENV === 'production') ? productionFeatures : testingFeatures;
export featureFlag = setUpFeatureFlags(features, platform);
Extra arguments passed into setUpFeatureFlags
will be passed into functions on the feature hash.
Features is a hash of bools and functions that return bools.
const productionFeatures = {
FOO_FLAG: true,
BAR_FLAG: function (platform) {
return platform === 'WEB';
}
};
type OnOption = any;
type OffOption = ?any;
export Foo = featureFlag(FOO_FLAG, OnOption, OffOption);
This works with reducers
, sagas
, components
, or anything else.
The output can be exported by modules at initilization time or inside modules at run time.
const setUpFeatureFlags = function (features: Features, ...args) {
return function featureFlag(flag: string, onOption, offOption) {
if (typeof features[flag] === 'function') {
return (features[flag](...args)) ? onOption : offOption;
}
return (features[flag]) ? onOption : offOption
}
}
I would also like to be able to enable/disable individual feature flags without changing the code by setting env vars. So maybe the productionFeatures and testingFeatures objects would just be defaults that can be overridden.