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

Commit c53f021

Browse files
committed
chore(WebStorageUtility): rebuild, decrease number of static methods
1 parent b14f9d5 commit c53f021

File tree

6 files changed

+50
-42
lines changed

6 files changed

+50
-42
lines changed

src/decorator/webstorage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { WebStorageUtility } from '../utility/webstorage.utility';
1+
import { WebStorageUtility } from '../utility';
22
import { LocalStorageService, SessionStorageService, WebStorageServiceInterface } from '../service/webstorage.service';
33
import * as isEmpty from 'is-empty';
44
import { Config } from '../config';

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ 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/webstorage.utility';
6+
export { WebStorageUtility } from './utility';
77
export { WebStorageConfigInterface, WEBSTORAGE_CONFIG } from './config';
88
export declare class Webstorable {
99
save(): void;

src/service/webstorage.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Injectable } from '@angular/core';
22
import { ClearType, Config } from '../config';
3-
import { WebStorageUtility } from '../utility/webstorage.utility';
3+
import { WebStorageUtility } from '../utility';
44
import { WebStorageConfigInterface } from '../config/config.interface';
55

66
export interface WebStorageServiceInterface {

src/utility/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { Config } from '../config';
2+
import { WebStorageUtilityClass } from './webstorage-utility.class';
3+
4+
export const WebStorageUtility: WebStorageUtilityClass = new WebStorageUtilityClass(Config.prefix);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
export class WebStorageUtilityClass {
2+
protected _prefix: string = '';
3+
4+
public static getSettable(value: any): string {
5+
return typeof value === "string" ? value : JSON.stringify(value);
6+
}
7+
8+
public static getGettable(value: string): any {
9+
if (value === 'undefined') return null;
10+
try {
11+
return JSON.parse(value);
12+
} catch(e) {
13+
return value;
14+
}
15+
}
16+
17+
public constructor(prefix: string) {
18+
this._prefix = prefix;
19+
}
20+
21+
22+
public getStorageKey(key: string): string {
23+
return `${this._prefix}${key}`;
24+
}
25+
26+
public get(storage: Storage, key: string): any {
27+
let storageKey = this.getStorageKey(key);
28+
let value = storage.getItem(storageKey);
29+
return WebStorageUtilityClass.getGettable(value);
30+
}
31+
32+
public set(storage: Storage, key: string, value: any): any {
33+
let storageKey = this.getStorageKey(key);
34+
let storable = WebStorageUtilityClass.getSettable(value);
35+
storage.setItem(storageKey, storable);
36+
return value;
37+
}
38+
39+
public remove(storage: Storage, key: string): void {
40+
let storageKey = this.getStorageKey(key);
41+
storage.removeItem(storageKey);
42+
}
43+
}

src/utility/webstorage.utility.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)