Skip to content
This repository was archived by the owner on Nov 28, 2025. It is now read-only.

Commit e9abc14

Browse files
committed
feat(WebStorageService): improve the clear method which now takes a parameter defining what should be removed
1 parent fceff05 commit e9abc14

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export { WebStorageUtility } from './utility/webstorage.utility';
77
export declare class Webstorable {
88
save(): void;
99
}
10+
export type ClearType = 'decorators' | 'prefix' | 'all';
1011
export let WEBSTORAGE_CONFIG = {
1112
prefix: 'angular2ws_'
1213
};

src/service/webstorage.service.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Injectable } from '@angular/core';
22
import { WebStorageUtility } from '../utility/webstorage.utility';
3+
import { ClearType, WEBSTORAGE_CONFIG } from '../index';
34

45
export interface WebStorageServiceInterface {
56
keys: Array<string>;
@@ -28,9 +29,20 @@ export abstract class WebStorageService {
2829
WebStorageUtility.remove(this.storage, key);
2930
}
3031

31-
clear(): void {
32-
for (let key of (<WebStorageServiceInterface>this.constructor).keys) {
33-
this.storage.removeItem(key);
32+
public clear(clearType: ClearType = 'decorators'): void {
33+
let keys = (<WebStorageServiceInterface>this.constructor).keys; // get keys from child class
34+
if (clearType === 'decorators') {
35+
for (let key of keys) {
36+
this.storage.removeItem(key);
37+
}
38+
} else if (clearType === 'prefix') {
39+
for (let key in this.storage) {
40+
if (this.storage.getItem(key).startsWith(WEBSTORAGE_CONFIG.prefix)) {
41+
this.storage.removeItem(key);
42+
}
43+
}
44+
} else if (clearType === 'all') {
45+
this.storage.clear();
3446
}
3547
}
3648
}

0 commit comments

Comments
 (0)