Skip to content

Commit

Permalink
feat: add local storage
Browse files Browse the repository at this point in the history
  • Loading branch information
wgbx committed Dec 31, 2024
1 parent 4a8ceb4 commit a54a9b2
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 1 deletion.
20 changes: 20 additions & 0 deletions packages/utils/Storage/getLocalStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { isSupport } from '../Type/index'
import { removeLocalStorage } from './removeLocalStorage'

/**
* 从localStorage中获取存储的数据,如果数据已过期,则删除该数据
* @param {string} key - 要获取的数据的键
* @returns {any} 返回存储的数据,如果数据不存在或已过期则返回null
* @example
* console.log(getLocalStorage("userInfo")); // 输出存储的用户信息或null
*/
export function getLocalStorage(key: string) {
if (isSupport('localStorage')) {
const data = JSON.parse(localStorage.getItem(key) ?? '{}')
if (data.expiredTime > new Date().getTime()) {
return data.data
}
removeLocalStorage(key)
return null
}
}
1 change: 1 addition & 0 deletions packages/utils/Storage/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './setLocalStorage'
9 changes: 9 additions & 0 deletions packages/utils/Storage/removeLocalStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* 从localStorage中删除指定的数据
* @param {string} key - 要删除的数据的键
* @example
* removeLocalStorage("userInfo"); // 删除存储的用户信息
*/
export function removeLocalStorage(key: string) {
localStorage.removeItem(key)
}
16 changes: 16 additions & 0 deletions packages/utils/Storage/setLocalStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { isSupport } from '../Type/index'

/**
* 将数据存储在localStorage中,如果浏览器支持的话
* @param {string} key - 存储数据使用的键
* @param {string} data - 要存储的数据
* @param {number} expiredTime - 数据过期时间(秒)
* @example
* setLocalStorage("userInfo", JSON.stringify({name: "John"}), 3600); // 将用户信息存储1小时
*/
export function setLocalStorage(key: string, data: string, expiredTime: number) {
if (isSupport('localStorage')) {
const expired = new Date().getTime() + expiredTime * 1000
localStorage.setItem(key, JSON.stringify({ data, expired }))
}
}
3 changes: 2 additions & 1 deletion packages/utils/Type/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './isString'
export * from './isNumber'
export * from './isNumber'
export * from './isSupport'
16 changes: 16 additions & 0 deletions packages/utils/Type/isSupport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* 检查当前浏览器是否支持指定的API
* @param {string} api - 要检查的API名称
* @param {boolean} [error=true] - 是否在不支持API时输出错误信息
* @returns {boolean} 如果浏览器支持该API,则返回true,否则返回false
* @example
* console.log(isSupport("localStorage")); // 输出 true 或 false
* console.log(isSupport("localStorage", false)); // 输出 true 或 false,不输出错误信息
*/
export function isSupport(api: string, error = true): boolean {
if (!(api in window)) {
error && console.error(`${api} is not supported by this browser.`)
return false
}
return true
}

0 comments on commit a54a9b2

Please sign in to comment.