Skip to content

Commit 5ab0d85

Browse files
committed
feat(WebStorageUtility): hold storage data inside the class, add clear() and forEach() methods, introduce LocalStorageUtility and SessionStorageUtility
1 parent 9a23976 commit 5ab0d85

File tree

6 files changed

+58
-35
lines changed

6 files changed

+58
-35
lines changed

src/config/config.helper.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { WebStorageUtilityClass } from '../utility/webstorage-utility.class';
22

33
export class ConfigHelper {
4-
protected static _webStorageUtility: WebStorageUtilityClass = new WebStorageUtilityClass('NGX-STORE_');
4+
protected static _webStorageUtility: WebStorageUtilityClass =
5+
new WebStorageUtilityClass(localStorage, 'NGX-STORE_');
56

67
public static getItem(key: string): any {
7-
return this._webStorageUtility.get(localStorage, key);
8+
return this._webStorageUtility.get(key);
89
}
910

1011
public static setItem(key: string, item: any): any {
11-
return this._webStorageUtility.set(localStorage, key, item);
12+
return this._webStorageUtility.set(key, item);
1213
}
1314
}

src/decorator/webstorage.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
import { WebStorageUtility } from '../utility';
1+
import { localStorageUtility, sessionStorageUtility } from '../utility';
22
import { LocalStorageService, SessionStorageService, WebStorageServiceInterface } from '../service/webstorage.service';
33
import * as isEmpty from 'is-empty';
44
import { Config } from '../config';
5+
import { WebStorageUtilityClass } from '../utility/webstorage-utility.class';
56

67
export function LocalStorage(key?: string) {
7-
return WebStorage(localStorage, LocalStorageService, key);
8+
return WebStorage(localStorageUtility, LocalStorageService, key);
89
}
910

1011
export function SessionStorage(key?: string) {
11-
return WebStorage(sessionStorage, SessionStorageService, key);
12+
return WebStorage(sessionStorageUtility, SessionStorageService, key);
1213
}
1314

1415
// initialization cache
1516
let cache: {[name: string]: boolean} = {};
1617

