<commit_before># detect-browser

This is a package that attempts to detect a browser vendor and version (in
a semver compatible format) using a navigator useragent in a browser or
`process.version` in node.

[![NPM](https://nodei.co/npm/detect-browser.png)](https://nodei.co/npm/detect-browser/)

[![stable](https://img.shields.io/badge/stability-stable-green.svg)](https://github.com/dominictarr/stability#stable) [![Build Status](https://api.travis-ci.org/DamonOehlman/detect-browser.svg?branch=master)](https://travis-ci.org/DamonOehlman/detect-browser) [![Maintainability](https://api.codeclimate.com/v1/badges/84947fce3f3b06da69d0/maintainability)](https://codeclimate.com/github/DamonOehlman/detect-browser/maintainability)

## Installation

[![CDN](https://img.shields.io/badge/CDN-UNPKG-blue.svg)](https://unpkg.com/browse/detect-browser)

[![NPM](https://img.shields.io/badge/Package-npm-purple.svg)](https://www.npmjs.com/package/detect-browser)

---

## Example Usage

```js
const { detect } = require('detect-browser');
const browser = detect();

// handle the case where we don't detect the browser
if (browser) {
  console.log(browser.name);
  console.log(browser.version);
  console.log(browser.os);
}
```

Or you can use a switch statement:

```js
const { detect } = require('detect-browser');
const browser = detect();

// handle the case where we don't detect the browser
switch (browser && browser.name) {
  case 'chrome':
  case 'firefox':
    console.log('supported');
    break;

  case 'edge':
    console.log('kinda ok');
    break;

  default:
    console.log('not supported');
}
```

Additionally, from `5.x` a `type` discriminator is included in the result
should you want to use this (it's a nice convenience in a TS environment).

Contrived example:

```ts
import { detect } from '../src';

const result = detect();
if (result) {
  switch (result.type) {
    case 'bot':
      // result is an instanceof BotInfo
      console.log(`found ${result.name} bot`);
      break;

    case 'bot-device':
      // result is an instanceof SearchBotDeviceInfo
      console.log(`found ${result.name} device bot`);
      break;

    case 'browser':
      // result is an instanceof BrowserInfo
      console.log(`found ${result.name} browser`);
      break;

    case 'node':
      // result is an instanceof NodeInfo
      console.log(`found node version ${result.version}`);
      break;
  }
}
```

**NOTE:** In addition to the the `detect` function, `browserName` and
`detectOS` are provided as exports if you want to only access certain
information.

## Adding additional browser support

The current list of browsers that can be detected by `detect-browser` is
not exhaustive. If you have a browser that you would like to add support for
then please submit a pull request with the implementation.

Creating an acceptable implementation requires two things:

1. A test demonstrating that the regular expression you have defined identifies
   your new browser correctly. Examples of this can be found in the
   `test/logic.js` file.

2. Write the actual regex to the `index.js` file. In most cases adding
   the regex to the list of existing regexes will be suitable (if usage of `detect-brower`
   returns `undefined` for instance), but in some cases you might have to add it before
   an existing regex. This would be true for a case where you have a browser that
   is a specialised variant of an existing browser but is identified as the<commit_msg>Fix typo in README.md, changing brower to browser (#147)<commit_after># detect-browser

This is a package that attempts to detect a browser vendor and version (in
a semver compatible format) using a navigator useragent in a browser or
`process.version` in node.

[![NPM](https://nodei.co/npm/detect-browser.png)](https://nodei.co/npm/detect-browser/)

[![stable](https://img.shields.io/badge/stability-stable-green.svg)](https://github.com/dominictarr/stability#stable) [![Build Status](https://api.travis-ci.org/DamonOehlman/detect-browser.svg?branch=master)](https://travis-ci.org/DamonOehlman/detect-browser) [![Maintainability](https://api.codeclimate.com/v1/badges/84947fce3f3b06da69d0/maintainability)](https://codeclimate.com/github/DamonOehlman/detect-browser/maintainability)

## Installation

[![CDN](https://img.shields.io/badge/CDN-UNPKG-blue.svg)](https://unpkg.com/browse/detect-browser)

[![NPM](https://img.shields.io/badge/Package-npm-purple.svg)](https://www.npmjs.com/package/detect-browser)

---

## Example Usage

```js
const { detect } = require('detect-browser');
const browser = detect();

// handle the case where we don't detect the browser
if (browser) {
  console.log(browser.name);
  console.log(browser.version);
  console.log(browser.os);
}
```

Or you can use a switch statement:

```js
const { detect } = require('detect-browser');
const browser = detect();

// handle the case where we don't detect the browser
switch (browser && browser.name) {
  case 'chrome':
  case 'firefox':
    console.log('supported');
    break;

  case 'edge':
    console.log('kinda ok');
    break;

  default:
    console.log('not supported');
}
```

Additionally, from `5.x` a `type` discriminator is included in the result
should you want to use this (it's a nice convenience in a TS environment).

Contrived example:

```ts
import { detect } from '../src';

const result = detect();
if (result) {
  switch (result.type) {
    case 'bot':
      // result is an instanceof BotInfo
      console.log(`found ${result.name} bot`);
      break;

    case 'bot-device':
      // result is an instanceof SearchBotDeviceInfo
      console.log(`found ${result.name} device bot`);
      break;

    case 'browser':
      // result is an instanceof BrowserInfo
      console.log(`found ${result.name} browser`);
      break;

    case 'node':
      // result is an instanceof NodeInfo
      console.log(`found node version ${result.version}`);
      break;
  }
}
```

**NOTE:** In addition to the the `detect` function, `browserName` and
`detectOS` are provided as exports if you want to only access certain
information.

## Adding additional browser support

The current list of browsers that can be detected by `detect-browser` is
not exhaustive. If you have a browser that you would like to add support for
then please submit a pull request with the implementation.

Creating an acceptable implementation requires two things:

1. A test demonstrating that the regular expression you have defined identifies
   your new browser correctly. Examples of this can be found in the
   `test/logic.js` file.

2. Write the actual regex to the `index.js` file. In most cases adding
   the regex to the list of existing regexes will be suitable (if usage of `detect-browser`
   returns `undefined` for instance), but in some cases you might have to add it before
   an existing regex. This would be true for a case where you have a browser that
   is a specialised variant of an existing browser but is identified as the
{'fraction_non_alphanumeric': 0.12203147353361946, 'fraction_numerical': 0.005007153075822604, 'mean_word_length': 3.895658263305322, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 24, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1101163', 'n_tokens_mistral': 2136, 'n_tokens_neox': 2048, 'n_words': 717}