CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-mobile-detect

Device detection library that analyzes User-Agent strings to identify mobile devices, tablets, phones, operating systems, browsers, and device capabilities

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

device-detection.mddocs/

Device Detection

Core device identification functionality for determining device types and manufacturers through User-Agent string analysis.

Capabilities

Mobile Device Detection

Detects if the User-Agent represents a mobile device and returns the manufacturer/family name.

/**
 * Returns the detected mobile device type/family string or null
 * @returns The mobile device manufacturer (e.g., 'Sony', 'Samsung', 'iPhone') or null if not mobile
 *          Possible values: Any phone or tablet key, 'UnknownPhone', 'UnknownTablet', 'UnknownMobile', or null
 */
mobile(): string | null;

Usage Examples:

const MobileDetect = require('mobile-detect');

// iPhone detection
const iphoneMd = new MobileDetect('Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X)');
console.log(iphoneMd.mobile()); // 'iPhone'

// Samsung Android detection  
const samsungMd = new MobileDetect('Mozilla/5.0 (Linux; Android 11; SM-G991B)');
console.log(samsungMd.mobile()); // 'Samsung'

// Desktop detection
const desktopMd = new MobileDetect('Mozilla/5.0 (Windows NT 10.0; Win64; x64)');
console.log(desktopMd.mobile()); // null

Phone Detection

Specifically detects phone devices (excluding tablets) and returns the manufacturer/family.

/**
 * Returns the detected phone type/family string or null
 * @returns The phone manufacturer (e.g., 'iPhone', 'Samsung', 'HTC') or null if not a phone
 */
phone(): string | null;

Detected Phone Families:

  • iPhone, BlackBerry, Pixel, HTC, Nexus, Dell, Motorola
  • Samsung, LG, Sony, Asus, Xiaomi, NokiaLumia, Micromax
  • Palm, Vertu, Pantech, Fly, Wiko, iMobile, SimValley
  • Wolfgang, Alcatel, Nintendo, Amoi, INQ, OnePlus, GenericPhone

Usage Examples:

// iPhone detection
const md1 = new MobileDetect('Mozilla/5.0 (iPhone; CPU iPhone OS 15_0 like Mac OS X)');
console.log(md1.phone()); // 'iPhone'

// Samsung phone detection
const md2 = new MobileDetect('Mozilla/5.0 (Linux; Android 12; SM-G998B)');
console.log(md2.phone()); // 'Samsung'

// iPad (tablet, not phone)
const md3 = new MobileDetect('Mozilla/5.0 (iPad; CPU OS 15_0 like Mac OS X)');
console.log(md3.phone()); // null
console.log(md3.tablet()); // 'iPad'

Tablet Detection

Detects tablet devices and returns the manufacturer/family name.

/**
 * Returns the detected tablet type/family string or null
 * @returns The tablet manufacturer (e.g., 'iPad', 'Samsung', 'Nexus') or null if not a tablet
 */
tablet(): string | null;

Detected Tablet Families:

  • iPad, NexusTablet, GoogleTablet, SamsungTablet, Kindle
  • SurfaceTablet, HPTablet, AsusTablet, BlackBerryTablet, HTCtablet
  • MotorolaTablet, NookTablet, AcerTablet, ToshibaTablet, LGTablet
  • FujitsuTablet, PrestigioTablet, LenovoTablet, DellTablet, YarvikTablet
  • And 80+ other tablet manufacturers/models

Usage Examples:

// iPad detection
const md1 = new MobileDetect('Mozilla/5.0 (iPad; CPU OS 15_0 like Mac OS X)');
console.log(md1.tablet()); // 'iPad'

// Samsung tablet detection
const md2 = new MobileDetect('Mozilla/5.0 (Linux; Android 12; SM-T870)');
console.log(md2.tablet()); // 'SamsungTablet'

// Phone (not tablet)
const md3 = new MobileDetect('Mozilla/5.0 (iPhone; CPU iPhone OS 15_0 like Mac OS X)');
console.log(md3.tablet()); // null
console.log(md3.phone()); // 'iPhone'

Size-Based Phone Detection

Determines if a device should be considered phone-sized based on screen dimensions (browser environments only).

/**
 * Checks whether the mobile device can be considered as phone regarding screen width
 * @param maxPhoneWidth - Optional maximum logical pixels to be considered as phone.
 *                        If not provided, uses constructor maxPhoneWidth (default: 600)
 * @returns true if screen width <= maxPhoneWidth, false otherwise, 
 *          undefined if screen size not detectable (server-side environments)
 */
isPhoneSized(maxPhoneWidth?: number): boolean | undefined;

/**
 * Static version of isPhoneSized method for current screen
 * @param maxPhoneWidth - Maximum logical pixels to be considered as phone (required for static version)
 * @returns true if current screen width <= maxPhoneWidth, false otherwise,
 *          undefined in server-side environments (no window.screen access)
 */
static isPhoneSized(maxPhoneWidth?: number): boolean | undefined;

Usage Examples:

// Instance method - uses constructor maxPhoneWidth or 600px default
const md = new MobileDetect(navigator.userAgent, 768);
console.log(md.isPhoneSized()); // true/false/undefined

// Instance method - override maxPhoneWidth
console.log(md.isPhoneSized(480)); // true/false/undefined

// Static method - check current screen
console.log(MobileDetect.isPhoneSized(600)); // true/false/undefined

// Server-side usage
const serverMd = new MobileDetect(req.headers['user-agent']);
console.log(serverMd.isPhoneSized()); // undefined (no screen access)

Device Categories

The library detects devices across these major categories:

Phone Manufacturers (41+ supported)

  • Apple: iPhone, iPod
  • Google: Pixel, Nexus phones
  • Samsung: Galaxy series, all Samsung phone models
  • Android OEMs: HTC, LG, Sony, Motorola, OnePlus, Xiaomi
  • Other: BlackBerry, Nokia Lumia, Palm, Vertu, and 30+ more

Tablet Manufacturers (100+ supported)

  • Apple: iPad (all models)
  • Google: Nexus tablets, Pixel tablets
  • Samsung: Galaxy Tab series, all Samsung tablet models
  • Amazon: Kindle Fire series
  • Microsoft: Surface tablets
  • Other OEMs: Asus, Acer, Lenovo, HP, Dell, and 90+ more

Implementation Notes

  • Pattern Matching: Uses extensive regex patterns for device identification
  • Caching: Detection results are cached per instance for performance
  • Fallback Detection: Uses detectmobilebrowsers.com patterns as fallback
  • Screen Size: isPhoneSized() only works in browser environments with screen access
  • Updates Required: Device patterns need regular updates as new devices are released

docs

device-detection.md

index.md

system-information.md

version-testing.md

tile.json