@@ -21,7 +21,6 @@ const PATTERNS = {
2121 GENERIC_VERSION : / v e r s i o n \/ ( \d + (?: \. \d + ) ? ) / i,
2222} as const ;
2323
24- // Ordered from most specific to least specific
2524const BROWSER_CONFIGS : BrowserConfig [ ] = [
2625 {
2726 name : 'Opera' ,
@@ -55,8 +54,8 @@ const BROWSER_CONFIGS: BrowserConfig[] = [
5554 } ,
5655 {
5756 name : 'Microsoft Edge' ,
58- pattern : / ( e d g e | e d g i o s | e d g a | e d g ) / i,
59- versionPattern : / ( e d g e | e d g i o s | e d g a | e d g ) [ \s \/ ] ( \d + (?: \. \d + ) ? ) / i,
57+ pattern : / (?: e d g e | e d g i o s | e d g a | e d g ) / i,
58+ versionPattern : / (?: e d g e | e d g i o s | e d g a | e d g ) [ \s \/ ] ( \d + (?: \. \d + ) ? ) / i,
6059 } ,
6160 {
6261 name : 'Firefox' ,
@@ -84,33 +83,16 @@ const matchUserAgent = (
8483 userAgent : string ,
8584 position : number ,
8685 pattern : RegExp ,
87- ) : string => {
88- const match = userAgent . match ( pattern ) ;
89- return match ?. [ position ] || '' ;
90- } ;
91-
92- const safeNavigator = ( ) =>
93- typeof navigator !== 'undefined' ? navigator : null ;
94- const safeWindow = ( ) => ( typeof window !== 'undefined' ? window : null ) ;
86+ ) : string => userAgent . match ( pattern ) ?. [ position ] || '' ;
9587
9688function extractVersion ( userAgent : string , config : BrowserConfig ) : string {
97- if ( config . versionPattern ) {
98- const match = userAgent . match ( config . versionPattern ) ;
99- if ( match ) {
100- const index = config . versionPattern . source . includes ( '(' )
101- ? config . name === 'Microsoft Edge'
102- ? 2
103- : 1
104- : 1 ;
105- return match [ index ] || '' ;
106- }
107- }
108- return matchUserAgent ( userAgent , 1 , PATTERNS . GENERIC_VERSION ) ;
89+ if ( ! config . versionPattern )
90+ return matchUserAgent ( userAgent , 1 , PATTERNS . GENERIC_VERSION ) ;
91+ const match = userAgent . match ( config . versionPattern ) ;
92+ return match ?. [ 1 ] || '' ;
10993}
11094
11195export function getBrowser ( userAgent : string ) : IBrowserResult {
112- if ( ! userAgent ) return { name : 'Unknown' , version : '' } ;
113-
11496 for ( const config of BROWSER_CONFIGS ) {
11597 if ( config . pattern . test ( userAgent ) ) {
11698 return { name : config . name , version : extractVersion ( userAgent , config ) } ;
@@ -122,44 +104,30 @@ export function getBrowser(userAgent: string): IBrowserResult {
122104 } ;
123105}
124106
125- function isAndroidDevice ( userAgent : string ) : boolean {
126- return (
127- ! ! userAgent &&
128- ! PATTERNS . LIKE_ANDROID . test ( userAgent ) &&
129- PATTERNS . ANDROID . test ( userAgent )
130- ) ;
131- }
107+ const isAndroidDevice = ( userAgent : string ) : boolean =>
108+ ! PATTERNS . LIKE_ANDROID . test ( userAgent ) && PATTERNS . ANDROID . test ( userAgent ) ;
132109
133110export function getIOSDeviceType ( userAgent : string ) : string {
134- if ( ! userAgent ) return '' ;
135-
136111 let deviceType = matchUserAgent (
137112 userAgent ,
138113 1 ,
139114 PATTERNS . IOS_DEVICES ,
140115 ) . toLowerCase ( ) ;
141116
142- // iPadOS workaround
143- const nav = safeNavigator ( ) ;
144- const win = safeWindow ( ) ;
145117 if (
146118 ! deviceType &&
147- nav ? .platform === 'MacIntel' &&
148- nav ? .maxTouchPoints > 2 &&
149- ! ( win as { MSStream ?: unknown } ) ?. MSStream
119+ navigator . platform === 'MacIntel' &&
120+ navigator . maxTouchPoints > 2 &&
121+ ! ( window as { MSStream ?: unknown } ) ?. MSStream
150122 ) {
151123 deviceType = 'ipad' ;
152124 }
153-
154125 return deviceType ;
155126}
156127
157128export function isTablet ( userAgent : string ) : boolean {
158- if ( ! userAgent ) return false ;
159-
160129 const isAndroid = isAndroidDevice ( userAgent ) ;
161130 const iOSDevice = getIOSDeviceType ( userAgent ) ;
162-
163131 return (
164132 ( PATTERNS . TABLET . test ( userAgent ) && ! PATTERNS . TABLET_PC . test ( userAgent ) ) ||
165133 iOSDevice === 'ipad' ||
@@ -170,12 +138,9 @@ export function isTablet(userAgent: string): boolean {
170138}
171139
172140export function isMobile ( userAgent : string ) : boolean {
173- if ( ! userAgent ) return false ;
174-
175141 const isTabletDevice = isTablet ( userAgent ) ;
176142 const isAndroid = isAndroidDevice ( userAgent ) ;
177143 const iOSDevice = getIOSDeviceType ( userAgent ) ;
178-
179144 return (
180145 ! isTabletDevice &&
181146 ( PATTERNS . MOBILE . test ( userAgent ) ||
0 commit comments