Skip to content

DISCLAIMER: This repo is part of the closed project. For informational purposes only! Clone, fork and use it at your own risk!

Notifications You must be signed in to change notification settings

DKI-FDE/storage

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Storage

Extended versions of standard global localStorage and sessionStorage.

Table of Contents

Installing

npm install --save @vayu-fde/storage

API docs

Constant rootStorage: StorageCollection

This constant holds base collection of extended storages (see Interface StorageCollection below). All storage keys created through these extended storages are internally namespaced with "Vayu." prefix. You can create your own subnamespaced versions out of it using namespaced() method.

Interface StorageCollection

export interface StorageCollection {
  local: ExtendedStorage;
  session: ExtendedStorage;
  namespaced: (subspace: string) => StorageCollection;
}

Property local: ExtendedStorage

Represents either extended version of standart global localStorage (see Interface ExtendedStorage below) or its temporal emulation to keep things working. In case of emulation values saved to the storage won't be preserved even between page reloads.

Property session: ExtendedStorage

Represents either extended version of standart global sessionStorage (see Interface ExtendedStorage below) or its temporal emulation to keep things working. In case of emulation values saved to the storage won't be preserved even between page reloads.

Method namespaced: (subspace: string) => StorageCollection

This methos creates subsequently namespaced version of current collection:

import { rootStorage } from '@vayu-fde/storage';

rootStorage.local.setItem('Hello', 'World');
// will create item with the key 'Vayu.Hello'

rootStorage.namespaced('Myspace.').local.setItem('Hello', 'World');
// will create item with the key 'Vayu.Myspace.Hello'

Interface ExtendedStorage

export interface ExtendedStorage extends Storage {
  getBool(key: string): boolean;
  setBool(key: string, value: boolean): void;
  getJson(key: string): unknown;
  setJson(key: string, value: unknown): void;
}

This interface decribes extended version of standard Storage objects (window.localStorage and window.sessionStorage). It includes modified versions of all standard methods and properties and some more described below. Additionally all storage keys created through extended storage are internally namespaced with "Vayu." prefix. That forms main storage namespace. You can further namespace it by using namespaced() method of StorageCollection object

Pay attention: modified copies of standard methods work differently in that way that they use namespaced keys.

Method setBool(key: string, value: boolean): void

This method sets (saves) a boolean value to the storage for the specified key this way:

  • the true value is saved as "1";
  • the false value is not saved at all, but rather removed if there is anything with the corresponding key in the storage.

Method getBool(key: string): boolean

This method gets a boolean value from the storage. If nothing was saved for the specified key or it's an empty string, then false value will be returned, otherwise true will be returned.

Method setJson(key: string, value: unknown): void

This method saves the provided value to the storage for the specified key using JSON.stringify() method. In case of undefined value the specified key will be removed from the storage.

Method getJson(key: string): unknown

This method loads the value from the storage for the specified key using JSON.parse() method or returns undefined if there is no value saved with the provided key.

About

DISCLAIMER: This repo is part of the closed project. For informational purposes only! Clone, fork and use it at your own risk!

Resources

Stars

Watchers

Forks