https://github.com/trapcodeio/browser-storage
Support all browser storage using one Api
https://github.com/trapcodeio/browser-storage
Last synced: 7 months ago
JSON representation
Support all browser storage using one Api
- Host: GitHub
- URL: https://github.com/trapcodeio/browser-storage
- Owner: trapcodeio
- Created: 2020-01-30T16:51:38.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-10-08T05:05:43.000Z (over 1 year ago)
- Last Synced: 2024-11-29T08:42:14.739Z (7 months ago)
- Language: TypeScript
- Size: 79.1 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
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")
```