https://github.com/azzgo/azz-storage
https://github.com/azzgo/azz-storage
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/azzgo/azz-storage
- Owner: azzgo
- Created: 2021-06-08T15:56:11.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2023-06-23T14:34:31.000Z (almost 2 years ago)
- Last Synced: 2025-02-08T12:11:54.393Z (4 months ago)
- Language: TypeScript
- Size: 61.5 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Azz Storage
[](https://badge.fury.io/js/azz-storage)
This is a handy wrapper for web localStorage for now
Support localStorage, sessionStorage, and InMemory Storage
## Installation
```sh
npm install --save azz-storage
```## Usage
```ts
import { LocalStorage } from "azz-storage";
// import { SessionStorage } from "azz-storage";
// import { MemoryStorage } from "azz-storage";const lStore = new LocalStorage("__pro");
lStore.set("token", "logined"); // Will save key as __protoken in localStorage
lStore.get("token"); // "logined"
```with parser like vue-ls
```ts
const vueLSLikeParser: IStorageParser = {
getVal(val: string, defaultVal: unknown): unknown {
try {
const data = JSON.parse(val);
return data.value;
} catch (e) {
return defaultVal;
}
},
setVal(val: unknown) {
return JSON.stringify({
value: val,
});
},
};const localVueLikeStore = new LocalStorage("__vuels", vueLSLikeParser);
```## API
### Storage
**Contructor**
```js
import { LocalStorage } from "azz-storage";
// SessionStorage and MemoryStorage have same api with LocalStorageconst lStore = new LocalStorage([keyPrefix, parser]);
```| argument | type | description | default | required |
| :-------- | :---------------- | :----------------------------------------------------------------------------- | --------- | -------- |
| keyPrefix | string | the key prefix for preventing conflict in your web app with other library | undefined | true |
| parser | IAzzStorageParser | for sepecific use, liking auto JSON parse/stringify like vue-ls or do some log | undefined | false |**Instance APi**
```js
lStore.get([key, defaultVal]);
lStore.set([key, val]);
lStore.remove([key]);
```| method | argument | type | description | default | required |
| :----- | ---------- | ------- | --------------------------------------------------------------- | --------- | -------- |
| get | key | string | the storage key | undefined | true |
| get | defaultVal | unknown | when the target value not defined, you will get the default one | undefined | false |
| set | key | string | same as get method key | undefined | true |
| set | val | unknown | the val you want to save | undefined | true |
| remove | key | string | remove key mapped value | undefined | true |