Skip to content

Commit

Permalink
feat: new function - isAndroid
Browse files Browse the repository at this point in the history
  • Loading branch information
GreatAuk committed Mar 6, 2023
1 parent e5c300a commit e699a85
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pnpm add @utopia-utils/dom

* waitForSelector: 等待指定的选择器匹配的元素出现在页面中,如果调用此方法时已经有匹配的元素,那么此方法立即返回。 如果指定的选择器在超时时间后扔不出现,返回 `null`[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/dom/src/waitForSelector.ts)
* panzoom: 为指定的元素添加拖拽缩放功能。[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/dom/src/panzoom/core.ts)
* isAndroid: 判断是否是 Android 系统。[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/dom/src/isAndroid.ts)
* isIOS: 判断是否是 IOS 系统。[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/dom/src/isIOS.ts)
* isWeixin: 判断是否是微信浏览器。[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/dom/src/isWeixin.ts)
* isMobile: 判断是否是移动端浏览器。[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/share/src/isMobile.ts)
Expand Down
3 changes: 2 additions & 1 deletion packages/dom/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './panzoom'
export * from './isMobile'
export * from './isAndroid'
export * from './isIOS'
export * from './isMobile'
export * from './isWeixin'
export * from './waitForSelector'
17 changes: 17 additions & 0 deletions packages/dom/src/isAndroid.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @vitest-environment happy-dom
*/
import { isAndroid } from './isAndroid'

describe('isAndroid', () => {
afterEach(() => {
vi.resetAllMocks()
})
it('return false if not android', () => {
expect(isAndroid()).toBe(false)
})
it('return true if android', () => {
vi.spyOn(navigator, 'userAgent', 'get').mockReturnValueOnce(('Android'))
expect(isAndroid()).toBe(true)
})
})
9 changes: 9 additions & 0 deletions packages/dom/src/isAndroid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Returns a boolean value indicating whether the current platform is Android.
* @returns A boolean value.
*/
export function isAndroid() {
if (typeof navigator === 'undefined')
return false
return /android/i.test(navigator.userAgent)
}
4 changes: 4 additions & 0 deletions packages/dom/src/isIOS.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* If the user agent string contains the word 'iPad', 'iPhone', or 'iPod', then we're on iOS.
* @returns A boolean value.
*/
export function isIOS() {
if (typeof navigator === 'undefined')
return false
Expand Down

0 comments on commit e699a85

Please sign in to comment.