@binance/ua-parser-url
v0.0.1
Published
Mini UAParser implementation for Binance web projects
Maintainers
Readme
@binance/ua-parser-url
A lightweight User-Agent parser for Binance web projects, designed to replace ua-parser-js with a smaller, more focused implementation.
Features
- Lightweight: Only ~5KB minified, compared to ~40KB for ua-parser-js
- Focused: Only implements the functionality actually used in Binance projects
- TypeScript: Full TypeScript support with proper type definitions
- Compatible: API compatible with ua-parser-js for easy migration
Installation
npm install @binance/ua-parser-urlUsage
Basic Usage
import { UAParser } from '@binance/ua-parser-url'
// Create parser with current user agent
const parser = new UAParser()
// Get complete result
const result = parser.getResult()
console.log(result)
// {
// ua: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)...",
// browser: { name: "Chrome", version: "126.0.0.0", major: "126" },
// engine: { name: "Blink", version: "126.0.0.0" },
// os: { name: "macOS", version: "10.15.7" },
// device: { model: "Macintosh", type: "desktop", vendor: "Apple" },
// cpu: { architecture: undefined }
// }
// Create parser with custom user agent
const customParser = new UAParser('Custom User Agent String')Device Detection
import { UAParser } from '@binance/ua-parser-url'
const parser = new UAParser()
// Get device information
const device = parser.getDevice()
console.log(device)
// { model: "iPhone", type: "mobile", vendor: "Apple" }Browser Detection
import { UAParser } from '@binance/ua-parser-url'
const parser = new UAParser()
const result = parser.getResult()
console.log(result.browser)
// { name: "Chrome", version: "126.0.0.0", major: "126" }OS Detection
import { UAParser } from '@binance/ua-parser-url'
const parser = new UAParser()
const result = parser.getResult()
console.log(result.os)
// { name: "macOS", version: "10.15.7" }Supported Browsers
- Chrome
- Chrome WebView
- Safari
- Mobile Safari
- Firefox
- Edge
- WebKit (fallback)
Supported Operating Systems
- macOS
- iOS
- Windows
- Android
- Linux
Supported Devices
- iPhone (mobile)
- iPad (tablet)
- Android devices (mobile)
- Desktop devices
API Reference
UAParser
Constructor
new UAParser(userAgent?: string)userAgent(optional): Custom user agent string. If not provided, usesnavigator.userAgent
Methods
getResult(): IUAParser
Returns the complete parsing result.
getDevice(): IUAParser['device']
Returns device information (model, type, vendor).
setUA(userAgent: string): UAParser
Sets a custom user agent string and returns the parser instance for chaining.
getUA(): string
Returns the current user agent string.
Types
interface IUAParser {
ua: string
browser: {
name: string | undefined
version: string | undefined
major: string | undefined
}
engine: {
name: string | undefined
version: string | undefined
}
os: {
name: string | undefined
version: string | undefined
}
device: {
model: string | undefined
type: string | undefined
vendor: string | undefined
}
cpu: {
architecture: string | undefined
}
}Migration from ua-parser-js
This library is designed to be a drop-in replacement for ua-parser-js in most cases:
// Before (ua-parser-js)
import { UAParser } from 'ua-parser-js'
const parser = new UAParser()
const result = parser.getResult()
// After (@binance/ua-parser-url)
import { UAParser } from '@binance/ua-parser-url'
const parser = new UAParser()
const result = parser.getResult()Differences from ua-parser-js
- Smaller size: ~5KB vs ~40KB
- Focused functionality: Only implements features used in Binance projects
- Simplified API: Some advanced features are not implemented
- Better TypeScript support: Native TypeScript implementation
Development
Building
npm run buildTesting
npm testLinting
npm run lintLicense
MIT
