Package Exports
- client-parser
Readme
client-parser
A TypeScript utility to detect detailed device information from the browser's User Agent string.
Overview
This module provides a getDeviceType function that analyzes the browser's User Agent string to determine:
Operating System: Identifies the OS (e.g., Windows, macOS, iOS, Android, Linux, Windows Phone). Device Type: Classifies the device as PC, mobile, or tablet. Browser: Detects the browser (e.g., Chrome, Firefox, Safari, Edge, Opera, Internet Explorer). Versions: Extracts the OS and browser versions when available.
The utility is designed to be lightweight, accurate, and easy to integrate into web applications. Features
Detects operating systems: Windows, macOS, iOS, Android, Linux, Windows Phone. Identifies device types: PC, mobile, or tablet. Supports major browsers: Chrome, Firefox, Safari, Edge, Opera, Internet Explorer. Returns version information for both OS and browser. TypeScript support with clear interface definitions.
Installation npm install device-type-detection
Usage Import and call the getDeviceType function to retrieve device information: import getDeviceType from 'device-type-detection';
const deviceInfo = getDeviceType(); console.log(deviceInfo);
Example Output { "type": "ios", "os": "iOS", "osVersion": "16.0", "isMobile": true, "isTablet": false, "browser": "Safari", "browserVersion": "16.0" }
Interface The IDeviceInfo interface defines the structure of the returned object: interface IDeviceInfo { type: 'unknown' | 'windows_phone' | 'ios' | 'android' | 'pc'; os?: string; osVersion?: string; isMobile: boolean; isTablet: boolean; browser?: string; browserVersion?: string; }
How It Works The module uses the browser's navigator.userAgent string to detect device and browser information. It employs two helper functions:
_detectOSAndDevice: Identifies the operating system and device type (mobile, tablet, or PC). _detectBrowser: Determines the browser and its version.
The detection prioritizes specific checks (e.g., Windows Phone before Android) to avoid false positives and uses regular expressions to extract version information. Limitations
Relies on User Agent strings, which can be spoofed or incomplete. May not detect niche browsers or operating systems. Browser and OS detection accuracy depends on the User Agent string's format.
Development Prerequisites
Node.js TypeScript npm
Setup
Clone the repository:git clone https://github.com/montasim/device-type-detection.git
Install dependencies:npm install
Build the project:npm run build
Testing Run tests to verify detection accuracy: npm test
Contributing Contributions are welcome! Please submit issues or pull requests to the GitHub repository.
Fork the repository. Create a feature branch (git checkout -b feature/YourFeature). Commit changes (git commit -m 'Add YourFeature'). Push to the branch (git push origin feature/YourFeature). Open a pull request.
License This project is licensed under the CC BY-NC-ND 4.0 License. Contact
Author: Mohammad Montasim -Al- Mamun Shuvo Email: montasimmamun@gmail.com GitHub: montasim