flagswappr
TypeScript icon, indicating that this package has built-in type declarations

0.8.15 • Public • Published

FlagSwappr

Code Style: Prettier TypeScript: Strict NPM version Join the chat at https://gitter.im/FullScreenShenanigans/community

Gates feature flags behind generational gaps.

Usage

FlagSwappr keeps track of whether features are enabled based on a current "generation". It's based off the concept of game generations, such as Pokemon, where features are turned on or off in different releases.

Constructor

const flagSwapper = new FlagSwappr({
    generations: {
        first: {
            eggs: false,
        },
        second: {
            eggs: true,
        },
    },
});

generation

Starting generation to enable, if not the first from generationNames.

const flagSwapper = new FlagSwappr({
    generation: "second",
    generations: {
        first: {
            eggs: false,
        },
        second: {
            eggs: true,
        },
    },
});

const { eggs } = flagSwapper.flags; // true

generationNames

Ordered names of the available generations, if not Object.keys(generations). The first key in this string[] is used as the starting generation.

const flagSwapper = new FlagSwappr({
    generationNames: ["second", "first"],
    generations: {
        first: {
            eggs: false,
        },
        second: {
            eggs: true,
        },
    },
});

const { eggs } = flagSwapper.flags; // true

generations

Groups of feature settings, in order. These represent the changes each generation made to the available feature flags.

Usage with TypeScript

FlagSwappr is templated across a TFlags type, where each of its flags are a Partial of that type.

The template is inferred from the constructor or can be specified manually.

interface Flags {
    eggs: boolean;
}

const flagSwapper = new FlagSwappr<IFlags>({
    generations: {
        first: {
            eggs: false,
        },
        second: {
            eggs: true,
        },
    },
});

flags

Getter for the generation-variant flags. When a new generation is set, the internal representation is reset to an object with flags for what the current generation's state is for that generation. Each member flag is equal the most "recent" generation setting, as defined by the generationNames order.

const flagSwapper = new FlagSwappr({
    generation: "third",
    generationNames: ["first", "second", "third"],
    generations: {
        first: {
            eggs: false,
        },
        second: {
            eggs: true,
        },
    },
});

const { eggs } = flagSwapper.flags; // true

setGeneration

Parameters:

  • generationName: string: Generation for flag setting.

Sets flags to a generation.

const flagSwapper = new FlagSwappr({
    generation: "third",
    generationNames: ["first", "second", "third"],
    generations: {
        first: {
            eggs: false,
        },
        second: {
            eggs: true,
        },
    },
});

flagSwapper.setGeneration("first");

const { eggs } = flagSwapper.flags; // false

Development

This repository is a portion of the EightBittr monorepo. See its docs/Development.md for details on how to get started. 💖

Running Tests

yarn run test

Tests are written in Mocha and Chai. Their files are written using alongside source files under src/ and named *.test.ts?. Whenever you add, remove, or rename a *.test.t* file under src/, watch will re-run yarn run test:setup to regenerate the list of static test files in test/index.html. You can open that file in a browser to debug through the tests, or run yarn test:run to run them in headless Chrome.

Readme

Keywords

none

Package Sidebar

Install

npm i flagswappr

Weekly Downloads

7

Version

0.8.15

License

MIT

Unpacked Size

24.6 kB

Total Files

23

Last publish

Collaborators

  • joshuakgoldberg