feature-utils.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { storageGet, storageSet } from './storage-utils';
  2. // prefix for all feature flags
  3. const STORAGE_KEY_PREFIX = 'scope-experimental:';
  4. const getKey = key => `${STORAGE_KEY_PREFIX}${key}`;
  5. /**
  6. * Returns true if `feature` is enabled
  7. *
  8. * Features can be enabled either via calling `setFeature()` or by setting
  9. * `localStorage.scope-experimental:featureName = true` in the console.
  10. * @param {String} feature Feature name, ideally one word or hyphenated
  11. * @return {Boolean} True if feature is enabled
  12. */
  13. export function featureIsEnabled(feature) {
  14. let enabled = storageGet(getKey(feature));
  15. if (typeof enabled === 'string') {
  16. // Convert back to boolean if stored as a string.
  17. enabled = JSON.parse(enabled);
  18. }
  19. return enabled;
  20. }
  21. /**
  22. * Returns true if any of the features given as arguments are enabled.
  23. *
  24. * Useful if features are hierarchical, e.g.:
  25. * `featureIsEnabledAny('superFeature', 'subFeature')`
  26. * @param {String} args Feature names
  27. * @return {Boolean} True if any of the features are enabled
  28. */
  29. export function featureIsEnabledAny(...args) {
  30. return Array.prototype.some.call(args, feature => featureIsEnabled(feature));
  31. }
  32. /**
  33. * Set true/false if a feature is enabled.
  34. * @param {String} feature Feature name
  35. * @param {Boolean} isEnabled true/false
  36. */
  37. export function setFeature(feature, isEnabled) {
  38. return storageSet(getKey(feature), isEnabled);
  39. }