faisalman / ua-parser-js

"Unmask Your Traffic" - UAParser.js: The Essential Web Development Tool for User-Agent Detection
https://uaparser.dev/
GNU Affero General Public License v3.0
9.28k stars 1.19k forks source link

Easier way of combining all extensions #718

Closed opablo closed 5 months ago

opablo commented 7 months ago

While reading the 2.0.0-beta documentation here: https://docs.uaparser.js.org/v2/api/submodules/extensions/overview.html

I learned that the required way of combining multiple Extensions is like this:

const botAndCLIParser = new UAParser(userAgentString, { browser : [...Bots.browser, ...CLIs.browser] })

it would be so much intuitive and simple to use if the library supports simply this:

const botAndCLIParser = new UAParser(userAgentString, [Bots, CLIs])

as an example of how ugly it can get... I wanted to implement the parsing of all possible known user agents and my code ended up like this:

  const uaParser = new UAParser(userAgent, {
    browser: [...Apps.browser, ...Bots.browser, ...CLIs.browser, ...Emails.browser, ...MediaPlayers.browser, ...Modules.browser],
    device: ExtraDevices.device
  })
  const uaParams = uaParser.getResult()

when it could have been as intuitive and simple as this:

  const uaParser = new UAParser(userAgent, [Apps, Bots, CLIs, Emails, ExtraDevices, MediaPlayers, Modules])
  const uaParams = uaParser.getResult()
faisalman commented 2 weeks ago

Thank you for this valuable suggestion, it's now possible to combine multiple extensions easily

import { UAParser } from 'ua-parser-js';
import { Bots, Emails, InApps, MediaPlayers } from 'ua-parser-js/extensions';

const userAgent = 'curl/7.38.0';
const uaParser = new UAParser(userAgent, [Bots, Emails, InApps, MediaPlayers]);
const uaParams = uaParser.getResult();