Skip to content

Commit

Permalink
Fix #718 - Extension param now accept multiple extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
faisalman committed Jun 6, 2024
1 parent f7810db commit 0a46ac3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/main/ua-parser.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ declare namespace UAParser {

type RegexMap = ((RegExp | string | (string | RegExp | Function)[])[])[];
type UAParserProps = 'browser' | 'cpu' | 'device' | 'engine' | 'os';
type UAParserExt = Partial<Record<UAParserProps, RegexMap>>;
type UAParserExt = Partial<Record<UAParserProps, RegexMap>> | Partial<Record<UAParserProps, RegexMap>>[];

export function UAParser(uastring?: string, extensions?: UAParserExt, headers?: Record<string, string>): IResult;
export function UAParser(uastring?: string, headers?: Record<string, string>): IResult;
Expand Down
27 changes: 18 additions & 9 deletions src/main/ua-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,21 @@
// Helper
//////////

var extend = function (regexes, extensions) {
var mergedRegexes = {};
for (var i in regexes) {
mergedRegexes[i] = extensions[i] && extensions[i].length % 2 === 0 ? extensions[i].concat(regexes[i]) : regexes[i];
var extend = function (defaultRgx, extensions) {
var mergedRgx = {};
var extraRgx = extensions;
if (!isExtensions(extensions)) {
extraRgx = {};
for (var i in extensions) {
for (var j in extensions[i]) {
extraRgx[j] = extensions[i][j].concat(extraRgx[j] ? extraRgx[j] : []);
}
}
}
for (var k in defaultRgx) {
mergedRgx[k] = extraRgx[k] && extraRgx[k].length % 2 === 0 ? extraRgx[k].concat(defaultRgx[k]) : defaultRgx[k];
}
return mergedRegexes;
return mergedRgx;
},
enumerize = function (arr) {
var enums = {};
Expand All @@ -124,9 +133,9 @@
}
return isString(str1) ? lowerize(str2).indexOf(lowerize(str1)) !== -1 : false;
},
isExtensions = function (obj) {
isExtensions = function (obj, deep) {
for (var prop in obj) {
return /^(browser|cpu|device|engine|os)$/.test(prop);
return /^(browser|cpu|device|engine|os)$/.test(prop) || (deep ? isExtensions(obj[prop]) : false);
}
},
isString = function (val) {
Expand Down Expand Up @@ -1163,7 +1172,7 @@
function UAParser (ua, extensions, headers) {

if (typeof ua === OBJ_TYPE) {
if (isExtensions(ua)) {
if (isExtensions(ua, true)) {
if (typeof extensions === OBJ_TYPE) {
headers = extensions; // case UAParser(extensions, headers)
}
Expand All @@ -1173,7 +1182,7 @@
extensions = undefined;
}
ua = undefined;
} else if (typeof ua === STR_TYPE && !isExtensions(extensions)) {
} else if (typeof ua === STR_TYPE && !isExtensions(extensions, true)) {
headers = extensions; // case UAParser(ua, headers)
extensions = undefined;
}
Expand Down
6 changes: 6 additions & 0 deletions test/mocha-test-extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ describe('Bots', () => {
assert.deepEqual(botsAndCLIsParser.setUA(wget).getBrowser(), {name: "Wget", version: "1.21.1", major: "1", type:"cli"});
assert.deepEqual(botsAndCLIsParser.setUA(facebookBot).getBrowser(), {name: "FacebookBot", version: "1.0", major: "1", type:"bot"});

// alternative merge options
const botsAndCLIsParser2 = new UAParser([Bots, CLIs]);
const botsAndCLIsParser3 = new UAParser(facebookBot, [Bots, CLIs]);
assert.deepEqual(botsAndCLIsParser2.setUA(wget).getBrowser(), {name: "Wget", version: "1.21.1", major: "1", type:"cli"});
assert.deepEqual(botsAndCLIsParser3.getBrowser(), {name: "FacebookBot", version: "1.0", major: "1", type:"bot"});

const emailParser = new UAParser(Emails);
assert.deepEqual(emailParser.setUA(outlook).getBrowser(), {name: "Microsoft Outlook", version: "16.0.9126", major: "16", type: "email"});
assert.deepEqual(emailParser.setUA(thunderbird).getBrowser(), {name: "Thunderbird", version: "78.13.0", major: "78", type: "email"});
Expand Down
7 changes: 7 additions & 0 deletions test/mocha-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ describe('Extending Regex', function () {
});
let myUA2 = 'Mozilla/5.0 MyTab 14 Pro Max';
assert.deepEqual(myParser2.setUA(myUA2).getDevice(), {vendor: "MyTab", model: "14 Pro Max", type: "tablet"});

let myParser3 = new UAParser([{
browser: myOwnListOfBrowsers
}, {
device: myOwnListOfDevices
}]);
assert.deepEqual(myParser3.setUA(myUA2).getDevice(), {vendor: "MyTab", model: "14 Pro Max", type: "tablet"});
});

describe('User-agent length', function () {
Expand Down

0 comments on commit 0a46ac3

Please sign in to comment.