项目作者: kmdavis

项目描述 :
Sir Arthur Conan Doyle's The Hound of the User Agent
高级语言: JavaScript
项目地址: git://github.com/kmdavis/baskerville.git
创建时间: 2014-07-08T21:19:56Z
项目社区:https://github.com/kmdavis/baskerville

开源协议:

下载


Baskerville

Sir Arthur Conan Doyle’s The Hound of the User Agent

@kmdavis/baskerville"">NPM Version
Build Status
@kmdavis/baskerville"">Downloads Stats

Baskerville will sniff a user agent string and provide some analysis of what it might mean.

Installation

  1. npm install @kmdavis/baskerville

Usage

Baskerville has 2 main methods: tokenize, and process.

Tokenize will return an array of tokens, for example:

  1. import Baskerville from "@kmdavis/baskerville";
  2. Baskerville.tokenize("Mozilla/5.0 (compatible; Konqueror/4.4; Linux 2.6.32-22-generic; X11; en_US) KHTML/4.4.3 (like Gecko) Kubuntu");

will return the following tokens:

  1. [
  2. {
  3. name: "compatible",
  4. },
  5. {
  6. name: "en_US",
  7. },
  8. {
  9. name: "KHTML",
  10. version: "4.4.3",
  11. like: "Gecko",
  12. },
  13. {
  14. name: "Konqueror",
  15. version: "4.4",
  16. },
  17. {
  18. name: "Kubuntu",
  19. },
  20. {
  21. name: "Linux",
  22. version: "2.6.32-22-generic",
  23. },
  24. {
  25. name: "Mozilla",
  26. version: "5.0",
  27. },
  28. {
  29. name: "X",
  30. version: "11",
  31. },
  32. ]

Process takes that array of tokens and does a little… processing… on it.

  1. Baskerville.process("Mozilla/5.0 (compatible; Konqueror/4.4; Linux 2.6.32-22-generic; X11; en_US) KHTML/4.4.3 (like Gecko) Kubuntu");

will return:

  1. [
  2. {
  3. name: "compatible",
  4. },
  5. {
  6. name: "en_US",
  7. },
  8. {
  9. type: "browser",
  10. name: "KHTML",
  11. version: "4.4.3",
  12. like: "Gecko",
  13. },
  14. {
  15. type: "browser",
  16. name: "Konqueror",
  17. version: "4.4",
  18. },
  19. {
  20. type: "os",
  21. name: "Kubuntu",
  22. },
  23. {
  24. type: "os",
  25. name: "Linux",
  26. version: "2.6.32-22-generic",
  27. },
  28. {
  29. type: "browser",
  30. name: "Mozilla",
  31. version: "5.0",
  32. },
  33. {
  34. name: "X",
  35. version: "11",
  36. },
  37. ]

The built-in processing is rather minimal: it will normalize version strings
(replacing underscores with dots), identify browsers and operating systems, and
the “security token” (N, U, or I). For additional processing, we expose a
registerProcessor method that allows you to create a processor plugin. For
example, you could use this to sort browsers or operating systems into buckets,
like mobile vs desktop, linux-based, etc. You could also use it to convert our
string versions into Version objects:

  1. Baskerville.registerProcessor(function wrapVersions (token) {
  2. if (token.version) {
  3. token.version = new Version(token.version);
  4. }
  5. });

Or you could parse the locale token (“en_US” in the example above):

  1. var carmen = require("@kmdavis/carmen"); // https://github.com/kmdavis/carmen
  2. Baskerville.registerProcessor(function identifyLocale (token) {
  3. var locale = carmen.parse(token.name); // will return undefined if it can't parse
  4. if (locale) {
  5. token.type = "locale";
  6. token.details = locale;
  7. return true; // We assume full ownership and responsibility for this token.
  8. }
  9. return false; // Let the other children play
  10. });

In the example above, if a processor returns true, no further processing will be
performed on that token. Also, don’t be scared to modify the token, you’re not
modifying the original token, but rather a copy. The token or array of tokens
passed into baskerville.process is not modified in any way.

Development setup

  1. npm install
  2. npm test

Release History

  • 0.1.0
    • Initial public release
  • 0.2.0
    • Rewrite using modern javascript
  • 0.2.1
    • Removed unnecessary (boilerplate) dependencies
    • Added a test using large numbers of randomly generated useragent strings
  • 0.2.2
    • Updating README
  • 0.2.3
    • Removing an unused rc file
  • 0.2.4
    • Fixing a licensing issue
  • 0.2.5
    • Switching to webpack
  • 0.2.6
    • Updating readme

Meta

Kevan Davis kevan.davis@me.com

Distributed under the MIT license.

https://github.com/kmdavis/baskerville

Contributing

  1. Fork it (https://github.com/kmdavis/baskerville/fork)
  2. Create your feature branch (git checkout -b feature/fooBar)
  3. Commit your changes (git commit -am 'Add some fooBar')
  4. Push to the branch (git push origin feature/fooBar)
  5. Create a new Pull Request