@alitu/user-agents-v2-api
Advanced tools
Comparing version 0.1.6 to 0.2.1
@@ -5,5 +5,6 @@ import type { OPAWGPatternEntry } from './user-agents-v2'; | ||
* @param {string} userAgent User-Agent string | ||
* @param {string} [referrer] Referer string | ||
* @returns {OPAWGPatternEntry | undefined} OPAWG pattern entry, undefined if not found | ||
*/ | ||
export default function parseUserAgent(userAgent: string): OPAWGPatternEntry | undefined; | ||
export default function parseUserAgent(userAgent: string, referrer?: string): OPAWGPatternEntry | undefined; | ||
export { OPAWGFileType } from './user-agents-v2'; |
@@ -30,5 +30,6 @@ "use strict"; | ||
* @param {string} userAgent User-Agent string | ||
* @param {string} [referrer] Referer string | ||
* @returns {OPAWGPatternEntry | undefined} OPAWG pattern entry, undefined if not found | ||
*/ | ||
function parseUserAgent(userAgent) { | ||
function parseUserAgent(userAgent, referrer) { | ||
// Skip empty strings | ||
@@ -43,4 +44,5 @@ if (!userAgent) | ||
sanitizedUserAgent = sanitizedUserAgent.replace(/(\r\n|\n|\r)/g, ''); | ||
var userAgentMatch; | ||
// Iterate lists in order | ||
for (var _i = 0, files_1 = files; _i < files_1.length; _i++) { | ||
filesLoop: for (var _i = 0, files_1 = files; _i < files_1.length; _i++) { | ||
var entries = files_1[_i]; | ||
@@ -51,2 +53,12 @@ for (var _a = 0, entries_1 = entries; _a < entries_1.length; _a++) { | ||
if (sanitizedUserAgent.match(entry.regexp)) { | ||
userAgentMatch = entry; | ||
break filesLoop; | ||
} | ||
} | ||
} | ||
// Attempt to lookup referrers if possible | ||
if (userAgentMatch.type === 'browsers' && referrer) { | ||
for (var _b = 0, _c = user_agents_v2_1.default.referrers; _b < _c.length; _b++) { | ||
var entry = _c[_b]; | ||
if (referrer.match(entry.regexp)) { | ||
return entry; | ||
@@ -56,6 +68,5 @@ } | ||
} | ||
// If no match is found, return undefined | ||
return undefined; | ||
return userAgentMatch; | ||
} | ||
var user_agents_v2_2 = require("./user-agents-v2"); | ||
Object.defineProperty(exports, "OPAWGFileType", { enumerable: true, get: function () { return user_agents_v2_2.OPAWGFileType; } }); |
@@ -25,1 +25,5 @@ "use strict"; | ||
}); | ||
test('identifies web apps by referrers', function () { | ||
var entry = (0, _1.default)('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36', 'https://podcasts.apple.com/us/podcast/xya/id12341'); | ||
expect(entry.name).toBe('Apple Podcasts'); | ||
}); |
{ | ||
"name": "@alitu/user-agents-v2-api", | ||
"version": "0.1.6", | ||
"version": "0.2.1", | ||
"description": "API for identifying User-Agent strings using OPAWG's user-agents-v2 lists.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
253643
6977