JavaScript
UserAgent

UserAgent

Represents a user agent string and provides methods for parsing it to determine the browser, device, engine, and operating system.

Usage

To use this module, you first need to import the UserAgent class:

import { UserAgent } from '@ponsetya/core'

Constructor

Creates a new instance of the UserAgent class.

new UserAgent(userAgent?: string): UserAgent

Throws: An error if the UserAgent class is instantiated on the server without a user agent string.

Parameters

userAgent?: string

An optional user agent string. If not specified, the constructor will attempt to use the user agent string from the window.navigator object.

Examples

const ua = new UserAgent(
  'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
)
 
console.log(ua.browser) // 'Chrome'

Accessors

browser

Gets the browser name based on the user agent string.

get browser(): UserAgentBrowser

Examples

const ua = new UserAgent(
  'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
)
 
console.log(ua.browser) // 'Chrome'

device

Gets the device type based on the user agent string.

get device(): UserAgentDevice

Examples

const ua = new UserAgent(
  'Mozilla/5.0 (Linux; Android 9; SAMSUNG SM-G970U1) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/12.1 Chrome/79.0.3945.136 Mobile Safari/537.36'
)
 
console.log(ua.device) // 'Mobile'

engine

Gets the rendering engine name based on the user agent string.

get node(): UserAgentEngine

Examples

const ua = new UserAgent(
  'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0'
)
 
console.log(ua.engine) // 'Gecko'

os

Gets the operating system name based on the user agent string.

get os(): UserAgentOperatingSystem

Examples

const ua = new UserAgent(
  'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0'
)
 
console.log(ua.engine) // 'Gecko'

Methods

toString

Returns the user agent string.

toString(): string

Examples

const ua = new UserAgent(
  'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
)
 
console.log(ua.toString()) // 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'

Type Aliases

User Agent Browser

type UserAgentBrowser =
  | 'Chrome'
  | 'Dolphin'
  | 'Edge'
  | 'Firefox'
  | 'IE'
  | 'MI'
  | 'Opera'
  | 'OperaMini'
  | 'Safari'
  | 'WindowsPhone'
  | 'Yandex'
  | 'Unknown'

User Agent Browser

type UserAgentBrowser =
  | 'Gecko'
  | 'WebKit'
  | 'Presto'
  | 'Trident'
  | 'Blink'
  | 'Unknown'

User Agent Operating System

type UserAgentOperatingSystem =
  | 'Android'
  | 'iOS'
  | 'Linux'
  | 'MacOS'
  | 'Pardus'
  | 'Windows'
  | 'Unknown'

User Agent Device

type UserAgentDevice = 'Mobile' | 'Tablet' | 'Computer' | 'Unknown'