17-
function WebStorage(webStorage: Storage, service: WebStorageServiceInterface, key: string) {
18+
function WebStorage(webStorageUtility: WebStorageUtilityClass, service: WebStorageServiceInterface, key: string) {
1819
return (target: any, propertyName: string): void => {
1920
key = key || propertyName;
2021

@@ -28,7 +29,8 @@ function WebStorage(webStorage: Storage, service: WebStorageServiceInterface, ke
2829
};
2930
}
3031

31-
let proxy = WebStorageUtility.get(webStorage, key);
32+
// let proxy = WebStorageUtility.get(webStorageUtility, key);
33+
let proxy = webStorageUtility.get(key);
3234
service.keys.push(key);
3335

3436
Object.defineProperty(target, propertyName, {
@@ -39,11 +41,12 @@ function WebStorage(webStorage: Storage, service: WebStorageServiceInterface, ke
3941
if (!cache[key]) { // first setter handle
4042
if (isEmpty(proxy)) {
4143
// if no value in localStorage, set it to initializer
42-
proxy = WebStorageUtility.set(webStorage, key, value);
44+
// proxy = WebStorageUtility.set(webStorage, key, value);
45+
proxy = webStorageUtility.set(key, value);
4346
}
4447
cache[key] = true;
4548
} else { // if there is no value in localStorage, set it to initializer
46-
proxy = WebStorageUtility.set(webStorage, key, value);
49+
proxy = webStorageUtility.set(key, value);
4750
}
4851

4952
// Object mutations below
@@ -52,7 +55,7 @@ function WebStorage(webStorage: Storage, service: WebStorageServiceInterface, ke
5255
// manual method for force save
5356
if (proxy instanceof Object) {
5457
proxy.save = function () {
55-
WebStorageUtility.set(webStorage, key, proxy);
58+
webStorageUtility.set(key, proxy);
5659
};
5760
}
5861

@@ -65,7 +68,7 @@ function WebStorage(webStorage: Storage, service: WebStorageServiceInterface, ke
6568
for (let method of methodsToOverwrite) {
6669
proxy[method] = function(value) {
6770
let result = Array.prototype[method].apply(proxy, arguments);
68-
WebStorageUtility.set(webStorage, key, proxy);
71+
webStorageUtility.set(key, proxy);
6972
return result;
7073
}
7174
}

src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { LocalStorageService, SessionStorageService } from './service/webstorage
33

44
export { LocalStorage, SessionStorage } from './decorator/webstorage'
55
export { WebStorageService, LocalStorageService, SessionStorageService } from './service/webstorage.service';
6-
export { WebStorageUtility } from './utility';
76
export { WebStorageConfigInterface, WEBSTORAGE_CONFIG } from './config';
87
export declare class Webstorable {
98
save(): void;

src/service/webstorage.service.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Injectable } from '@angular/core';
22
import { ClearType, Config } from '../config';
3-
import { WebStorageUtility } from '../utility';
43
import { WebStorageConfigInterface } from '../config/config.interface';
4+
import { WebStorageUtilityClass } from '../utility/webstorage-utility.class';
5+
import { localStorageUtility, sessionStorageUtility } from '../utility';
56

67
export interface WebStorageServiceInterface {
78
keys: Array<string>;
@@ -18,7 +19,11 @@ export interface WebStorageServiceInterface {
1819
export abstract class WebStorageService {
1920
public static keys: Array<string>;
2021

21-
constructor(protected storage: Storage) {}
22+
protected _webStorageUtility: WebStorageUtilityClass;
23+
24+
public constructor(protected webStorageUtility: WebStorageUtilityClass) {
25+
this._webStorageUtility = webStorageUtility;
26+
}
2227

2328
/**
2429
* Gets keys from child class
@@ -33,15 +38,16 @@ export abstract class WebStorageService {
3338
}
3439

3540
public get(key: string): any {
36-
return WebStorageUtility.get(this.storage, key);
41+
return this._webStorageUtility.get(key);
3742
}
3843

3944
public set(key: string, value: any): void {
40-
WebStorageUtility.set(this.storage, key, value);
45+
return this._webStorageUtility.set(key, value);
4146
}
4247

48+
// TODO return true if item existed and false otherwise (?)
4349
public remove(key: string): void {
44-
WebStorageUtility.remove(this.storage, key);
50+
return this._webStorageUtility.remove(key);
4551
}
4652

4753
/**
@@ -53,17 +59,17 @@ export abstract class WebStorageService {
5359
clearType = clearType || Config.clearType;
5460
if (clearType === 'decorators') {
5561
for (let key of this.keys) {
56-
this.storage.removeItem(key);
62+
this._webStorageUtility.remove(key);
5763
}
5864
} else if (clearType === 'prefix') {
5965
prefix = prefix || Config.prefix;
60-
for (let key in this.storage) {
61-
if (this.storage.getItem(key).startsWith(prefix)) {
62-
this.storage.removeItem(key);
66+
this._webStorageUtility.forEach((key) => {
67+
if (key.startsWith(prefix)) {
68+
this._webStorageUtility.remove(key);
6369
}
64-
}
70+
});
6571
} else if (clearType === 'all') {
66-
this.storage.clear();
72+
this._webStorageUtility.clear();
6773
}
6874
}
6975
}
@@ -73,7 +79,7 @@ export class LocalStorageService extends WebStorageService {
7379
public static keys: Array<string> = [];
7480

7581
constructor() {
76-
super(localStorage);
82+
super(localStorageUtility);
7783
}
7884
}
7985

@@ -82,6 +88,6 @@ export class SessionStorageService extends WebStorageService {
8288
public static keys: Array<string> = [];
8389

8490
constructor() {
85-
super(sessionStorage);
91+
super(sessionStorageUtility);
8692
}
8793
}

src/utility/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { Config } from '../config';
22
import { WebStorageUtilityClass } from './webstorage-utility.class';
33

4-
export const WebStorageUtility: WebStorageUtilityClass = new WebStorageUtilityClass(Config.prefix);
4+
export const localStorageUtility: WebStorageUtilityClass =
5+
new WebStorageUtilityClass(localStorage, Config.prefix, Config.previousPrefix);
6+
export const sessionStorageUtility: WebStorageUtilityClass =
7+
new WebStorageUtilityClass(sessionStorage, Config.prefix, Config.previousPrefix);
Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export class WebStorageUtilityClass {
22
protected _prefix: string = '';
3+
protected _storage: Storage;
34

45
public static getSettable(value: any): string {
56
return typeof value === "string" ? value : JSON.stringify(value);
@@ -14,30 +15,40 @@ export class WebStorageUtilityClass {
1415
}
1516
}
1617

17-
public constructor(prefix: string) {
18+
public constructor(storage: Storage, prefix: string, previousPrefix?: string) {
19+
this._storage = storage;
1820
this._prefix = prefix;
1921
}
2022

21-
2223
public getStorageKey(key: string): string {
2324
return `${this._prefix}${key}`;
2425
}
2526

26-
public get(storage: Storage, key: string): any {
27+
public get(key: string): any {
2728
let storageKey = this.getStorageKey(key);
28-
let value = storage.getItem(storageKey);
29+
let value = this._storage.getItem(storageKey);
2930
return WebStorageUtilityClass.getGettable(value);
3031
}
3132

32-
public set(storage: Storage, key: string, value: any): any {
33+
public set(key: string, value: any): any {
3334
let storageKey = this.getStorageKey(key);
3435
let storable = WebStorageUtilityClass.getSettable(value);
35-
storage.setItem(storageKey, storable);
36+
this._storage.setItem(storageKey, storable);
3637
return value;
3738
}
3839

39-
public remove(storage: Storage, key: string): void {
40+
public remove(key: string): void {
4041
let storageKey = this.getStorageKey(key);
41-
storage.removeItem(storageKey);
42+
this._storage.removeItem(storageKey);
43+
}
44+
45+
public clear() {
46+
this._storage.clear();
47+
}
48+
49+
public forEach(func: Function) {
50+
for (let key in this._storage) {
51+
func(key, this._storage[key]);
52+
}
4253
}
4354
}

0 commit comments

Comments
 (0)