# dom-storage

> W3C DOM Storage (localStorage and sessionStorage) for node.js

Latest version **2.1.0** (published 2018-03-28) · (MIT or Apache-2.0) license · 0 weekly downloads

## Install

```sh
npm install dom-storage
pnpm add dom-storage
yarn add dom-storage
bun add dom-storage
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2018-03-28 |
| First published | 2012-05-19 |
| Weekly downloads | 0 |
| License | (MIT or Apache-2.0) |
| TypeScript types | none |
| Module format | CommonJS |
| Node | * |
| Dependencies | 0 |
| Unpacked size | 17.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | AJ ONeal |
| Maintainers | coolaj86 |

## Links

- npm: https://www.npmjs.com/package/dom-storage
- Repository: https://git.coolaj86.com/coolaj86/dom-storage.js
- npm.io page: https://npm.io/package/dom-storage

## Recent versions

- 2.1.0 (latest) — 2018-03-28
- 2.0.2 — 2016-01-21
- 2.0.1 — 2014-03-08
- 2.0.0 — 2014-01-29
- 1.0.2 — 2013-01-26
- 1.0.0 — 2012-05-19

## README

sessionStorage & localStorage for NodeJS
===

| **dom-storage**
| [atob](https://git.coolaj86.com/coolaj86/atob.js)
| [btoa](https://git.coolaj86.com/coolaj86/btoa.js)
| [unibabel.js](https://git.coolaj86.com/coolaj86/unibabel.js)
| Sponsored by [ppl](https://ppl.family)


An inefficient, but as W3C-compliant as possible using only pure JavaScript, `DOMStorage` implementation.

Purpose
----

This is meant for the purpose of being able to run unit-tests and such for browser-y modules in node.

Usage
----

```javascript
var Storage = require('dom-storage');

// in-file, doesn't call `String(val)` on values (default)
var localStorage = new Storage('./db.json', { strict: false, ws: '  ' });

// in-memory, does call `String(val)` on values (i.e. `{}` becomes `'[object Object]'`
var sessionStorage = new Storage(null, { strict: true });

var myValue = { foo: 'bar', baz: 'quux' };

localStorage.setItem('myKey', myValue);
myValue = localStorage.getItem('myKey');

// use JSON to stringify / parse when using strict w3c compliance
sessionStorage.setItem('myKey', JSON.stringify(myValue));
myValue = JSON.parse(localStorage.getItem('myKey'));
```

API
---

  * getItem(key)
  * setItem(key, value)
  * removeItem(key)
  * clear()
  * key(n)
  * length

### Options

  * strict - whether to stringify strictly as text `[Object object]` or as json `{ foo: bar }`.
  * ws - the whitespace to use saving json to disk. Defaults to `'  '`.

Tests
---

```javascript
0 === localStorage.length;
null === localStorage.getItem('doesn\'t exist');
undefined === localStorage['doesn\'t exist'];

localStorage.setItem('myItem');
'undefined' === localStorage.getItem('myItem');
1 === localStorage.length;

localStorage.setItem('myItem', 0);
'0' === localStorage.getItem('myItem');

localStorage.removeItem('myItem', 0);
0 === localStorage.length;

localStorage.clear();
0 === localStorage.length;
```

Notes
---

  * db is read in synchronously
  * No callback when db is saved
  * Doesn't not emit `Storage` events (not sure how to do)

License
-------

Code copyright 2012-2018 AJ ONeal

Dual-licensed MIT and Apache-2.0

Docs copyright 2012-2018 AJ ONeal

Docs released under Creative Commons.

---
_Source: https://npm.io/package/dom-storage · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
