(ua: string, model?: string)
| 304 | } |
| 305 | |
| 306 | export function getDevice(ua: string, model?: string) { |
| 307 | // Check for known phone models first (from top brands) |
| 308 | // This handles app-style UAs where the model is explicitly stated |
| 309 | if (isKnownPhoneModel(model) || isKnownPhoneModel(ua)) { |
| 310 | return 'mobile'; |
| 311 | } |
| 312 | |
| 313 | // Samsung mobile devices use SM-[A,G,N,etc]XXX pattern |
| 314 | const isSamsungMobile = SAMSUNG_MOBILE_REGEX.test(ua); |
| 315 | if (isSamsungMobile) { |
| 316 | return 'mobile'; |
| 317 | } |
| 318 | |
| 319 | // Samsung tablets use SM-TXXX pattern |
| 320 | if (SAMSUNG_TABLET_REGEX.test(ua)) { |
| 321 | return 'tablet'; |
| 322 | } |
| 323 | |
| 324 | // LG mobile devices use LG-XXXX pattern |
| 325 | const isLGMobile = LG_MOBILE_REGEX.test(ua); |
| 326 | if (isLGMobile) { |
| 327 | return 'mobile'; |
| 328 | } |
| 329 | |
| 330 | // Check for mobile patterns |
| 331 | const mobile1 = MOBILE_REGEX_1.test(ua); |
| 332 | const mobile2 = MOBILE_REGEX_2.test(ua.slice(0, 4)); |
| 333 | |
| 334 | if (mobile1 || mobile2) { |
| 335 | return 'mobile'; |
| 336 | } |
| 337 | |
| 338 | // Check for tablet patterns |
| 339 | // Note: We already checked for Samsung mobile/tablet and LG mobile above |
| 340 | const isAndroid = ANDROID_REGEX.test(ua); |
| 341 | const hasMobileKeyword = MOBILE_KEYWORD_REGEX.test(ua); |
| 342 | |
| 343 | // Only consider it a tablet if it's explicitly a tablet device |
| 344 | // Don't default Android to tablet just because "mobile" keyword is missing |
| 345 | const tablet = TABLET_REGEX.test(ua); |
| 346 | |
| 347 | if (tablet) { |
| 348 | return 'tablet'; |
| 349 | } |
| 350 | |
| 351 | // For Android without explicit mobile/tablet indicators and no known model, |
| 352 | // check if there's any brand/model info suggesting it's a phone |
| 353 | if (isAndroid && !hasMobileKeyword && !isSamsungMobile && !isLGMobile) { |
| 354 | // Extract model from app-style UA if present |
| 355 | const appInfo = extractAppStyleInfo(ua); |
| 356 | if (appInfo.model && isKnownPhoneModel(appInfo.model)) { |
| 357 | return 'mobile'; |
| 358 | } |
| 359 | // If we have a brand but no clear device type, assume mobile for Android |
| 360 | const brand = detectBrand(ua, appInfo.model); |
| 361 | if (brand) { |
| 362 | return 'mobile'; |
| 363 | } |
no test coverage detected