4.0.0 • Published 3 years ago

@writetome51/item-in-browser-storage v4.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

ItemInBrowserStorage

An abstract TypeScript/JavaScript class representing an item stored in the browser's
localStorage or sessionStorage. The choice of localStorage or sessionStorage
must be decided by a subclass using the __storageType argument in the constructor.
The stored item is identified by a unique string __key and stored as a key:value pair.
When you call the constructor, you must provide a __key. if the value argument is not
undefined or null, the item will be stored immediately. Else, the item won't be stored until
you call this.set().

Note: this only works when run in a browser environment.

Constructor

constructor(
    __storageType: sessionStorage | localStorage,
    
    __key: string,
    
    value?: any = undefined
	// If `value` is defined, the item will be stored on instantiation.
)

Methods

set(value): void
    // Saves item `value` in storage.  Replaces previous value, if any.

get(): any
    // Browser storage always saves the value as a string, so by default 
    // that's the type returned.  But subclasses may want to return the value 
    // in a modified form, so the return type is `any`.

remove(): void
    // Removes both key and value from storage.
    // You can store the item again by calling this.set()

Installation

npm i  @writetome51/item-in-browser-storage

Loading

import {ItemInBrowserStorage} from '@writetome51/item-in-browser-storage';