# local-data-storage

> Just another LocalStorage wrapper.

Latest version **0.4.4** (published 2024-09-17) · ISC license · 0 weekly downloads

## Install

```sh
npm install local-data-storage
pnpm add local-data-storage
yarn add local-data-storage
bun add local-data-storage
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.4 |
| Published | 2024-09-17 |
| First published | 2019-09-09 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 23.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Márk Munkácsi |
| Maintainers | munkacsimark |
| Keywords | localstorage, local-storage |

## Links

- npm: https://www.npmjs.com/package/local-data-storage
- Repository: https://github.com/munkacsimark/local-data-storage
- Issues: https://github.com/munkacsimark/local-data-storage/issues
- npm.io page: https://npm.io/package/local-data-storage

## Alternatives

- [localforage](https://npm.io/package/localforage.md) — 6.2M weekly downloads
- [localforage-observable](https://npm.io/package/localforage-observable.md) — 30.8K weekly downloads
- [@y/y](https://npm.io/package/@y/y.md) — 30.1K weekly downloads
- [@metaobjectsdev/render](https://npm.io/package/@metaobjectsdev/render.md) — 3.5K weekly downloads
- [@ledgerhq/coin-algorand](https://npm.io/package/@ledgerhq/coin-algorand.md) — 1.1K weekly downloads

## Recent versions

- 0.4.4 (latest) — 2024-09-17
- 0.4.3 — 2023-07-15
- 0.4.2 — 2022-03-24
- 0.4.1 — 2022-03-24
- 0.4.0 — 2022-03-24
- 0.3.0 — 2021-08-27
- 0.2.4 — 2021-08-06
- 0.2.3 — 2020-11-23
- 0.2.2 — 2020-11-23
- 0.2.0 — 2019-09-10
- 0.1.1 — 2019-09-09
- 0.1.0 — 2019-09-09

## README

# Local Data Storage

It's a simple **wrapper** over browsers native **localStorage**. It uses "smart" items and can work with existing localStorage entries. It always returns success of operation as boolean.

## Installation

```bash
$ npm install local-data-storage
```

## Usage

| Name                  | Parameters                                                         | Description                           | Return value           |
| --------------------- | ------------------------------------------------------------------ | ------------------------------------- | ---------------------- |
| `isAvailable()`       |                                                                    | It tests if localStorage is available | boolean                |
| `itemExists()`        | key: string                                                        | It check existence of an item         | boolean                |
| `clearExpiredItems()` |                                                                    | Clears expired items                  | boolean                |
| `getItem()`           | key: string                                                        | Return an item by key                 | \*LocalDataStorageItem |
| `setItem()`           | key: string, obj: \*LocalDataStorageItem, forceOverwrite?: boolean | Saves an item with given key          | boolean                |
| `removeItem()`        | key: string                                                        | Removes an item by key                | boolean                |
| `clear()`             |                                                                    | Clears the whole storage              | boolean                |

### Examples:

```javascript
import {
  isAvailable,
  itemExists,
  clearExpiredItems,
  getItem,
  setItem,
  removeItem,
  clear,
} from "local-data-storage";

if (isAvailable()) {
  /* do something */
}

if (itemExists("cat")) {
  /* do something */
}

clearExpiredItems();

const myItem = getItem("cat");

setItem("cat", { value: "meow" });
setItem("willExpireCat", { value: "meow", expiryDate: Date.now() + 86_400 });
setItem("cat", { value: "OVERWRITTEN" }, true);

removeItem("cat");

clear();
```

## \*LocalDataStorageItem

| Property    | Type   | Description                                                                                                    |
| ----------- | ------ | -------------------------------------------------------------------------------------------------------------- |
| value       | string | This will always exist, will contain unparseable items from storage as well.                                   |
| createdDate | number | This is a timestamp of creation. It will be set automatically at saving.                                       |
| expiryDate  | number | This is an optional property. It's a timestamp of expiry, will be deleted by `clearExpiredItems()` if expired. |

### Example:

```javascript
{
	value: 'cat',
	createdDate: 86400,
	expiryDate: 86401
}
```

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