# @trapcode/browser-storage

> Support all browser storage using one Api

Latest version **2.4.2** (published 2022-05-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install @trapcode/browser-storage
pnpm add @trapcode/browser-storage
yarn add @trapcode/browser-storage
bun add @trapcode/browser-storage
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.4.2 |
| Published | 2022-05-10 |
| First published | 2020-01-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 32.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | trapcodeio |
| Maintainers | trapcode |

## Links

- npm: https://www.npmjs.com/package/@trapcode/browser-storage
- Repository: https://github.com/trapcodeio/browser-storage
- Homepage: https://github.com/trapcodeio/browser-storage#readme
- Issues: https://github.com/trapcodeio/browser-storage/issues
- npm.io page: https://npm.io/package/@trapcode/browser-storage

## Recent versions

- 2.4.2 (latest) — 2022-05-10
- 2.4.1 — 2022-03-01
- 2.4.0 — 2022-01-01
- 2.3.2 — 2021-12-28
- 2.3.1 — 2021-12-28
- 2.3.0 — 2021-12-28
- 2.2.4 — 2021-12-28
- 2.2.3 — 2021-12-07
- 2.2.2 — 2021-12-07
- 2.2.1 — 2021-12-07
- 2.2.0 — 2021-12-07
- 2.1.6 — 2021-08-09
- 2.1.5 — 2021-08-07
- 2.1.4 — 2021-08-07
- 2.1.3 — 2021-08-07
- … 14 more at https://npm.io/package/@trapcode/browser-storage/versions

## README

# Browser Storage

A simple package for handling `localstorage` & `sessionStorage`.

### Installation

```shell
npm i @trapcode/browser-storage
# OR
yarn add @trapcode/browser-storage
```

### Problem

The native `localstorage` and `sessionStorage` stores every data as `string` by default, This makes **setting** and **
getting** other types difficult. For example:

```javascript
localStorage.setItem("isVerified", true)
localStorage.setItem("age", 18)
localStorage.setItem("object", {foo: "bar"})

// All will return as string regardless
localStorage.getItem("isVerified") // "true" as string
localStorage.getItem("age") // "18" as string
localStorage.getItem("object") // "[Object Object]" as string
```

From the above example, you will have to do some conversions to get the original data type stored.

### Solution

The way the browser stores data cannot be modified but how you **set & get** data can make a huge difference. This
package includes **semantic helper methods** that adds some sort of **type certainty**.

For example:

```javascript
// Import and initialize store.
import BrowserStorage from '@trapcode/browser-storage';

const localStore = BrowserStorage.getLocalStore();

localStore.setBoolean("isVerified", true)
localStore.setNumber("age", 18)
localStore.setObject("object", {foo: "bar"})

// You get the exact types.
localStore.getBoolean("isVerified") // true as boolean
localStore.getNumber("age") // 18 as number
localStore.getObject("object", {foo: "bar"}) // {foo: "bar"} as object
```

### Base64Encryption

if you don't want the values in your store to be humanly readable, you can enable `Base64Encryption`. Your data will
be **Encoded** on `set` actions and **Decoded** automatically on `get` actions

```javascript
import BrowserStorage from '@trapcode/browser-storage';

// Initialize
const localStore = BrowserStorage.getLocalStore();

// Enable Encryption
localStore.enableBase64Encryption()

// Also accepts true/false value as argument
localStore.enableBase64Encryption(true / false)

// For example
localStore.enableBase64Encryption(process.env.NODE_ENV === "production")
```

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