Git Product home page Git Product logo

knobz's Introduction

knobz

npm version Build Status Dependency Status

knobz lets you declare and manage feature flags in your JavaScript/Node.js application using JSON Predicates to declarative configure whether features should be enabled.

Quick Start

Install the module using npm:

npm install knobz --save

Usage example in Node.js:

const knobz = require('knobz');

knobz.configure({
  features: [{
    id: 'user_account_lockout',
    description: 'Failed sign-in attempts will cause a user account to be locked.',
    owner: 'Ismael Rivera <[email protected]>',
    enabled: true
  }]
});

if (knobz.isFeatureEnabled('user_account_lockout')) {
  // user account should be locked
}

Phased rollouts

If you're rolling out a new feature, you might want to verify the feature as expected by slowly enabling it for a percentage of your users.

knobz.configure({
  features: [{
    id: 'one_click_checkout_beta',
    description: 'Phased rollout of feature that allows to make online purchases with a single click.',
    owner: 'Ismael Rivera <[email protected]>',
    percentage: 0.2, // 20%
    percentagePath: '/email'
  }]
});

The algorithm for determining whether the feature is enabled for a given context is as follows:

djb2(String(valueAtPath)) % 100 < (feature.percentage * 100);

Define feature criteria using JSON Predicate

Features may be enabled only if a given context satisfies its criteria, defined as a JSON Predicate.

The following example enables the feature to a subset of users based on their job title:

knobz.configure({
  features: [{
    id: 'cool_new_email_for_managers',
    criteria: {
      op: 'contains',
      path: '/jobTitle',
      value: 'Manager',
      ignore_case: true
    }
  }]
});

API

configure(options)

Configure knobz using any of the following options:

  • features: can be either an array of features or a function to load features dynamically. If a function is passed, it must return a Promise that resolves to an array of features.
  • reloadInterval: interval in ms used by knobz to reload features when configured with a function to load features dynamically.

getFeatures(context)

Return object with all features IDs as properties, and true/false as values, indicating whether they are enabled for a given context.

isFeatureEnabled(featureId, context)

Return true/false if a feature is enabled for a given context.

reload()

Force a reload by calling the configured function to load features dynamically.

Events

knobz is also an event emitter which provides an on method allowing your application to listen for any of the following events.

knobz.on('check', fn)

Triggers whenever the feature is checked with a given context. The listener is called with featureId, the context used, and the boolean enabled indicating whether the feature is enabled.

{
  enabled: <Boolean>,
  featureId: <String>,
  context: <Object>
}

knobz.on('reload', fn)

Triggers whenever the features are reloaded. The listener is called with an object including the features array.

{
  features: <Object[]>
}

knobz.on('reload:error', fn)

Triggers whenever the function to reload features throws an error. The event handler is called with the error.

Tests

To run the test suite, first install the dependencies, then run npm test:

npm install
npm test

License

MIT

knobz's People

Contributors

ismriv avatar

Stargazers

 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.