persist-flag
v1.0.0
Published
Lightweight persistent flag manager with query disabling and scoped key storage
Maintainers
Readme
700B gzipped
➤ Install
yarn add persist-flag➤ Import
import PersistFlag from 'persist-flag';➤ Usage
const pf = new PersistFlag({
namespace: 'myapp',
disableQuery: 'no-flags',
storage: 'session',
});
pf.set('onboarding-complete');
pf.on('onboarding-complete', (value) => console.log('Flag value:', value));➤ Options
| Option | Type | Default | Description |
|-----------------|--------------------------|-----------|----------------------------------------------------------------|
| namespace | string | 'pf' | Prefix for all stored keys. |
| disableQuery | string | 'test' | If this query param is in the URL, flags are disabled (no-op). |
| storage | 'local' | 'session' | 'local' | Choose between localStorage and sessionStorage. |
➤ API
| Method | Description |
|--------------------|-----------------------------------------------------------------------------|
| set(flag, value) | Persists the flag with the given value ('1' by default). |
| get(flag) | Retrieves the stored value for a flag. |
| has(flag) | Returns true if the flag exists in storage. |
| on(flag, cb) | If flag exists, calls cb with its value and returns true; else false. |
| reset(flag?) | Removes a single flag or all flags in the namespace. |
| init(map) | Accepts a flag map of flag: true or flag: callback. |
➤ Disabling via URL
To bypass all flag behavior (e.g. in testing or specific environments), append the disableQuery key to the URL:
https://your-app.com?test➤ License
persist-flag is released under the MIT license.
