Skip to content

Commit

Permalink
feat(utils): 新增 inTaro
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Jul 22, 2020
1 parent 7685872 commit 19ad7f3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/utils/inTaro.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { inTaro } from './inTaro'

describe('inTaro', () => {
test('表现正常', () => {
expect(inTaro()).toBeFalse()
Object.defineProperty(navigator, 'product', {
value: 'Taro',
})
expect(inTaro()).toBeTrue()
})
})
9 changes: 9 additions & 0 deletions src/utils/inTaro.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* 检查是否在 [Taro 3](https://taro.js.org/) 中。
*
* @returns 返回检查结果
*/
export function inTaro(): boolean {
// ref: https://github.com/NervJS/taro/blob/next/packages/taro-runtime/src/bom/navigator.ts#L16
return typeof navigator !== 'undefined' && navigator.product === 'Taro'
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export * from './indent'
export * from './inIOS'
export * from './inMiniProgram'
export * from './inNodeJS'
export * from './inTaro'
export * from './inWechat'
export * from './isChineseIDCardNumber'
export * from './isNumeric'
Expand Down

0 comments on commit 19ad7f3

Please sign in to comment.