npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

parse-user-agent

v1.1.2

Published

parseUserAgent is a function that takes a user agent and returns the browser name and version (and soon the operating system and version).

Downloads

29

Readme

parseUserAgent

Description

parseUserAgent is a function that takes a user agent and returns the name and version of the corresponding browser & operating system.

The Wheel

There are many libraries and solutions for this problem, but I decided to re-invent the wheel and write my own as I've not found one that did all of the following:

  1. Pattern-based, I don't want to store huge lists of known-good agents
  2. Local, I don't want to send requests to (or pay for) an external API
  3. Reliable, I've tested lots of libraries, many have incorrect results
  4. Specific, I don't just want the main 6~ and then "Other"

Principles & Methodology

  • Name should be the official name and casing/style used by the developers, for example:
    • "Edge" should be "Microsoft Edge", as this is what Microsoft calls it
    • "surf" is the official name of the web browser from suckless.org not "Surf"
  • Name should be the most recent name for that software, for example:
    • Previously officially called "[Microsoft/Windows] Internet Explorer", it is now just "Internet Explorer"
  • Version should be the release version number, not the official public name, for example:
    • "Windows XP" is the official public name, which may refer to "Windows 5.1" or "Windows 5.2"
      • See below for usage of the "parseWindowsVersion" function
  • Version should ideally start with a number, but never start with "v", for example:
    • "IBM WebExplorer /v0.94" should return "IBM WebExplorer" & "0.94"

Usage

PHP

<?php

$user_agent = $_SERVER['HTTP_USER_AGENT'];
// Mozilla/5.0 (Linux; Android 10; BLA-L09) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.96 Mobile Safari/537.36 (Ecosia [email protected])

print_r(
    parseUserAgent($user_agent)
);

The above code will output:

Array
(
    [browser_name] => Ecosia Browser
    [browser_version] => [email protected]
    [operating_system_name] => Android
    [operating_system_version] => 10
    [is_mobile] => 1
)

JavaScript

let user_agent = request.headers["user-agent"];
// Mozilla/5.0 (Linux; Android 10; BLA-L09) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.96 Mobile Safari/537.36 (Ecosia [email protected])

console.table(parseUserAgent(user_agent));

The above code will output:

┌──────────────────────────┬────────────────────────┐
│         (index)          │         Values         │
├──────────────────────────┼────────────────────────┤
│       browser_name       │    'Ecosia Browser'    │
│     browser_version      │ '[email protected]' │
│  operating_system_name   │       'Android'        │
│ operating_system_version │          '10'          │
│        is_mobile         │          true          │
└──────────────────────────┴────────────────────────┘

parseWindowsVersion

Included in both PHP and JavaScript versions, is a function to convert Windows versions to public names:

let user_agent = request.headers["user-agent"];
// Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN) AppleWebKit/523.15 (KHTML, like Gecko, Safari/419.3) Arora/0.3 (Change: 287 c9dfb30)

let user_agent_parsed = parseUserAgent(user_agent);

console.table(user_agent_parsed);

user_agent_parsed = parseWindowsVersion(user_agent_parsed);

console.table(user_agent_parsed);

The above code will output:

┌──────────────────────────┬───────────┐
│         (index)          │  Values   │
├──────────────────────────┼───────────┤
│       browser_name       │  'Arora'  │
│     browser_version      │   '0.3'   │
│  operating_system_name   │ 'Windows' │
│ operating_system_version │   '5.1'   │
│        is_mobile         │   false   │
└──────────────────────────┴───────────┘
┌──────────────────────────┬───────────┐
│         (index)          │  Values   │
├──────────────────────────┼───────────┤
│       browser_name       │  'Arora'  │
│     browser_version      │   '0.3'   │
│  operating_system_name   │ 'Windows' │
│ operating_system_version │   'XP'    │
│        is_mobile         │   false   │
└──────────────────────────┴───────────┘

Function accepts either a JavaScript object/PHP array with "operating_system_version" key, or just a string:

console.log(parseWindowsVersion("5.1")); // XP