libs/core/src/lib/local-storage/local-storage.service.ts
LocalStorage save objects with a given key
Properties |
|
Methods |
|
constructor()
|
Public clearStorage |
clearStorage()
|
clears the complete local storage
Returns :
void
|
Protected defineDefault |
defineDefault(key: string, object: any)
|
sets a default value for the given key, which will be delivered, if nothing is stored in localstorage for the key
Returns :
void
|
Public load | ||||||
load(key: string)
|
||||||
Type parameters :
|
||||||
loads the object with for the key
Parameters :
Returns :
T
the object if exists, else null |
Public loadArray | ||||||
loadArray(key: string)
|
||||||
Type parameters :
|
||||||
loads an array of objects for the key
Parameters :
Returns :
T[]
the array of objects if exists, else null |
Public loadTextual | ||||||
loadTextual(key: string)
|
||||||
load a textual string for the given key
Parameters :
Returns :
string
the string if exists, else null |
Public removeItem | ||||||
removeItem(key: string)
|
||||||
removes the item for the specified key
Parameters :
Returns :
void
|
Public save |
save(key: string, object: any)
|
Saves the object with the key in the local storage
Returns :
boolean
successfull saving |
Private defaults |
Type : object
|
Default value : {}
|
Private localStorageEnabled |
Default value : false
|
import { Injectable } from '@angular/core';
/**
* LocalStorage save objects with a given key
*
* @export
*/
@Injectable()
export class LocalStorage {
private localStorageEnabled = false;
private defaults = {};
constructor() {
if (typeof (Storage) !== 'undefined') {
this.localStorageEnabled = true;
}
}
/**
* Saves the object with the key in the local storage
*
* @param key
* @param object
* @returns successfull saving
*/
public save(key: string, object: any): boolean {
if (this.localStorageEnabled) {
localStorage.setItem(key, JSON.stringify(object));
return true;
}
return false;
}
/**
* loads the object with for the key
*
* @param key
* @returns the object if exists, else null
*/
public load<T>(key: string): T {
if (this.localStorageEnabled) {
const result = localStorage.getItem(key);
if (result) {
return JSON.parse(result);
}
}
return this.defaults[key];
}
/**
* loads an array of objects for the key
*
* @param key
* @returns the array of objects if exists, else null
*/
public loadArray<T>(key: string): T[] {
if (this.localStorageEnabled) {
const result = localStorage.getItem(key);
if (result) {
return JSON.parse(result);
}
}
return this.defaults[key];
}
/**
* load a textual string for the given key
*
* @param key
* @returns the string if exists, else null
*/
public loadTextual(key: string): string {
if (this.localStorageEnabled) {
const result = localStorage.getItem(key);
if (result) {
return result;
}
}
return this.defaults[key];
}
/**
* clears the complete local storage
*/
public clearStorage() {
if (this.localStorageEnabled) {
localStorage.clear();
}
}
/**
* removes the item for the specified key
* @param key
*/
public removeItem(key: string) {
if (this.localStorageEnabled) {
localStorage.removeItem(key);
}
}
/**
* sets a default value for the given key, which will be delivered, if nothing is stored in localstorage for the key
*
* @param key
* @param object
*/
protected defineDefault(key: string, object: any) {
this.defaults[key] = object;
}
}