File

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

Description

Wrapper for accessing browsers local storage.

Index

Methods

Constructor

constructor()

Methods

clear
clear(filter?: (key?: string) => void)

Clears the Local Storage

Parameters :
Name Type Optional
filter function yes
Returns : any
getItem
getItem(key: string)

Retrieves a value by key from local storage and parses it

Parameters :
Name Type Optional Description
key string no

The key of the item to be retrieved

Returns : any

The stored value

getStorage
getStorage()
Returns : any
hasItem
hasItem(key: string)

Checks if Key is available in Local Storage

Parameters :
Name Type Optional Description
key string no

The items key

Returns : boolean

True if a value exists for the given key, false otherwise

isEmpty
isEmpty()
Returns : boolean
isKeyDefined
isKeyDefined(key: string)

Checks if a Key is defined in local storage

Parameters :
Name Type Optional Description
key string no

The items key

Returns : boolean | void

True if key eyists, false otherwise

removeItem
removeItem(key: string)

Removes a key value from Local Storage by provided Key

Parameters :
Name Type Optional Description
key string no

The items key

Returns : void
setItem
setItem(key: string, value: any)

Sets the provided value to the local storage

Parameters :
Name Type Optional Description
key string no

The items key

value any no

The items value

Returns : boolean | void
setStorage
setStorage(settings: any)
Parameters :
Name Type Optional
settings any no
Returns : void
import {Injectable} from '@angular/core';

/**
 * @ignore
 */
let instance;


/**
 * Wrapper for accessing browsers local storage.
 */
@Injectable({
  providedIn: 'root'
})
export class LocalStorageService {

  private storage: any;

  constructor() {

    if (instance) {
      instance = this;
    }
    this.storage = this.isLocalStorageAvailable() ? window.localStorage : null;
    return instance;
  }

  /**
   * Sets the provided value to the local storage
   *
   * @param key The items key
   * @param value The items value
   */
  setItem(key: string, value: any): boolean | void {
    if (typeof key === 'undefined' || typeof value === 'undefined') {
      return false;
    }
    this.storage.setItem(key, JSON.stringify(value));
  }

  /**
   * Retrieves a value by key from local storage and parses it
   *
   * @param key The key of the item to be retrieved
   * @return The stored value
   */
  getItem(key: string): any {
    this.isKeyDefined(key);
    const value = this.storage.getItem(key);

    if (value) {
      return JSON.parse(value);
    }
    return false;
  }

  /**
   * Checks if Key is available in Local Storage
   * @param key The items key
   * @return True if a value exists for the given key, false otherwise
   */
  hasItem(key: string): boolean {
    this.isKeyDefined(key);
    return this.storage.getItem(key) === null;
  }

  /**
   * Removes a key value from Local Storage by provided Key
   * @param key The items key
   */
  removeItem(key: string) {
    this.isKeyDefined(key);
    this.storage.removeItem(key);
  }

  /**
   * Clears the Local Storage
   */
  clear(filter?: (key: string) => boolean) {
    return filter ? Object.keys(this.storage).forEach(key => filter(key) && this.removeItem(key)) : this.storage.clear();
  }

  /**
   * Checks if a Key is defined in local storage
   * @param key The items key
   * @return True if key eyists, false otherwise
   */
  isKeyDefined(key: string): boolean | void {
    if (typeof key === 'undefined') {
      return false;
    }
  }

  getStorage() {
    return this.storage;
  }

  setStorage(settings: any) {
    for (const [key, value] of Object.entries(settings)) {
      this.storage.setItem(key, value);
    }
  }

  isEmpty(): boolean {
    return this.storage && this.storage.length === 0;
  }

  /**
   * Checks if the Browser Supports Local Storage
   */
  private isLocalStorageAvailable() {
    try {
      const storage = window.localStorage;
      storage.setItem('test', 'test');
      storage.removeItem('test');
      return true;
    } catch (e) {
      return false;
    }
  }
}

results matching ""

    No results matching ""