-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve platform detection, add tests
- Loading branch information
Showing
4 changed files
with
67 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
type Platform = 'android' | 'ios' | ||
|
||
export function detectPlatform(context: Window): Platform | null { | ||
const { | ||
navigator: { maxTouchPoints, platform, userAgent }, | ||
} = context | ||
|
||
if (/android/i.test(userAgent)) { | ||
return 'android' | ||
} | ||
|
||
if ( | ||
/iPad|iPhone|iPod/.test(userAgent) || | ||
(platform === 'MacIntel' && maxTouchPoints > 1) | ||
) { | ||
return 'ios' | ||
} | ||
|
||
return null | ||
} | ||
|
||
export default Platform |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { detectPlatform } from '../Platform' | ||
|
||
describe('detectPlatform', () => { | ||
it('should detect iPhone', () => { | ||
const context = { | ||
navigator: { | ||
userAgent: | ||
'Mozilla/5.0 (iPhone; CPU iPhone OS 16_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148', | ||
platform: 'iPhone', | ||
maxTouchPoints: 5, | ||
}, | ||
} as Window | ||
|
||
expect(detectPlatform(context)).toEqual('ios') | ||
}) | ||
|
||
it('should detect newer iPads', () => { | ||
const context = { | ||
navigator: { | ||
userAgent: | ||
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko)', | ||
platform: 'MacIntel', | ||
maxTouchPoints: 5, | ||
}, | ||
} as Window | ||
|
||
expect(detectPlatform(context)).toEqual('ios') | ||
}) | ||
|
||
it('should detect older iPads', () => { | ||
const context = { | ||
navigator: { | ||
userAgent: | ||
'Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10', | ||
platform: 'MacIntel', | ||
maxTouchPoints: 5, | ||
}, | ||
} as Window | ||
|
||
expect(detectPlatform(context)).toEqual('ios') | ||
}) | ||
}) |
This file was deleted.
Oops, something went wrong.