Mini UAParser implementation for Binance web projects
npm install @binance/ua-parser-urlA lightweight User-Agent parser for Binance web projects, designed to replace ua-parser-js with a smaller, more focused implementation.
- 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
``bash`
npm install @binance/ua-parser-url
`typescript
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')
`
`typescript
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" }
`
`typescript
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" }
`
`typescript
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" }
`
- Chrome
- Chrome WebView
- Safari
- Mobile Safari
- Firefox
- Edge
- WebKit (fallback)
- macOS
- iOS
- Windows
- Android
- Linux
- iPhone (mobile)
- iPad (tablet)
- Android devices (mobile)
- Desktop devices
#### Constructor
`typescript`
new UAParser(userAgent?: string)
- userAgent (optional): Custom user agent string. If not provided, uses navigator.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.
`typescript`
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
}
}
This library is designed to be a drop-in replacement for ua-parser-js in most cases:
`typescript
// 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()
`
1. Smaller size: ~5KB vs ~40KB
2. Focused functionality: Only implements features used in Binance projects
3. Simplified API: Some advanced features are not implemented
4. Better TypeScript support: Native TypeScript implementation
`bash`
npm run build
`bash`
npm test
`bash``
npm run lint
MIT