Skip to content

Commit

Permalink
feat: add inWechatWebview
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Oct 24, 2018
1 parent e444352 commit e8c6761
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/inWechatMiniProgram.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import inBrowser from './inBrowser'
import isFunction from './isFunction'
import isObject from './isObject'

declare const wx: any

Expand All @@ -13,7 +14,7 @@ let isInWechatMiniProgram: boolean | undefined
*/
export default function inWechatMiniProgram(callback?: () => void): boolean {
if (isInWechatMiniProgram === undefined) {
isInWechatMiniProgram = !inBrowser() && typeof wx === 'object' && isFunction(wx.getSystemInfo)
isInWechatMiniProgram = !inBrowser() && isObject(wx) && isFunction((wx as any).getSystemInfo)
}
/* istanbul ignore if */
if (isInWechatMiniProgram && isFunction(callback)) {
Expand Down
21 changes: 21 additions & 0 deletions src/inWechatWebview.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import inBrowser from './inBrowser'
import isFunction from './isFunction'

let isInWechatWebview: boolean | undefined

/**
* 检查是否在微信浏览器环境中。
*
* @param [callback] 在微信浏览器环境中执行的回调
* @returns 是(true)或否(false)
*/
export default function inWechatWebview(callback?: () => void): boolean {
if (isInWechatWebview === undefined) {
isInWechatWebview = inBrowser() && /micromessenger/.test(navigator.userAgent.toLowerCase())
}
/* istanbul ignore if */
if (isInWechatWebview && isFunction(callback)) {
callback()
}
return isInWechatWebview
}
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { default as Disposer } from './Disposer'
export { default as base64Decode } from './base64Decode'
export { default as base64Encode } from './base64Encode'
export { default as base64UrlDecode } from './base64UrlDecode'
Expand All @@ -6,13 +7,13 @@ export { default as bindEvent } from './bindEvent'
export { default as castArray } from './castArray'
export { default as clamp } from './clamp'
export { default as cssTransform } from './cssTransform'
export { default as Disposer } from './Disposer'
export { default as fill } from './fill'
export { default as forOwn } from './forOwn'
export { default as getType } from './getType'
export { default as has } from './has'
export { default as inBrowser } from './inBrowser'
export { default as inWechatMiniProgram } from './inWechatMiniProgram'
export { default as inWechatWebview } from './inWechatWebview'
export { default as isArray } from './isArray'
export { default as isBoolean } from './isBoolean'
export { default as isDate } from './isDate'
Expand Down
6 changes: 6 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -686,3 +686,9 @@ describe('inWechatMiniProgram', () => {
expect(callback.notCalled).toBeTruthy()
})
})

describe('inWechatWebview', () => {
test('不在', () => {
expect(vtils.inWechatWebview()).toBeFalsy()
})
})

0 comments on commit e8c6761

Please sign in to comment.