matias-storage
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

mt-storage

web storage 的二次封装、提供TS类型支持

说明

matias-storage支持保存objectstringbooleannumber类型的数据。其他类型的数据可以包装在object中使用,其实这个库主要也是使用stringify对实现object的支持。同时通过泛型支持在读取的时候指定类型。以前都是每个项目处理,感觉可以简单封装一下。

目前支持持久化目的地只支持localStoragesessionStorage,后面考虑扩展其他。

注意我平常使用object比较简单,如果数据比较大,可能有性能问题,后续考虑支持DB

使用

安装

  • pnpm导入

$ pnpm add -D matias-storage

  • yarn导入

$ yarn add -D matias-storage

  • npm导入

$ npm install -D matias-storage

引入

import { WebStorageType, storageWrite, storageRead, storageRemove, storageRemoveAll } from 'matias-storage'

interface TestType {
    value: number
}

const obj: TestType = {
    value: 100
}
// 默认使用localStorage
storageWrite('LOCL_OBJECT', obj)
// 使用sessionStorage
storageWrite('SESSION_OBJECT', obj, WebStorageType.SESSION)

interface TestType {
    value: number
}
const localObjectValue = storageRead<TestType>('LOCL_OBJECT')
console.log(typeof localObjectValue?.value) // number

删除

storageRemove('LOCL_OBJECT')

清除

storageRemoveAll()

版本

0.1.0

  • 支持objectstringbooleannumber类型存储。
  • 支持localStoragesessionStorage存储。
  • 读取支持泛型指定类型。

Package Sidebar

Install

npm i matias-storage

Weekly Downloads

1

Version

0.1.0

License

ISC

Unpacked Size

24.6 kB

Total Files

17

Last publish

Collaborators

  • matiastang