File

libs/core/src/lib/local-storage/local-storage.service.ts

Description

LocalStorage save objects with a given key

Example

Index

Properties
Methods

Constructor

constructor()

Methods

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

Parameters :
Name Type Optional
key string No
object any No
Returns : void
Public load
load(key: string)
Type parameters :
  • T

loads the object with for the key

Parameters :
Name Type Optional
key string No
Returns : T

the object if exists, else null

Public loadArray
loadArray(key: string)
Type parameters :
  • T

loads an array of objects for the key

Parameters :
Name Type Optional
key string No
Returns : T[]

the array of objects if exists, else null

Public loadTextual
loadTextual(key: string)

load a textual string for the given key

Parameters :
Name Type Optional
key string No
Returns : string

the string if exists, else null

Public removeItem
removeItem(key: string)

removes the item for the specified key

Parameters :
Name Type Optional
key string No
Returns : void
Public save
save(key: string, object: any)

Saves the object with the key in the local storage

Parameters :
Name Type Optional
key string No
object any No
Returns : boolean

successfull saving

Properties

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;
    }

}

result-matching ""

    No results matching ""