UNPKG

minigame-std

Version:

Cross-platform standard library for WeChat minigame and web browsers with unified APIs for crypto, fs, fetch, storage, and more.

397 lines (396 loc) 10.1 kB
import { validateString } from "./_internal.mjs"; import { Err, Ok, RESULT_VOID, tryResult } from "happy-rusty"; import { asyncIOResultify, syncIOResultify } from "./utils.mjs"; //#region src/macros/env.ts /** * 如果在小游戏环境中返回 true,否则返回 false。 */ const IS_MINA = __MINIGAME_STD_MINA__; //#endregion //#region src/std/storage/mina_storage.ts /** * @internal * 小游戏平台的存储操作实现。 */ /** * 异步设置存储项。 * @param key - 存储键名。 * @param data - 要存储的字符串数据。 * @returns 返回操作结果。 */ async function setItem$2(key, data) { return (await asyncIOResultify(wx.setStorage)({ key, data })).and(RESULT_VOID); } /** * 异步获取存储项。 * @param key - 存储键名。 * @returns 返回存储的字符串数据。 */ async function getItem$2(key) { return (await asyncIOResultify(wx.getStorage)({ key })).map((x) => x.data); } /** * 异步移除存储项。 * @param key - 要移除的存储键名。 * @returns 返回操作结果。 */ async function removeItem$2(key) { return (await asyncIOResultify(wx.removeStorage)({ key })).and(RESULT_VOID); } /** * 异步清空所有存储数据。 * @returns 返回操作结果。 */ async function clear$2() { return (await asyncIOResultify(wx.clearStorage)()).and(RESULT_VOID); } /** * 异步获取存储项数量。 * @returns 返回存储项的数量。 */ async function getLength$2() { return (await getStorageKeys()).map((x) => x.length); } /** * 异步检查存储项是否存在。 * @param key - 要检查的存储键名。 * @returns 返回是否存在的布尔值。 */ async function hasItem$2(key) { return (await getStorageKeys()).map((x) => x.includes(key)); } /** * 同步设置存储项。 * @param key - 存储键名。 * @param data - 要存储的字符串数据。 * @returns 返回操作结果。 */ function setItemSync$1(key, data) { return syncIOResultify(wx.setStorageSync)(key, data); } /** * 同步获取存储项。 * @param key - 存储键名。 * @returns 返回存储的字符串数据。 */ function getItemSync$1(key) { return syncIOResultify(wx.getStorageSync)(key); } /** * 同步移除存储项。 * @param key - 要移除的存储键名。 * @returns 返回操作结果。 */ function removeItemSync$1(key) { return syncIOResultify(wx.removeStorageSync)(key); } /** * 同步清空所有存储数据。 * @returns 返回操作结果。 */ function clearSync$1() { return syncIOResultify(wx.clearStorageSync)(); } /** * 同步获取存储项数量。 * @returns 返回存储项的数量。 */ function getLengthSync$1() { return getStorageKeysSync().map((x) => x.length); } /** * 同步检查存储项是否存在。 * @param key - 要检查的存储键名。 * @returns 返回是否存在的布尔值。 */ function hasItemSync$1(key) { return getStorageKeysSync().map((x) => x.includes(key)); } /** * 获取所有存储键名。 */ async function getStorageKeys() { return (await asyncIOResultify(wx.getStorageInfo)()).map((x) => x.keys); } /** * 同步获取所有存储键名。 */ function getStorageKeysSync() { return syncIOResultify(wx.getStorageInfoSync)().map((x) => x.keys); } //#endregion //#region src/std/storage/web_storage.ts /** * @internal * Web 平台的存储操作实现。 */ /** * 设置存储项。 * @param key - 存储键名。 * @param data - 要存储的字符串数据。 * @returns 返回操作结果。 */ function setItem$1(key, data) { return tryResult(() => { localStorage.setItem(key, data); }); } /** * 获取存储项。 * @param key - 存储键名。 * @returns 返回存储的字符串数据,若不存在则返回错误。 */ function getItem$1(key) { const data = localStorage.getItem(key); return data == null ? Err(/* @__PURE__ */ new Error(`Key '${key}' does not exist`)) : Ok(data); } /** * 移除存储项。 * @param key - 要移除的存储键名。 * @returns 返回操作结果。 */ function removeItem$1(key) { localStorage.removeItem(key); return RESULT_VOID; } /** * 清空所有存储数据。 * @returns 返回操作结果。 */ function clear$1() { localStorage.clear(); return RESULT_VOID; } /** * 获取存储项数量。 * @returns 返回存储项的数量。 */ function getLength$1() { return Ok(localStorage.length); } /** * 检查存储项是否存在。 * @param key - 要检查的存储键名。 * @returns 返回是否存在的布尔值。 */ function hasItem$1(key) { return Ok(localStorage.getItem(key) != null); } //#endregion //#region src/std/storage/mod.ts /** * 将数据存储在本地缓存中。 * @param key - 数据的键名。 * @param data - 要存储的数据。 * @returns 存储操作的异步结果。 * @since 1.0.0 * @example * ```ts * const result = await setItem('username', 'john'); * if (result.isOk()) { * console.log('存储成功'); * } * ``` */ async function setItem(key, data) { const keyRes = validateString(key, "key"); if (keyRes.isErr()) return keyRes; const dataRes = validateString(data, "data"); if (dataRes.isErr()) return dataRes; return IS_MINA ? setItem$2(key, data) : setItem$1(key, data); } /** * 从本地缓存中读取数据。 * @param key - 数据的键名。 * @returns 包含数据的异步结果,如果不存在则返回空字符串。 * @since 1.0.0 * @example * ```ts * const result = await getItem('username'); * if (result.isOk()) { * console.log('用户名:', result.unwrap()); * } * ``` */ async function getItem(key) { const keyRes = validateString(key, "key"); if (keyRes.isErr()) return keyRes.asErr(); return IS_MINA ? getItem$2(key) : getItem$1(key); } /** * 从本地缓存中移除指定的数据。 * @param key - 数据的键名。 * @returns 移除操作的异步结果。 * @since 1.0.0 * @example * ```ts * const result = await removeItem('username'); * if (result.isOk()) { * console.log('移除成功'); * } * ``` */ async function removeItem(key) { const keyRes = validateString(key, "key"); if (keyRes.isErr()) return keyRes; return IS_MINA ? removeItem$2(key) : removeItem$1(key); } /** * 清除所有的本地存储数据。 * @returns 清除操作的异步结果。 * @since 1.0.0 * @example * ```ts * const result = await clear(); * if (result.isOk()) { * console.log('所有数据已清除'); * } * ``` */ function clear() { return IS_MINA ? clear$2() : Promise.resolve(clear$1()); } /** * 获取本地存储数据的项数。 * @returns 包含存储项数的异步结果。 * @since 1.2.0 * @example * ```ts * const result = await getLength(); * if (result.isOk()) { * console.log('存储项数:', result.unwrap()); * } * ``` */ function getLength() { return IS_MINA ? getLength$2() : Promise.resolve(getLength$1()); } /** * 检查本地存储中是否存在指定的数据。 * @param key - 数据的键名。 * @returns 包含是否存在的布尔值的异步结果。 * @since 1.9.3 * @example * ```ts * const result = await hasItem('username'); * if (result.isOk() && result.unwrap()) { * console.log('键存在'); * } * ``` */ async function hasItem(key) { const keyRes = validateString(key, "key"); if (keyRes.isErr()) return keyRes.asErr(); return IS_MINA ? hasItem$2(key) : hasItem$1(key); } /** * `setItem` 的同步版本,将数据存储在本地缓存中。 * @param key - 数据的键名。 * @param data - 要存储的数据。 * @returns 存储操作的结果。 * @since 1.0.0 * @example * ```ts * const result = setItemSync('username', 'john'); * if (result.isOk()) { * console.log('存储成功'); * } * ``` */ function setItemSync(key, data) { const keyRes = validateString(key, "key"); if (keyRes.isErr()) return keyRes; const dataRes = validateString(data, "data"); if (dataRes.isErr()) return dataRes; return (IS_MINA ? setItemSync$1 : setItem$1)(key, data); } /** * `getItem` 的同步版本,从本地缓存中读取数据。 * @param key - 数据的键名。 * @returns 包含数据的操作结果。 * @since 1.0.0 * @example * ```ts * const result = getItemSync('username'); * if (result.isOk()) { * console.log('用户名:', result.unwrap()); * } * ``` */ function getItemSync(key) { const keyRes = validateString(key, "key"); if (keyRes.isErr()) return keyRes.asErr(); return (IS_MINA ? getItemSync$1 : getItem$1)(key); } /** * `removeItem` 的同步版本,从本地缓存中移除指定的数据。 * @param key - 数据的键名。 * @returns 移除操作的结果。 * @since 1.0.0 * @example * ```ts * const result = removeItemSync('username'); * if (result.isOk()) { * console.log('移除成功'); * } * ``` */ function removeItemSync(key) { const keyRes = validateString(key, "key"); if (keyRes.isErr()) return keyRes; return (IS_MINA ? removeItemSync$1 : removeItem$1)(key); } /** * `clear` 的同步版本,清除所有的本地存储数据。 * @returns 清除操作的结果。 * @since 1.0.0 * @example * ```ts * const result = clearSync(); * if (result.isOk()) { * console.log('所有数据已清除'); * } * ``` */ function clearSync() { return (IS_MINA ? clearSync$1 : clear$1)(); } /** * `getLength` 的同步版本,获取本地存储数据的项数。 * @returns 包含存储项数的操作结果。 * @since 1.2.0 * @example * ```ts * const result = getLengthSync(); * if (result.isOk()) { * console.log('存储项数:', result.unwrap()); * } * ``` */ function getLengthSync() { return (IS_MINA ? getLengthSync$1 : getLength$1)(); } /** * `hasItem` 的同步版本,检查本地存储中是否存在指定的数据。 * @param key - 数据的键名。 * @returns 包含是否存在的布尔值的操作结果。 * @since 1.9.3 * @example * ```ts * const result = hasItemSync('username'); * if (result.isOk() && result.unwrap()) { * console.log('键存在'); * } * ``` */ function hasItemSync(key) { const keyRes = validateString(key, "key"); if (keyRes.isErr()) return keyRes.asErr(); return (IS_MINA ? hasItemSync$1 : hasItem$1)(key); } //#endregion export { clear, clearSync, getItem, getItemSync, getLength, getLengthSync, hasItem, hasItemSync, removeItem, removeItemSync, setItem, setItemSync }; //# sourceMappingURL=storage.mjs.map