Code Monkey home page Code Monkey logo

feature-toggles's Introduction

feature-toggles

Simple implementation of feature toggles for JavaScript (also called feature flipping).

Features

This module encapsulates all calls to check if a certain feature (toggle) is enabled. Furthermore it provides the possibility to have computed feature toggle values in form of functions.

Simple toggle values

Example for using plain values:

// define toggles
var toggles = {foo: true, bar: false};

// load them into the module
var featureToggles = require('feature-toggles');
featureToggles.load(toggles);

// check if a feature is enabled
if (featureToggles.isFeatureEnabled('foo')) {
    // do something
}

Computed toggle values

For computed values provide a function for the value of a feature toggle. Whenever checking if the feature is enabled the provided function will be invoked. All arguments passed to the isFeatureEnabled() call except the first one are passed on to the function.

Example:

// define a computed toggle
var toggles = {
    foo: function(a, b) {
        return (a == 'a' && b == 'b');
     }
}

// load them into the module
var featureToggles = require('feature-toggles');
featureToggles.load(toggles);

// check if a feature is enabled and provide additional arguments
if (featureToggles.isFeatureEnabled('foo', 'a', 'b') {
    // do something
}

If the function throws an exception the isFeatureEnabled() call will return false to prevent runtime crashes.

Advanced examples (pseudo code)

Enabling a toggle based on an url parameter within an express app:

var toggles = {
    foo: function(request) {
        return request.param('enableFoo');
    }
}

var featureToggles = require('feature-toggles');
featureToggles.load(toggles);

app.get('/', function(request, response) {
    if (featureToggles.isFeatureEnabled('foo', request)) {
        // do something
    }
});

Enabling a toggle based on a certain date:

var toggles = {
    foo: function() {
        var date = new Date();
        return date.getDate() > 15;
    }
}

var featureToggles = require('feature-toggles');
featureToggles.load(toggles);

if (featureToggles.isFeatureEnabled('foo')) {
    // do something
}

Express integration

Use the middleware to easily toggle features per request. This way a special version of isFeatureEnabled() is exposed to all views (res.locals). Computed toggles will automatically receive the current request and response as arguments.

var toggles = {
    foo: function(request, response) {
        return request.param('enableFoo');
    }
}
var application = express();

application.use(featureToggles.middleware);
- if (isFeatureEnabled('foo'))
    Foo is enabled

Alternative modules

feature-toggles's People

Contributors

alexlawrence avatar gilbarbara avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.