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

melanite

v1.2.8

Published

User-Agent to TAL Device matching

Downloads

2,767

Readme

melanite

Convert a user-agent to a normalised device.

Installation

npm install --save melanite

Usage

Getting started

In order to use melanite, you need to provide one or more "matchers"; a matcher represents a device you wish to identify. Below is an example matcher that could be used to identify a Microsoft Xbox One device:

{
  "brand": "microsoft",
  "model": "xbox-one",
  "invariants": [
    "Xbox One"
  ],
  "disallowed": [],
  "fuzzy": "Mozilla/5.0(Windows NT 10.0; Win64; x64; Xbox; Xbox One) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063",
  "type": "tv"
}

Let's take a look at each component of a matcher (all of which are mandatory).

brand

The brand of the device; it serves no purpose for identification and is merely a friendly name for a group of several different devices (e.g. by manufacturer)

model

The model of the device; like brand, it serves no purpose for identification and is instead a friendly name for a specific device.

invariants

invariants is an array of strings (each of these is referred to as an invariant); in order for a user agent to be matched to this matcher, it must contain every invariant (similar to an allowlist).

disallowed

disallowed is an array of strings, it is in the opposite of invariants. In order for a user agent to be matched to this matcher, it must not contain any of these strings (similar to a denylist).

In the above example, we haven't specified any disallowed items. The disallowed property is most useful when you have two or more matchers that are very similar to each other.

fuzzy

fuzzy can be thought of as an example user agent. When a group of matchers have been filtered using invariants and disallowed, melanite calculates the Levenshtein distance between the fuzzy and the user-agent - the matcher with the lowest Levenshtein distance is the one returned by melanite.

type

The type of the device; like brand and model, it serves no purpose for identification.

Identifying devices

Now that we have a matcher, let's use it to identify some user agents:

const melanite = require('melanite')

const userAgents = [
  'Mozilla/5.0(Windows NT 10.0; Win64; x64; Xbox; Xbox One) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.713.12 Safari/57.36 Edge/15.4063',
  'Some very strange user agent that we do not know about'
]

const matchers = [
  {
    "brand": "microsoft",
    "model": "xbox-one",
    "invariants": [
	  "Xbox One"
    ],
    "disallowed": [],
    "fuzzy": "Mozilla/5.0(Windows NT 10.0; Win64; x64; Xbox; Xbox One) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063",
    "type": "tv"
  }
]

const identifyDevice = melanite.match(matchers)

const devices = userAgents.map((userAgent) => identifyDevice(userAgent))

console.log(devices)
/*
[
  { brand: 'microsoft', model: 'xbox-one', type: 'tv' },
  { brand: 'generic', model: 'device', type: 'unknown' }
]
*/

For a further example, please see example.js.