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

Commit b14f9d5

Browse files
committed
feat(WebStorageService): add some comments, keys and config properties, make clear method more customisable
1 parent 989c78d commit b14f9d5

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

src/service/webstorage.service.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { Injectable } from '@angular/core';
22
import { ClearType, Config } from '../config';
33
import { WebStorageUtility } from '../utility/webstorage.utility';
4+
import { WebStorageConfigInterface } from '../config/config.interface';
45

56
export interface WebStorageServiceInterface {
67
keys: Array<string>;
78
new(): {
9+
keys: Array<string>;
10+
config: WebStorageConfigInterface;
811
get(key: string): any;
912
set(key: string, value: any): void;
1013
remove(key: string): void;
@@ -17,6 +20,18 @@ export abstract class WebStorageService {
1720

1821
constructor(protected storage: Storage) {}
1922

23+
/**
24+
* Gets keys from child class
25+
* @returns {Array<string>}
26+
*/
27+
public get keys(): Array<string> {
28+
return (<WebStorageServiceInterface>this.constructor).keys;
29+
}
30+
31+
public get config(): WebStorageConfigInterface {
32+
return Config;
33+
}
34+
2035
public get(key: string): any {
2136
return WebStorageUtility.get(this.storage, key);
2237
}
@@ -29,16 +44,21 @@ export abstract class WebStorageService {
2944
WebStorageUtility.remove(this.storage, key);
3045
}
3146

32-
public clear(clearType?: ClearType): void {
47+
/**
48+
* Clears chosen data from Storage
49+
* @param clearType 'decorators', 'prefix' or 'all'
50+
* @param prefix if clearType = prefix, defines the prefix
51+
*/
52+
public clear(clearType?: ClearType, prefix?: string): void {
3353
clearType = clearType || Config.clearType;
34-
let keys = (<WebStorageServiceInterface>this.constructor).keys; // get keys from child class
3554
if (clearType === 'decorators') {
36-
for (let key of keys) {
55+
for (let key of this.keys) {
3756
this.storage.removeItem(key);
3857
}
3958
} else if (clearType === 'prefix') {
59+
prefix = prefix || Config.prefix;
4060
for (let key in this.storage) {
41-
if (this.storage.getItem(key).startsWith(Config.prefix)) {
61+
if (this.storage.getItem(key).startsWith(prefix)) {
4262
this.storage.removeItem(key);
4363
}
4464
}

0 commit comments

Comments
 (0)