Skip to content

Instantly share code, notes, and snippets.

@zodman
Created February 26, 2020 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zodman/f0b08795cbe18aacfb56cc92b20a93f2 to your computer and use it in GitHub Desktop.
Save zodman/f0b08795cbe18aacfb56cc92b20a93f2 to your computer and use it in GitHub Desktop.
Simple featureflag in nodejs
/*
* Simple featureFlag
* */
const YAML = require('yaml');
const fs = require("fs");
class FeatureFlag {
constructor() {
let filepath = 'featureflag.yaml';
let fileContent= fs.readFileSync(filepath, 'utf8');
let yamlLoad = YAML.parse(fileContent);
//console.log(JSON.stringify(yaml_load));
this.config = yamlLoad;
}
isFeatureEnabled(component, featureFlag) {
let isEnabled = this.isComponentEnabled(component)
if(!isEnabled) {
return false;
}
return this.checkFeature(component, featureFlag);
}
checkFeature(component, featureFlag) {
let comp = this.getComponent(component)
//console.log("checkFeature", comp.featureflags[featureFlag])
return comp.featureflags[featureFlag].enabled
}
isComponentEnabled(component) {
return this.checkComponent(component);
}
getComponent(component) {
let comp = {};
this.config.Components.forEach(e =>{
if( component in e) {
comp = e[component];
return
}
})
return comp
}
checkComponent(component) {
let e = this.getComponent(component);
try {
return e.enabled;
} catch (e) {
if( e instanceof TypeError) {
throw Error("component \""+ component + "\" missing in yaml");
}
}
}
}
module.exports=FeatureFlag
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment