-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
64 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './setLocalStorage' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 })) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |