# flip-js

> Key-Function store for registering and checking features and variants.

Latest version **0.0.4** (published 2013-12-03) · APACHE license · 0 weekly downloads

## Install

```sh
npm install flip-js
pnpm add flip-js
yarn add flip-js
bun add flip-js
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.4 |
| Published | 2013-12-03 |
| First published | 2013-05-25 |
| Weekly downloads | 0 |
| License | APACHE |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=0.8 |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Derek Reynolds |
| Maintainers | drk |
| Keywords | feature, testing, feature toggle, feature switch, access control, variants, ab testing |

## Links

- npm: https://www.npmjs.com/package/flip-js
- Repository: https://github.com/derekr/flip
- Issues: https://github.com/derekr/flip/issues
- npm.io page: https://npm.io/package/flip-js

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads
- [aws-elasticsearch-connector](https://npm.io/package/aws-elasticsearch-connector.md) — 37.4K weekly downloads

## Recent versions

- 0.0.4 (latest) — 2013-12-03
- 0.0.3 — 2013-05-25
- 0.0.2 — 2013-05-25
- 0.0.1 — 2013-05-25

## README

[![Build Status](https://travis-ci.org/diy/flip.png?branch=master)](https://travis-ci.org/diy/flip)

# Flip

A simple singleton key-function store for registering feature toggles and variant values.

## Install

You use npm right?

```
npm install flip-js
```

## Usage

Flip is a singleton, so everytime you `require('flip')` it will return the same instance. It's inspired by routes in express and template helpers in handlebars if you're familiar with either of those.

You'll want to `register` features somewhere in your server setup.
```js
var flip = require('flip');

flip.register('my-feature', function (subject) {
    return subject.isAdmin;
});
```

Then somewhere in your route or request handler you can `check` 
if the feature should be enabled.
```js
var flip = require('flip');

module.exports = function (req, res) {
    var templateContext = {
        'my-feature-enabled': flip.check('my-feature', req.user)
    };
};
```

You can also run checks asynchronously by providing a callback to the `check` function.
```js
var flip = require('flip');

module.exports = function (req, res) {
    flip.check('my-feature', req.user, function (myFeature) {
        var templateContext = {
            'my-feature-enabled': myFeature
        };
    });
};
```

## Bundled

Flip comes with a few baked `check` functions.

### Blessed List

Most times you just have a list of keys (users for example) that should have the 
feature enabled. In this case you can provide a list of the blessed users and be on your way.

```js
// Register a feature and provide a list of blessed users
flip.register('my-feature', flip.bundled.blessedList(['drk', 'astro']));

// Then provide the user in the check to see if the feature should be enabled
flip.check('my-feature', 'drk'); // true
flip.check('my-feature', 'bogus'); // false
```

### Variant

Another common use case is a/b testing or providing a variant per session. Here you can provide a hash of variants where the values are list of keys (users for example) for each variant.

```js
var variants = {
    awesome: ['astro'],
    favorite: ['drk']
};

flip.register('my-variant', flip.bundled.variant(variants));

// Check for variant for a given user
flip.check('my-variant', 'astro'); // 'awesome'
flip.check('my-variant', 'drk'); // 'favorite'
flip.check('my-variant', 'bogus'); // false (no variant has been assigned!)
``` 

In the last example — `'bogus'` — no variant has been assigned. You can handle the 
logic here for assigning a variant for `'bogus'` and then register the variant again. In this case the variants are stored in memory which isn't extremely useful, but no reason you couldn't use another store like redis.

## Tests

```
npm test
```

---
_Source: https://npm.io/package/flip-js · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
