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

feature-release

v0.0.4

Published

A simple feature flag management without hassle

Downloads

14

Readme

feature-release

A simple feature flag management without hassle

  • Release by percentage
  • Release by individual targets

How to use?

Simple usage by percentage release

1. Initialize feature flags

/* my-feature-release.js */
const { initFeatureRelease } = require('feature-release');
// or import { initFeatureRelease } from 'feature-release';

const featureRelease = initFeatureRelease({
  myFirstFeatureFlag: {
    releaseByPercentage: 30,
  },
  mySecondFeatureFlag: {
    releaseByPercentage: 100,
  },
});

module.exports = {
  featureRelease,
};

2. Use the feature flags with unique IDs

const { featureRelease } = require('./my-feature-release');

function newFeatureImplementation1(userID) {
  // The same userID will return the same result consistently.
  // For example, if userID, '12345', started to return true at 30%, it will
  // return true all from 30% to 100%.
  if (featureRelease.isEnabled('myFirstFeatureFlag', userID)) {
    return 'Oh! 30% of users will see this message!';
  } else {
    return 'Oh! 70% of users will see this message!';
  }
}

function newFeatureImplementation2(userID) {
  if (featureRelease.isEnabled('mySecondFeatureFlag', userID)) {
    return 'Oh! all users will see this message!';
  } else {
    return 'Oh! No user will see this message because you released 100%!';
  }
}

Simple usage by individual targets

1. Initialize feature flags

/* my-feature-release.js */
const { initFeatureRelease } = require('feature-release');
// or import { initFeatureRelease } from 'feature-release';

const featureRelease = initFeatureRelease({
  myFirstFeatureFlag: {
    individualTargets: {
      10001: true,
      10002: true,
    },
  },
  mySecondFeatureFlag: {
    individualTargets: {
      '[email protected]': true,
      '[email protected]': true,
    },
  },
});

module.exports = {
  featureRelease,
};

2. Use the feature flags with unique IDs

const { featureRelease } = require('./my-feature-release');

function newFeatureImplementation1(userID) {
  if (featureRelease.isEnabled('myFirstFeatureFlag', userID)) {
    return 'Oh! the userID, 10001 and 10002, will see this message!';
  } else {
    return 'Oh! all other users see this message!';
  }
}

function newFeatureImplementation2(email) {
  if (featureRelease.isEnabled('mySecondFeatureFlag', email)) {
    return 'Oh! the emails, [email protected] and [email protected], will see this message!';
  } else {
    return 'Oh! all other users see this message!';
  }
}

Simple usage by both percentage and targets

1. Initialize feature flags

/* my-feature-release.js */
const { initFeatureRelease } = require('feature-release');
// or import { initFeatureRelease } from 'feature-release';

const featureRelease = initFeatureRelease({
  myFirstFeatureFlag: {
    releaseByPercentage: 30,
    individualTargets: {
      10001: true,
      10002: false,
    },
  },
  mySecondFeatureFlag: {
    individualTargets: {
      '[email protected]': false,
      '[email protected]': false,
    },
    releaseByPercentage: 100,
  },
});

module.exports = {
  featureRelease,
};

2. Use the feature flags with unique IDs

const { featureRelease } = require('./my-feature-release');

function newFeatureImplementation1(userID) {
  if (featureRelease.isEnabled('myFirstFeatureFlag', userID)) {
    return '1) the userID, 10001, will see this message. 2) 30% of users will see this message, too!';
  } else {
    return '1) the userID, 10002, will see this message. 2) 70% of users will see this message, too!';
  }
}

function newFeatureImplementation2(email) {
  if (featureRelease.isEnabled('mySecondFeatureFlag', email)) {
    return 'All users will see this message except [email protected] and [email protected]';
  } else {
    return '[email protected] and [email protected], will see this message even if it is released 100%';
  }
}

Advanced usage

Namespace

The namespace acts like an independent feature flag. However, it's convenient if you like to organize different subcategories under the same feature flag name.

1. Initialize feature flags

/* my-feature-release.js */
const { initFeatureRelease } = require('feature-release');
// or import { initFeatureRelease } from 'feature-release';

const featureRelease = initFeatureRelease({
  myFirstFeatureFlag: {
    'prod-west1': {
      releaseByPercentage: 30,
      individualTargets: {
        10001: true,
      },
    },
    'prod-east1': {
      releaseByPercentage: 40,
      individualTargets: {
        10002: true,
      },
    },
  },
});

module.exports = {
  featureRelease,
};

2. Use the feature flags with unique IDs and the namespace

const { featureRelease } = require('./my-feature-release');

function newFeatureImplementation1(userID, environment, region) {
  if (featureRelease.isEnabled('myFirstFeatureFlag', userID, `${environment}-${region}`)) {
    return 'Oh! 30% of users and userID(10001) in prod-west1 will see this message while 40% of users and userID(10002) in prod-east1 see this message';
  } else {
    return 'Oh! other users will see this message!';
  }
}

Remote configuration

You can also pull the configuration from a remote server. There is also an option that pulls the configuration periodically. In this way, you are able to change the configuration remotely without restarting your application.

// https://example.com/remote-config.json
{
  "myFirstFeatureFlag": {
    "individualTargets": {
      "10001": true,
      "10002": true
    }
  },
  "mySecondFeatureFlag": {
    "individualTargets": {
      "[email protected]": true,
      "[email protected]": true
    }
  }
}
/* my-feature-release.js */
const { initFeatureReleaseAsync } = require('feature-release');
// or import { initFeatureReleaseAsync } from 'feature-release';

let featureRelease = undefined;

async function getFeatureReleaseInstance() {
  if(!featureRelease) {
    featureRelease = await initFeatureReleaseAsync({
      hostedConfigUrl: 'https://example.com/remote-config.json',
      // enableConfigPolling: false, // Optional. Default to false. Pulls the configuration from the `hostedConfigUrl` every `pollingIntervalInSeconds` seconds.
      // pollingIntervalInSeconds: 60, // Optional. Default to 60 seconds.
    });
    return featureRelease;
  }

  return featureRelease;
}

module.exports = {
  getFeatureReleaseInstance,
};