Skip to content
On this page

UAParser Class Overview

Constructor

new UAParser(uastring?: string, extensions?: Record<string, RegexMap>, headers?: Record<string, string>): UAParser

When called with the new keyword, it will return a new instance of UAParser.

js
const parser = new UAParser("your user-agent here"); // you need to pass the user-agent for nodejs
console.log(parser); 
/* 
    {}
*/

const parserResults = parser.getResult();
console.log(parserResults);
/* 
    {
        ua      : "",
        browser : {},
        engine  : {},
        os      : {},
        device  : {},
        cpu     : {}
    }
*/

UAParser(uastring?: string, extensions?: Record<string, RegexMap>, headers?: Record<string, string>): IResult

When called without the new keyword, it will directly return the results of getResult():

js
const parser = UAParser("your user-agent here");
console.log(parser);
/* 
    {
        ua      : "",
        browser : {},
        engine  : {},
        os      : {},
        device  : {},
        cpu     : {}
    }
*/

TIP

In browser environment you don't need to pass the user-agent string, as it should automatically get the string from the current window.navigator.userAgent.

TIP

In Node.js environment, user-agent string must be passed in order for the function to work. Usually you can find it in: request.headers["user-agent"].

Methods

The methods are self explanatory, here's a small overview of available methods:

getBrowser(): IBrowser

returns the browser name, version, and major.

getCPU(): ICPU

returns CPU architectural design name.

getDevice(): IDevice

returns the device model, type, vendor.

getEngine(): IEngine

returns the browser engine name and version.

getOS(): IOS

returns the operating system name and version.

getResult(): IResult

returns all function object calls, user-agent string, browser info, cpu, device, engine, os.

getUA(): string

returns the user-agent string.

setUA(ua: string): UAParser

set a custom user-agent string to be parsed.

Licensed under the MIT License.