npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

knobz

v0.4.0

Published

A feature flags/flips/toggles JavaScript library with JSON Predicates support.

Downloads

2

Readme

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