Low-level feature toggle tools for Node and browsers.
npm install --save @paralleldrive/feature-toggles
import {
getCurrentActiveFeatureNames,
isActiveFeatureName
} from '@paralleldrive/feature-toggles';
const initialFeatures = [
{
name: 'comments',
isActive: true
},
{
name: 'ratings',
isActive: false
},
{
name: 'faq',
isActive: false
},
{
name: 'help'
isActive: false
}
];
const req = { query: { ft: 'ratings,help' } };
const activeFeaturesNames = getCurrentActiveFeatureNames({
initialFeatures,
req
});
const isCommentsActive = isActiveFeatureName('comments', activeFeaturesNames); // true
const isRatingsActive = isActiveFeatureName('ratings', activeFeaturesNames); // true ( enabled via req query object )
const isFAQActive = isActiveFeatureName('faq', activeFeaturesNames); // false
const isHelpActive = isActiveFeatureName('help', activeFeaturesNames); // true ( enabled via req query object )
interface Feature {
name: String,
isActive: false,
dependencies?: [...String]
}
[...String] => [...Feature] => [...Feature]
Activates Features by name of the provided array of Features.
const initialFeatures = [
{ name: 'foo', isActive: true },
{ name: 'bar', isActive: false },
{ name: 'baz', isActive: false }
];
activateFeatures(['bar', 'baz'])(initialFeatures);
//
// [
// { name: 'foo', isActive: true },
// { name: 'bar', isActive: true },
// { name: 'baz', isActive: true },
// ]
//
([...Feature]) => [...String]
Takes an array of feature objects and returns an array of active feature names. This function respects Feature dependencies.
Takes a window.location.search
string and returns an array of active feature names. If search is not provided will grab the global window.location.search
if available.
(search?) => [...String]
const search = '?ft=foo,bar,baz';
getBrowserQueryFeatureNames(search); // ['foo', 'bar', 'baz']
Takes an array of initialFeatures, a req object, and a window.location.search
string and returns an array of active feature names. If search is not provided will grab the global window.location.search
if available. This function respects Feature dependencies.
({ initialFeatures = [...Feature], req? , search? }) => [...String])]
const initialFeatures = [
{ name: 'foo', isActive: true },
{ name: 'bar', isActive: false },
{ name: 'baz', isActive: false },
{ name: 'other': isActive: false }
]
getCurrentActiveFeatureNames({ initialFeatures }); // ['foo']
const req = {
query:{
ft='bar,baz'
}
};
getCurrentActiveFeatureNames({ initialFeatures, req }); // ['foo', 'bar', 'baz']
(req = {}) => [...String]
Takes a req object and returns an array of enabled feature names.
const req = {
query:{
ft='foo,bar,help'
}
};
getReqQueryFeatureNames(req); // ['foo', 'bar', 'help']
(query = {}) => [...String]
Takes a query object and returns an array of enabled feature names.
const query = { ft='foo,bar,help' }
getQueryFeatureNames(query); // ['foo', 'bar', 'help']
String => [...String] => boolean
Returns true if a feature name is in the array else it returns false.
const currentFeatures = ['foo', 'bar', 'baz'];
isActiveFeatureName('bar', currentFeatures); // true
isActiveFeatureName('cat', currentFeatures); // false
(...[...String]) => [...String]
Merge feature names without duplicating.
const currentFeatures = ['foo', 'bar', 'baz'];
mergeFeatureNames(currentFeatures, ['fish', 'bar', 'cat']); // ['foo', 'bar', 'baz', 'fish', 'cat']
([...String], [...String]) => [...String]
Removes feature names
const currentFeatures = ['foo', 'bar', 'baz', 'cat'];
removeFeatureNames(currentFeatures, ['fish', 'bar', 'cat']); // ['foo', 'baz']