-
Notifications
You must be signed in to change notification settings - Fork 22
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
4 changed files
with
76 additions
and
0 deletions.
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,15 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`getEnvironment 表现正常 1`] = ` | ||
Object { | ||
"android": false, | ||
"browser": true, | ||
"deno": false, | ||
"ios": false, | ||
"miniProgram": false, | ||
"nodejs": true, | ||
"taro": false, | ||
"wechatMiniProgram": false, | ||
"wechatWebView": false, | ||
} | ||
`; |
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,7 @@ | ||
import { getEnvironment } from './getEnvironment' | ||
|
||
describe('getEnvironment', () => { | ||
test('表现正常', () => { | ||
expect(getEnvironment()).toMatchSnapshot() | ||
}) | ||
}) |
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,53 @@ | ||
import { inAndroid } from './inAndroid' | ||
import { inBrowser } from './inBrowser' | ||
import { inDeno } from './inDeno' | ||
import { inIOS } from './inIOS' | ||
import { inMiniProgram } from './inMiniProgram' | ||
import { inNodeJS } from './inNodeJS' | ||
import { inTaro } from './inTaro' | ||
import { inWechatWebView } from './inWechatWebView' | ||
|
||
export interface GetEnvironmentResult { | ||
/** 浏览器 */ | ||
readonly browser: boolean | ||
/** 微信 WebView */ | ||
readonly wechatWebView: boolean | ||
/** 小程序 */ | ||
readonly miniProgram: boolean | ||
/** 微信小程序 */ | ||
readonly wechatMiniProgram: boolean | ||
/** iOS */ | ||
readonly ios: boolean | ||
/** 安卓 */ | ||
readonly android: boolean | ||
/** Node.js */ | ||
readonly nodejs: boolean | ||
/** Deno */ | ||
readonly deno: boolean | ||
/** Taro 3 */ | ||
readonly taro: boolean | ||
} | ||
|
||
let env: GetEnvironmentResult | undefined | ||
|
||
/** | ||
* 获取运行环境信息。 | ||
* | ||
* @returns 返回运行环境信息 | ||
*/ | ||
export function getEnvironment(): GetEnvironmentResult { | ||
if (env == null) { | ||
env = { | ||
browser: inBrowser(), | ||
wechatWebView: inWechatWebView(), | ||
miniProgram: !!inMiniProgram(), | ||
wechatMiniProgram: !!inMiniProgram('微信'), | ||
ios: inIOS(), | ||
android: inAndroid(), | ||
nodejs: inNodeJS(), | ||
deno: inDeno(), | ||
taro: inTaro(), | ||
} | ||
} | ||
return env | ||
} |
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