# lasting-stack

> The data can be stored in a specific way, such as cookie, session storage, local storage, or on your own server

Latest version **1.0.4** (published 2020-11-26) · ISC license · 0 weekly downloads

## Install

```sh
npm install lasting-stack
pnpm add lasting-stack
yarn add lasting-stack
bun add lasting-stack
```

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2020-11-26 |
| First published | 2020-11-25 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 14.5 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | laizhibin |
| Maintainers | laizhibin |
| Keywords | stack, lasting, lasting-stack, persistence, cookie, localStorage, sessionStorage |

## Links

- npm: https://www.npmjs.com/package/lasting-stack
- Repository: https://github.com/bill-lai/lasting-stack
- Issues: https://github.com/bill-lai/lasting-stack/issues
- npm.io page: https://npm.io/package/lasting-stack

## Dependencies (3)

- [js-cookie](https://npm.io/package/js-cookie.md) ^2.2.1
- [@babel/runtime](https://npm.io/package/@babel/runtime.md) ^7.12.1
- [@babel/plugin-transform-runtime](https://npm.io/package/@babel/plugin-transform-runtime.md) ^7.12.1

## Alternatives

- [gamedig](https://npm.io/package/gamedig.md) — 29.3K weekly downloads
- [join-monster](https://npm.io/package/join-monster.md) — 12.8K weekly downloads
- [masked](https://npm.io/package/masked.md) — 5.5K weekly downloads
- [@comunica/actor-query-process-explain-logical](https://npm.io/package/@comunica/actor-query-process-explain-logical.md) — 4.7K weekly downloads
- [@veracity/vui](https://npm.io/package/@veracity/vui.md) — 4.6K weekly downloads

## Recent versions

- 1.0.4 (latest) — 2020-11-26
- 1.0.3 — 2020-11-26
- 1.0.2 — 2020-11-25
- 1.0.1 — 2020-11-25
- 1.0.0 — 2020-11-25

## README

# lasting-stack
对数据进行栈式管理，可以选用特定方式存储，比如cookie，sessionStorage，localStorage，或者自己服务器上

## 安装
```javascript
npm install lasting-stack
```

## 使用
```
import lastingStack from 'lasting-stack'

const stack = lastingStack({
  name: 'aa',
  decode: JSON.parse,
  encode: JSON.stringify
})

stack.push({ name: 'lzb', sex: 1 })
stack.push({ name: 'bill', sex: 1 })

// { name: 'lzb', sex: 1 }
stack.get(0)
// { name: 'bill', sex: 1 }
stack.pop()
```

## lastingStack API

### lastingStack([config [, isAsync])
构造一个stack对象，注意 <i>isAsync</i> 传入true与false返回对象不一样<br>
当 <i>isAsync</i> 为false时返回 [StorageStack](#StorageStack)
当 <i>isAsync</i> 为true时返回 [AyncStorageStack](#AyncStorageStack)

#### config
```
{
  // 标识符，注意如果需要持久化除了选用特定模式name值应该也要保存起来，
  // 下次传入即可恢复对象,如果不提供则每次都新建
  name?: 'aa',
  // 提供编码接口可以对持久化的数据进行编码，存储的对象即为编码后的数据
  encode?: JSON.stringify,
  // 提供解吗接口与encode相对应，每次获取数据进行解吗再返回
  decode?: JSON.parse
}
```

#### StorageStack
|  方法   | 说明  | 
|  ----  | ----  |
| push(data: T) : T | 向栈顶推入数据 |
| pop() : T | 栈定推出数据 |
| getData() : T | 获取栈顶数据 |
| get(index: number) : T | 获取指定位置的数据 |
| clear(): void | 清除栈数据 |


#### AyncStorageStack
|  方法   | 说明  | 
|  ----  | ----  |
| load(cb: function) : void | 栈初始化完成后的回调 |
| push(data: T) : Promise\<T\> | 向栈顶推入数据 |
| pop() : Promise\<T\> | 栈定推出数据 |
| getData() : Promise\<T\> | 获取栈顶数据 |
| get(index: number) : Promise\<T\> | 获取指定位置的数据 |
| clear(): Promise\<void\> | 清除栈数据 |



### lastingStack.setMode(mode)
设置栈存储的方式，mode为字符串，现在已提供 <i>object</i>，<i>cookie</i>，<i>sessionStorage</i>，<i>localStorage</i>  4种模式

|  mode   | 说明  | 
|  ----  | ----  |
| object | 内存中存储，不可持久化 |
| cookie | cookie中存储，可以持久化 |
| session | sessionStorage中存储，可以持久化，关闭标签后不可恢复 |
| local | localStorage中存储，可以持久化 |


### lastingStack.installMode(name, storage)
为lastingStack添加模式，添加后可在 [setMode](#lastingStack.setMode(mode))中使用<br>
name 为字符串，为模式的名称比如 <i>aliy</i><br>
storage 为 [Storage](#Storage)或[AyncStorage](#AyncStorage)对象

#### Storage
```
{
  // 删除一条栈元素
  removeItem: (key: string) => void,
  // 添加或是设置一条栈元素
  setItem: (key: string, val: T) => void,
  // 获取某条栈元素设置一条栈元素
  getItem: (key: string) => T
}
```
#### AyncStorage
```
{
  // 删除一条栈元素
  removeItem: (key: string) => Promise<void>
  // 添加或是设置一条栈元素
  setItem: (key: string, val: T) => Promise<void>,
  // 获取某条栈元素设置一条栈元素
  getItem: (key: string) => Promise<T>
}
```

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