1.0.10 • Published 2 years ago

type-storage-utils v1.0.10

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

介绍

带类型的本地存储,同时还提供设置过期时间,支持的数据类型有:

  • string

  • number

  • boolean

  • null

  • undefined

  • array

  • object

  • date

下载

npm

npm i type-storage-utils

src

<script src="../dist/type-storage-utils.js"></script>

使用

// npm
import { localStorage, sessionStorage } from 'type-storage-utils'

// src
const { localStorage, sessionStorage } = typeStorage

APIs

setItem

/**
 * 设置本地存储,支持设置过期时间,当访问过期的数据时,会返回 null。
 * 
 * @param {string} key - 键名
 * @param {*} value - 键值
 * @param {(number|Date)} [expiresOrDate] - 过期时间,接收数字或日期
 *  - 数字 60 代表将在一分钟后过期
 *  - 日期 Sun Jul 24 2022 12:54:33 GMT+0800 (中国标准时间) 代表将在这个时间过期
 */
localStorage.setItem('foo', { bar: 'bar' }, 60)

getItem

/**
 * 获取本地存储 
 * 
 * @param {string} key - 键名
 * @returns {*} 键值
 */
const value = localStorage.getItem('foo') // output: { bar: 'bar' }
Object.prototype.toString.call(value) // output: [object Object]

removeItem

/**
 * 清除指定键名的本地存储 
 * 
 * @param {string} key - 键名
 */
localStorage.removeItem('foo')

clear

/** 清除所有的本地存储 */
localStorage.clear()

used

/**
 * 查询已使用的存储容量
 * 
 * @returns {string} 返回已使用的存储容量
 */
localStorage.used() // 0.000045%
1.0.2

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.10

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago