Adapted feature check to allow only some features

This commit is contained in:
David Kaltschmidt
2016-11-15 11:35:35 +01:00
parent afe177cdba
commit faa8ee098c
3 changed files with 47 additions and 13 deletions

View File

@@ -6,7 +6,8 @@ describe('FeatureUtils', () => {
const FEATURE_Y_KEY = 'my feature 2';
beforeEach(() => {
FU.clearFeatures();
FU.setFeature(FEATURE_X_KEY, false);
FU.setFeature(FEATURE_Y_KEY, false);
});
describe('Setting of features', () => {
@@ -26,5 +27,17 @@ describe('FeatureUtils', () => {
expect(success).toBeTruthy();
expect(FU.featureIsEnabled(FEATURE_X_KEY)).toBeFalsy();
});
it('should allow for either feature', () => {
let success;
expect(FU.featureIsEnabledAny(FEATURE_X_KEY, FEATURE_Y_KEY)).toBeFalsy();
success = FU.setFeature(FEATURE_X_KEY, true);
expect(success).toBeTruthy();
expect(FU.featureIsEnabledAny(FEATURE_X_KEY, FEATURE_Y_KEY)).toBeTruthy();
success = FU.setFeature(FEATURE_X_KEY, false);
success = FU.setFeature(FEATURE_Y_KEY, true);
expect(success).toBeTruthy();
expect(FU.featureIsEnabledAny(FEATURE_X_KEY, FEATURE_Y_KEY)).toBeTruthy();
});
});
});

View File

@@ -1,18 +1,39 @@
import { storageGetObject, storageSetObject } from './storage-utils';
import { storageGet, storageSet } from './storage-utils';
const STORAGE_FEATURE_KEY = 'scopeFeatureFlags';
// prefix for all feature flags
const STORAGE_KEY_PREFIX = 'scope-experimental-';
const getKey = key => `${STORAGE_KEY_PREFIX}${key}`;
/**
* Returns true if `feature` is enabled
*
* Features can be enabled either via calling `setFeature()` or by setting
* `localStorage.scope-experimental-featureName = true` in the console.
* @param {String} feature Feature name, ideally one word or hyphenated
* @return {Boolean} True if feature is enabled
*/
export function featureIsEnabled(feature) {
const features = storageGetObject(STORAGE_FEATURE_KEY, {});
return features[feature];
return storageGet(getKey(feature));
}
/**
* Returns true if any of the features given as arguments are enabled.
*
* Useful if features are hierarchical, e.g.:
* `featureIsEnabledAny('superFeature', 'subFeature')`
* @param {String} args Feature names
* @return {Boolean} True if any of the features are enabled
*/
export function featureIsEnabledAny(...args) {
return Array.prototype.some.call(args, feature => featureIsEnabled(feature));
}
/**
* Set true/false if a feature is enabled.
* @param {String} feature Feature name
* @param {Boolean} isEnabled true/false
*/
export function setFeature(feature, isEnabled) {
const features = storageGetObject(STORAGE_FEATURE_KEY, {});
features[feature] = isEnabled;
return storageSetObject(STORAGE_FEATURE_KEY, features);
}
export function clearFeatures() {
return storageSetObject(STORAGE_FEATURE_KEY, {});
return storageSet(getKey(feature), isEnabled);
}

View File

@@ -6,7 +6,7 @@ const localStorageMock = (function() {
return store[key];
},
setItem: function(key, value) {
store[key] = value.toString();
store[key] = value;
},
clear: function() {
store = {};