UNPKG

minigame-std

Version:

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

166 lines (165 loc) 4.33 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); let happy_rusty = require("happy-rusty"); //#region src/macros/env.ts /** * 如果在小游戏环境中返回 true,否则返回 false。 */ const IS_MINA = __MINIGAME_STD_MINA__; //#endregion //#region src/std/platform/base.ts /** * 获取当前的平台类型。 * @returns 返回当前的运行环境类型,可能是 'minigame' 或 'web'。 * @since 1.0.0 * @example * ```ts * const type = getTargetType(); * console.log('当前平台:', type); // 'minigame' 或 'web' * ``` */ function getTargetType() { return "wx" in globalThis ? "minigame" : "web"; } /** * 判断当前是否在小游戏环境中。 * @returns 如果在小游戏环境中返回 true,否则返回 false。 * @since 1.0.0 * @example * ```ts * if (isMiniGame()) { * console.log('当前在小游戏环境'); * } * ``` */ function isMiniGame() { return getTargetType() === "minigame"; } //#endregion //#region src/std/platform/user_agent.ts /** * 解析 userAgent 获取设备信息。 * @param ua - 要解析的 userAgent 字符串,默认为 navigator.userAgent。 * @returns 解析后的设备信息,包含 model、platform 和 system。 * @internal */ function parseUserAgent(ua = navigator.userAgent) { const iosMatch = ua.match(/\((iPhone|iPad|iPod);.*?OS (\d+[_\d]*)/); if (iosMatch) return { model: iosMatch[1], platform: "ios", system: `iOS ${iosMatch[2].replace(/_/g, ".")}` }; const androidMatch = ua.match(/Android\s+([\d.]+);(?:.*?;\s*)?([^;]+)\s+Build/); if (androidMatch) { const version = androidMatch[1]; return { model: androidMatch[2].trim(), platform: "android", system: `Android ${version}` }; } const macMatch = ua.match(/Mac OS X (\d+[_\d]*)/); if (macMatch) return { model: "Mac", platform: "mac", system: `macOS ${macMatch[1].replace(/_/g, ".")}` }; const winMatch = ua.match(/Windows NT ([\d.]+)/); if (winMatch) return { model: "PC", platform: "windows", system: `Windows NT ${winMatch[1]}` }; if (ua.includes("Linux")) return { model: "Linux", platform: "linux", system: "Linux" }; return { model: "unknown", platform: "unknown", system: "unknown" }; } //#endregion //#region src/std/platform/device.ts const deviceInfo = /*#__PURE__*/ (0, happy_rusty.Lazy)(() => IS_MINA ? wx.getDeviceInfo ? wx.getDeviceInfo() : wx.getSystemInfoSync() : getWebDeviceInfo()); /** * 获取设备信息。 * @returns 返回小游戏的设备信息对象。 * @since 1.0.0 * @example * ```ts * const info = getDeviceInfo(); * console.log('设备平台:', info.platform); * console.log('设备品牌:', info.brand); * console.log('设备型号:', info.model); * ``` */ function getDeviceInfo() { return deviceInfo.force(); } /** * 获取 Web 环境下的设备信息。 */ function getWebDeviceInfo() { const { model, platform, system } = parseUserAgent(); return { benchmarkLevel: -2, brand: "", memorySize: (navigator.deviceMemory ?? 0) * 1024, model, platform, system }; } //#endregion //#region src/std/platform/target.ts /** * 判断当前是否在小游戏的开发者工具中。 * @returns 如果在小游戏的开发者工具中返回 true,否则返回 false。 * @since 1.9.0 * @example * ```ts * if (isMiniGameDevtools()) { * console.log('在开发者工具中'); * } * ``` */ function isMiniGameDevtools() { return isMiniGame() && getPlatform() === "devtools"; } /** * 获取当前平台类型。 */ function getPlatform() { return getDeviceInfo().platform.toLowerCase(); } //#endregion //#region src/std/performance/mod.ts /** * 性能模块,提供高精度时间戳获取功能。 * @module performance */ /** * 小游戏性能管理器实例(延迟初始化)。 */ const minaPerformance = /*#__PURE__*/ (0, happy_rusty.Lazy)(() => wx.getPerformance()); /** * 参见`performance.now()` * @returns 当前时间以毫秒为单位的时间戳 * @since 2.0.0 * @example * ```ts * const start = getPerformanceNow(); * // 执行一些操作... * const end = getPerformanceNow(); * console.log('耗时:', end - start, 'ms'); * ``` */ function getPerformanceNow() { return IS_MINA ? minaPerformance.force().now() / (isMiniGameDevtools() ? 1 : 1e3) : performance.now(); } //#endregion exports.getPerformanceNow = getPerformanceNow; //# sourceMappingURL=performance.cjs.map