super-device-detect
TypeScript icon, indicating that this package has built-in type declarations

1.1.2 • Public • Published

super-device-detect

super-device-detect is a precise user agent parser and device detector written in TypeScript, backed by the largest and most up-to-date open-source user agent database.

This is a javascript port of Matomo device-detector (4.2.3).

Features

  • No dependencies
  • TypeScript support
  • Thoroughly tested
  • New iphone version

Getting Started

Installation

To use super-device-detect in your project, run:

npm install super-device-detect

Usage

ES2015 import:
import DeviceDetector from "super-device-detect";
TypeScript import:
import DeviceDetector = require("super-device-detect");

Example - user agent detection:

import DeviceDetector from "super-device-detect";

const deviceDetector = new DeviceDetector();
const userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 Safari/537.36";
const device = deviceDetector.parse(userAgent);

console.log(device);

Output:

{
  "client": {
    "type": "browser",
    "name": "Chrome",
    "version": "69.0",
    "engine": "Blink",
    "engineVersion": ""
  },
  "os": {
    "name": "Mac",
    "version": "10.13",
    "platform": ""
  },
  "device": {
    "type": "desktop",
    "brand": "Apple",
    "model": ""
  },
  "bot": null
}

Example - bot detection:

import BotDetector from "super-device-detect/dist/parsers/bot";

const botDetector = new BotDetector();
const userAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25 (compatible; Googlebot-Mobile/2.1; +http://www.google.com/bot.html)";
const bot = botDetector.parse(userAgent);

if (bot) {
  console.log(bot);
}

Output:

{
  "name": "Googlebot",
  "category": "Search bot",
  "url": "http://www.google.com/bot.html",
  "producer": {
    "name": "Google Inc.",
    "url": "http://www.google.com"
  }
}

API Documentation

new DeviceDetector([options])

  • options <[Object]> Options object which might have the following properties:
    • skipBotDetection <[boolean]> If true, bot detection will completely be skipped (bots will be detected as regular devices). Defaults to false.
    • versionTruncation <[0 | 1 | 2 | 3 | null]> Passing null disables version truncation, so full versions will be returned. Defaults to 1, only minor versions will be returned (e.g. X.Y).

TypeScript

Type definitions are included in this library and exposed via:

import { DeviceDetectorResult, DeviceDetectorOptions } from "super-device-detect";

License

This is a free/libre library under license LGPL v3 or later.

Package Sidebar

Install

npm i super-device-detect

Weekly Downloads

4

Version

1.1.2

License

LGPL-3.0

Unpacked Size

1.65 MB

Total Files

101

Last publish

Collaborators

  • thinhcodeunited