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

@reliance/feature-flags

v0.1.2

Published

A package for determining feature flag status based on environment variables or flag objects.

Downloads

19

Readme

Feature Flag Management Documentation

Overview

This module provides a framework for managing feature flags in a JavaScript application. It allows you to define feature flags as predicate functions that return boolean values, enabling conditional behavior based on the flags.

Functions

isFeatureEnabled(feature, params = {})

Description:

Checks if a feature is enabled by evaluating its corresponding predicate function. If the function returns a boolean value, that value is used to determine the feature's state. If the function does not return a boolean or throws an error, the function defaults to false. Additionally, it checks for an environment variable with the feature name, which can override the predicate.

Parameters:

  • feature (string): The name of the feature to check.
  • params (object, optional): Parameters to pass to the feature's predicate function.

Returns:

  • boolean: true if the feature is enabled, false otherwise.

Usage Example:

const { isFeatureEnabled } = require("./isFeatureEnabled");

const isEnabled = isFeatureEnabled("newDashboard", { userId: 12345 });
if (isEnabled) {
  // Enable the new dashboard feature
}

Notes:

  • If the predicate function for the feature does not return a boolean value, a warning is logged, and the function defaults to returning false.
  • If the feature is not defined as a predicate, the function will check the corresponding environment variable (e.g., process.env.newDashboard).

setFeature(featureDefinitions)

Description:

Registers a set of feature flags, each associated with a predicate function. The predicates should return boolean values, determining whether the corresponding feature is enabled or not.

Parameters:

  • featureDefinitions (object): An object where each key is a feature name and each value is a predicate function.

Throws:

  • PredicateNotAFunctionError: Thrown if any value in featureDefinitions is not a function.
  • InvalidFeatureDefinition: Thrown if featureDefinitions is not an object or is null.

Usage Example:

const { setFeature } = require("./setFeature");

setFeature({
  newDashboard: ({ userId }) => userId === 12345,
  betaFeature: () => Math.random() > 0.5,
});

Notes:

  • Ensure that all predicate functions return a boolean value.
  • This function should be called during the application startup to register all necessary feature flags.

Error Classes

PredicateNotAFunctionError

Description:

An error thrown when a feature definition is attempted with a non-function predicate. This ensures that only valid functions are used to define feature flags.

Usage Example:

throw new PredicateNotAFunctionError(featureName);

Notes:

  • The error message will include the name of the feature that caused the error.

InvalidFeatureDefinition

Description:

An error thrown when the setFeature function is called with an invalid featureDefinitions object. This ensures that the input to setFeature is always a valid object.

Usage Example:

throw new InvalidFeatureDefinition();

Notes:

  • This error is thrown if featureDefinitions is not an object or is null.

Environmental Variables

Environment Variable Override

  • The isFeatureEnabled function can also check for environment variables to determine if a feature is enabled. If the environment variable matches the feature name and is set to "true", the feature is considered enabled.

Example:

export newDashboard=true

In this case, calling isFeatureEnabled("newDashboard") will return true regardless of the predicate function.


Summary

The isFeatureEnabled and setFeature functions provide a flexible and robust way to manage feature flags in your application. By defining features as predicate functions, you can control which features are enabled based on complex logic while maintaining a fallback to environment variables for simple overrides. The error handling ensures that invalid feature definitions are caught early, preventing runtime issues.