-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathlocal-storage.service.ts
100 lines (88 loc) · 2.51 KB
/
local-storage.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { Injectable } from "@angular/core";
import { IProtectedFieldName, getProtectedFieldName } from "data-models";
import { SyncServiceBase } from "../syncService.base";
const STORAGE_PREFIX = "rp-contact-field";
@Injectable({
providedIn: "root",
})
export class LocalStorageService extends SyncServiceBase {
constructor() {
super("LocalStorage");
}
private get(key: string): string | null {
if (!key) return null;
if (!key.startsWith(STORAGE_PREFIX)) {
key = `${STORAGE_PREFIX}.${key}`;
}
return localStorage.getItem(key);
}
private set(key: string, value: string, allowProtected = false) {
if (!key) return;
if (!allowProtected && this.isProtected(key)) {
console.warn(`[DEPRECATED] - set local-storage with protected name: ${key}`);
}
if (!key.startsWith(STORAGE_PREFIX)) {
key = `${STORAGE_PREFIX}.${key}`;
}
return localStorage.setItem(key, value);
}
private remove(key: string) {
if (!key) return;
if (!key.startsWith(STORAGE_PREFIX)) {
key = `${STORAGE_PREFIX}.${key}`;
}
return localStorage.removeItem(key);
}
getString(key: string) {
return this.get(key);
}
setString(key: string, value: string) {
return this.set(key, value);
}
getJSON(key: string): any {
let jsonString = this.get(key);
if (jsonString) {
try {
return JSON.parse(jsonString);
} catch (ex) {}
}
return null;
}
setJSON(key: string, value: any): any {
try {
let jsonString = JSON.stringify(value);
this.set(key, jsonString);
} catch (ex) {
return null;
}
}
getAll() {
const values = {};
Object.keys(localStorage)
.filter((k) => k.startsWith(STORAGE_PREFIX))
.forEach((k) => (values[k] = localStorage.getItem(k)));
return values;
}
clear() {
localStorage.clear();
}
setProtected(field: IProtectedFieldName, value: string) {
const fieldName = getProtectedFieldName(field);
return this.set(fieldName, value, true);
}
getProtected(field: IProtectedFieldName) {
const fieldName = getProtectedFieldName(field);
return this.get(fieldName);
}
removeProtected(field: IProtectedFieldName) {
const fieldName = getProtectedFieldName(field);
return this.remove(fieldName);
}
/** Check if a field name is protected (starts with underscore prefixed or non-prefixed) */
isProtected(key: string) {
if (key.startsWith(STORAGE_PREFIX)) {
key = key.replace(`${STORAGE_PREFIX}.`, "");
}
return key.startsWith("_");
}
}