Skip to content

Environment 环境方法 API

本文逐项记录 @fast-china/utils 的 Environment 环境公开函数。

isBrowser 运行环境

判断当前运行时是否具有浏览器 windowdocument

签名

ts
export function isBrowser(): boolean;

示例

ts
import { isBrowser } from "@fast-china/utils";

const result = isBrowser();

输入

该方法没有输入参数。

返回

返回值返回值类型返回值说明
resultboolean两项能力均存在时返回 true;不读取 DOM 内容。

isWebWorker 运行环境

判断当前运行时是否像 Web Worker 且不是 Window。

签名

ts
export function isWebWorker(): boolean;

示例

ts
import { isWebWorker } from "@fast-china/utils";

const result = isWebWorker();

输入

该方法没有输入参数。

返回

返回值返回值类型返回值说明
resultboolean具有 importScripts 且不是浏览器 Window 时返回 true

isNode 运行环境

判断当前运行时是否暴露 Node.js 版本标记。

签名

ts
export function isNode(): boolean;

示例

ts
import { isNode } from "@fast-china/utils";

const result = isNode();

输入

该方法没有输入参数。

返回

返回值返回值类型返回值说明
resultbooleanprocess.versions.node 为字符串时返回 true

isUniApp 运行环境

判断当前运行时是否暴露 uni-app 的 uni 运行时对象。

签名

ts
export function isUniApp(): boolean;

示例

ts
import { isUniApp } from "@fast-china/utils";

const result = isUniApp();

输入

该方法没有输入参数。

返回

返回值返回值类型返回值说明
resultbooleanuni 标识符或兼容全局属性存在时返回 true;不调用任何平台 API。

hasWebCrypto 加密能力

判断当前运行时是否具备本库完整加密 API 所需的 Web Crypto 能力。

签名

ts
export function hasWebCrypto(): boolean;

示例

ts
import { hasWebCrypto } from "@fast-china/utils";

const result = hasWebCrypto();

输入

该方法没有输入参数。

返回

返回值返回值类型返回值说明
resultboolean同时提供本库 Web Crypto 功能所需方法时返回 true

detectRuntime 运行环境

返回当前主要运行环境。

签名

ts
export function detectRuntime(): RuntimeKind;

示例

ts
import { detectRuntime } from "@fast-china/utils";

const result = detectRuntime();

输入

该方法没有输入参数。

返回

返回值返回值类型返回值说明
resultRuntimeKindbrowserworkernode 或无法识别时的 unknown

isMobileUserAgent 移动设备

基于 User-Agent 启发式判断手机设备。

签名

ts
export function isMobileUserAgent(userAgent: string = currentUserAgent()): boolean;

示例

ts
import { isMobileUserAgent } from "@fast-china/utils";

const result = isMobileUserAgent("Mozilla/5.0 (iPhone; Mobile)");

输入

输入值输入值类型必填/默认值输入值说明
userAgentstring否,默认 currentUserAgent()默认读取当前 navigator.userAgent;平台对象不存在时使用空字符串。

返回

返回值返回值类型返回值说明
resultboolean命中手机特征时返回 true

isTabletUserAgent 移动设备

基于 User-Agent 与触点数量启发式判断平板设备。

签名

ts
export function isTabletUserAgent(userAgent: string = currentUserAgent(), maxTouchPoints: number = currentTouchPoints()): boolean;

示例

ts
import { isTabletUserAgent } from "@fast-china/utils";

const result = isTabletUserAgent("Mozilla/5.0 (iPad)", 5);

输入

输入值输入值类型必填/默认值输入值说明
userAgentstring否,默认 currentUserAgent()默认读取当前 User-Agent。
maxTouchPointsnumber否,默认 currentTouchPoints()用于识别桌面 User-Agent 模式下的 iPadOS,默认读取当前触点数。

返回

返回值返回值类型返回值说明
resultboolean命中平板特征时返回 true