@writetome51/item-in-browser-storage
TypeScript icon, indicating that this package has built-in type declarations

4.0.0 • Public • Published

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

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

Methods

view 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';

Readme

Keywords

none

Package Sidebar

Install

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

Weekly Downloads

7

Version

4.0.0

License

MIT

Unpacked Size

6.29 kB

Total Files

5

Last publish

Collaborators

  • writetome51