Skip to content

Commit

Permalink
feat(utils): 新增 getEnvironment
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Jul 24, 2020
1 parent 4b31555 commit 6e09372
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/utils/__snapshots__/getEnvironment.test.ts.snap
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,
}
`;
7 changes: 7 additions & 0 deletions src/utils/getEnvironment.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { getEnvironment } from './getEnvironment'

describe('getEnvironment', () => {
test('表现正常', () => {
expect(getEnvironment()).toMatchSnapshot()
})
})
53 changes: 53 additions & 0 deletions src/utils/getEnvironment.ts
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
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export * from './bindEvent'
export * from './dedent'
export * from './EventBus'
export * from './formatNumber'
export * from './getEnvironment'
export * from './getWechatPublicAccountQrcodeUrl'
export * from './inAndroid'
export * from './inBrowser'
Expand Down

0 comments on commit 6e09372

Please sign in to comment.