https://github.com/writetome51/item-in-browser-storage
An abstract TypeScript/JavaScript class representing an item stored in the browser's localStorage or sessionStorage
https://github.com/writetome51/item-in-browser-storage
browser-storage javascript typescript
Last synced: 18 days ago
JSON representation
An abstract TypeScript/JavaScript class representing an item stored in the browser's localStorage or sessionStorage
- Host: GitHub
- URL: https://github.com/writetome51/item-in-browser-storage
- Owner: writetome51
- License: mit
- Created: 2019-05-23T21:21:18.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2020-11-30T21:22:11.000Z (over 5 years ago)
- Last Synced: 2025-02-05T07:18:59.879Z (over 1 year ago)
- Topics: browser-storage, javascript, typescript
- Language: TypeScript
- Size: 22.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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
```ts
constructor(
__storageType: sessionStorage | localStorage,
__key: string,
value?: any = undefined
// If `value` is defined, the item will be stored on instantiation.
)
```
## Methods
view methods
```ts
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
```bash
npm i @writetome51/item-in-browser-storage
```
## Loading
```js
import {ItemInBrowserStorage} from '@writetome51/item-in-browser-storage';
```