Extended versions of standard global localStorage
and sessionStorage
.
- Storage
npm install --save @vayu-fde/storage
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.
export interface StorageCollection {
local: ExtendedStorage;
session: ExtendedStorage;
namespaced: (subspace: string) => StorageCollection;
}
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.
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.
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'
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.
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.
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.
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.
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
.