Skip to content
This repository has been archived by the owner on Mar 19, 2024. It is now read-only.

Commit

Permalink
Introduce a tidy-up method to remove old features
Browse files Browse the repository at this point in the history
  • Loading branch information
hkan authored Jun 13, 2023
2 parents a5d9079 + 0340d94 commit cf4981c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[*.ts]
indent_size = 4
indent_style = space
26 changes: 23 additions & 3 deletions spec/FeatureToggleService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { FeatureToggleService } from '../src/FeatureToggleService';
describe('Feature toggling', () => {
beforeEach(() => {
localStorage.clear();
FeatureToggleService.clearInternalFeatureList();
});

it('can register a feature', () => {
Expand Down Expand Up @@ -32,7 +33,7 @@ describe('Feature toggling', () => {
expect(JSON.parse(localStorage.getItem('_feature.foo')).enabled).toBe(true);
});

it('it considers a feature disabled if it is not set to on', () => {
it('considers a feature disabled if it is not set to on', () => {
const service = new FeatureToggleService();

service.register({
Expand All @@ -43,7 +44,7 @@ describe('Feature toggling', () => {
expect(service.check('foo')).toBe(false);
});

it('it considers a feature disabled if the localStorage key is missing', () => {
it('considers a feature disabled if the localStorage key is missing', () => {
const service = new FeatureToggleService();

service.register({
Expand All @@ -56,7 +57,7 @@ describe('Feature toggling', () => {
expect(service.check('foo')).toBe(false);
});

it('it considers a feature enabled if it is set to on', () => {
it('considers a feature enabled if it is set to on', () => {
const service = new FeatureToggleService();

service.register({
Expand All @@ -71,4 +72,23 @@ describe('Feature toggling', () => {

expect(service.check('foo')).toBe(true);
});

describe('tidyUp method', () => {
it('removes the features that is not registered from localStorage', () => {
localStorage.setItem('_feature.some_old_feature', JSON.stringify({
name: 'Old feature that we will no longer register',
enabled: true,
}));

const service = new FeatureToggleService();
service.register({
name: 'Some new feature',
key: 'some_new_feature',
});

service.tidyUpLocalStorageData();

expect(localStorage.getItem('_feature.some_old_feature')).toBeNull();
});
});
});
35 changes: 34 additions & 1 deletion src/FeatureToggleService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ export class FeatureToggleService {
localStorage.setItem('_feature._enabled', JSON.stringify(true));
}

/**
* Clears the internal feature list. This only exists to be used in tests.
* Do not use in production code. No really, don't.
*
* @private
*/
static clearInternalFeatureList() {
features.clear();
}

register(feature: Feature) {
if (!feature.name) {
feature.name = feature.key;
Expand All @@ -27,6 +37,29 @@ export class FeatureToggleService {
}
}

/**
* This method is used to tidy up localStorage. It will remove any features
* that are no longer registered.
*
* You should only call this method after registering all features.
*/
tidyUpLocalStorageData() {
const registeredFeatures = Array.from(features.keys());
const keysInLocalStorage = Object.keys(localStorage)
.filter(key =>
key.startsWith(this.localStoragePrefix) &&
key !== `${this.localStoragePrefix}._enabled`
);

keysInLocalStorage.forEach(key => {
const feature = key.replace(`${this.localStoragePrefix}.`, '');

if (registeredFeatures.indexOf(feature) === -1) {
localStorage.removeItem(key);
}
});
}

check(featureKey: string): boolean {
if (!features.has(featureKey)) {
console.warn(`The feature ${featureKey} has not been registered`);
Expand All @@ -41,4 +74,4 @@ export class FeatureToggleService {
private localStorageKey(featureKey: string): string {
return `${this.localStoragePrefix}.${featureKey}`;
}
}
}

0 comments on commit cf4981c

Please sign in to comment.