aboutsummaryrefslogtreecommitdiffstats
path: root/public/projects/angular-small-apps/apps/recipes/src/app/shared/services/local-storage.service.ts
blob: 82a72e79782fb1b2410cc53c246bc383ba43d1fe (plain)
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
import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root',
})
export class LocalStorageService {
  constructor() {}

  get(key: string): any {
    try {
      const serialItem = localStorage.getItem(key);
      if (serialItem) {
        return JSON.parse(serialItem);
      } else {
        return undefined;
      }
    } catch (e) {
      console.log(e);
      return undefined;
    }
  }

  set(key: string, value: any) {
    try {
      const serialItem = JSON.stringify(value);
      localStorage.setItem(key, serialItem);
    } catch (e) {
      console.log(e);
    }
  }

  remove(key: string) {
    localStorage.removeItem(key);
  }

  clear() {
    localStorage.clear;
  }
}