1.0.2 • Published 12 months ago

@minisss/localstorage v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

@minisss/localstorage

@minisss/localstorage 是一个专为浏览器环境设计的轻量级本地存储库,旨在简化对 localStorage 的操作,同时提供丰富的功能和更好的开发体验。

  • 🤡 支持广泛浏览器
  • ✅ 自定义前缀
  • ✅ 数据过期机制
  • ✅ 支持JSON数据格式
  • ✅ 内置 TS 类型提示(全局定义 IMiniLocalStorage 定义即可)
  • ✅ 支持 ESM CJS UMD

安装方式

# pnpm
pnpm i mini-local-store

# yarn
yarn add mini-local-store

# npm
npm install mini-local-store

# bun
bun install mini-local-store

使用方式

ESM

import MLS from 'mini-local-storage';
MLS.set('key', 'val')
console.log(MLS.get('key'))

CJS

const MLS = require('mini-local-storage');
MLS.set('key', 'val')
console.log(MLS.get('key'))

UMD xxx.html 普通 script 直接导入 -> 访问全局变量 MLS

// UMD 
<script src="https://unpkg.com/@minisss/localstorage@0.0.1-alpha.1"></script>;
<script>
  console.log("MLS 包对象: ", MLS)
  MLS.set('key', 'val')
  console.log(MLS.get('key'))
  console.log(MLS.has('key'))
</script>

UMD xxx.html script type module 模块化内部引入方式

<script type="module">
  import MLS from "https://unpkg.com/@minisss/localstorage@0.0.1-alpha.1/dist/index.esm.js";
  console.log("MLS 包对象: ", MLS)
  MLS.set('key', 'val')
  console.log(MLS.get('key'))
  console.log(MLS.del('key'))
  console.log(MLS.has('key'))
</script>

API

名称描述参数返回值
create创建实例(推荐)create(config?:Config)实例对象
set设置set(key:string, val:any, config?:Config)void
get获取get(key:string, config?:Config)any
has判断has(key:string, config?:Config)boolean
del删除del(key:string, config?:Config)boolean
key获取 keykey(index:number)any
clear清除本地数据clear()void
length本地存储数量number
isSupport是否支持boolean
version版本信息string

实例对象

名称描述示例返回值
set设置set(key:string, val:any, config?:Config)void
get获取get(key:string, config?:Config)any
has判断has(key:string, config?:Config)boolean
del删除del(key:string, config?:Config)boolean
key获取 keykey(index:number)any
clear清除本地数据clear()void
length本地存储数量number
isSupport是否支持boolean
version版本信息string

Config

名称描述数据类型默认值
prefixkey前缀string''
maxAge过期时间,单位为秒number0
expires过期时间(maxAge会覆盖此参数)Date0

支持 TS 类型提示(可选)

// 覆写 IMiniLocalStorage 类型接口
// 例如: 在 type/xxx.d.ts | global.d.ts 或某个 .d.ts 文件中定义 IMiniLocalStorage 类型接口
interface IMiniLocalStorage {
  name:string
  age:number
}
// 将 types/xxx.d.ts 加入到 tsconfig.json includes 中即可获得自定义类型提示功能

License

MIT

1.0.2

12 months ago

1.0.1

12 months ago

1.0.0

12 months ago

0.0.1-alpha.1

12 months ago