@jedmao/storage
Version:
A Storage class that implements the Storage interface of the Web Storage API.
50 lines (35 loc) • 2.26 kB
Markdown
# @jedmao/storage
[](https://github.com/jedmao/storage/actions)
[](https://www.npmjs.org/package/@jedmao/storage)
[](https://www.npmjs.org/package/@jedmao/storage)
[](https://codecov.io/gh/jedmao/storage)
[](https://bundlephobia.com/result?p=@jedmao/storage)
[](https://bundlephobia.com/result?p=@jedmao/storage)
[](https://github.com/prettier/prettier)
[](https://twitter.com/sindresorhus/status/457989012528316416?ref_src=twsrc%5Etfw&ref_url=https%3A%2F%2Fwww.quora.com%2FWhat-does-the-unicorn-approved-shield-mean-in-GitHub)
[](https://nodei.co/npm/@jedmao/storage/)
A `Storage` class that implements the [Storage](https://developer.mozilla.org/en-US/docs/Web/API/Storage) interface of the [Web Storage API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API).
This class comes out-of-the-box with [TypeScript][] type annotations and [TSDoc](https://github.com/Microsoft/tsdoc#tsdoc) comments.
## Installation
```
$ npm install [--save[-dev]] @jedmao/storage
```
## Usage
```ts
import { Storage } from '@jedmao/storage'
const data = { bar: 42 }
const storage = new Storage()
storage.setItem('foo', data)
storage.getItem('foo') // "[object Object]"
storage.setItem('foo', JSON.stringify(data))
storage.getItem('foo') // "{"bar":42}"
storage.setItem('baz', 42)
storage.getItem('baz') // '42'
storage.key(1) // 'baz'
storage.length // 2
storage.removeItem('foo')
storage.length // 1
storage.clear()
storage.length // 0
```
[typescript]: http://www.typescriptlang.org/