Skip to content

Commit

Permalink
feat: add inWechatMiniProgram
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Oct 23, 2018
1 parent 007ba2b commit 3a24ee6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/inWechatMiniProgram.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import inBrowser from './inBrowser'
import isFunction from './isFunction'

declare const wx: any

let isInWechatMiniProgram: boolean | undefined

/**
* 检查是否在微信小程序环境中。
*
* @param [callback] 在微信小程序环境中执行的回调
* @returns 是(true)或否(false)
*/
export default function inWechatMiniProgram(callback?: () => void): boolean {
if (isInWechatMiniProgram === undefined) {
isInWechatMiniProgram = !inBrowser() && typeof wx === 'object' && isFunction(wx.getSystemInfo)
}
/* istanbul ignore if */
if (isInWechatMiniProgram && isFunction(callback)) {
callback()
}
return isInWechatMiniProgram
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ 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 isArray } from './isArray'
export { default as isBoolean } from './isBoolean'
export { default as isDate } from './isDate'
Expand Down
9 changes: 9 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -677,3 +677,12 @@ describe('mapValues', () => {
})
})
})

describe('inWechatMiniProgram', () => {
test('inWechatMiniProgram', () => {
const callback = sinon.fake()
expect(vtils.inWechatMiniProgram()).toBeFalsy()
expect(vtils.inWechatMiniProgram(callback)).toBeFalsy()
expect(callback.notCalled).toBeTruthy()
})
})

0 comments on commit 3a24ee6

Please sign in to comment.