From 5177c460a57ce9872a97152724cbcdad652fa788 Mon Sep 17 00:00:00 2001 From: Faisal Salman Date: Tue, 6 Dec 2016 12:41:48 +0700 Subject: [PATCH] Use constructors --- src/ua-parser.js | 74 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 52 insertions(+), 22 deletions(-) diff --git a/src/ua-parser.js b/src/ua-parser.js index df69765..d138f8e 100644 --- a/src/ua-parser.js +++ b/src/ua-parser.js @@ -81,27 +81,28 @@ var mapper = { - rgx : function () { + rgx : function (ua, arrays) { - var result = {}, i = 0, j, k, p, q, matches, match, args = arguments; + //var result = {}, + var i = 0, j, k, p, q, matches, match;//, args = arguments; - // construct object barebones + /*// construct object barebones for (p = 0; p < args[1].length; p++) { q = args[1][p]; result[typeof q === OBJ_TYPE ? q[0] : q] = undefined; - } + }*/ // loop through all regexes maps - while (i < args.length && !matches) { + while (i < arrays.length && !matches) { - var regex = args[i], // even sequence (0,2,4,..) - props = args[i + 1]; // odd sequence (1,3,5,..) + var regex = arrays[i], // even sequence (0,2,4,..) + props = arrays[i + 1]; // odd sequence (1,3,5,..) j = k = 0; // try matching uastring with regexes while (j < regex.length && !matches) { - matches = regex[j++].exec(this.getUA()); + matches = regex[j++].exec(ua); if (!!matches) { for (p = 0; p < props.length; p++) { @@ -112,32 +113,33 @@ if (q.length == 2) { if (typeof q[1] == FUNC_TYPE) { // assign modified match - result[q[0]] = q[1].call(this, match); + this[q[0]] = q[1].call(this, match); } else { // assign given value, ignore regex match - result[q[0]] = q[1]; + this[q[0]] = q[1]; } } else if (q.length == 3) { // check whether function or regex if (typeof q[1] === FUNC_TYPE && !(q[1].exec && q[1].test)) { // call function (usually string mapper) - result[q[0]] = match ? q[1].call(this, match, q[2]) : undefined; + this[q[0]] = match ? q[1].call(this, match, q[2]) : undefined; } else { // sanitize match using given regex - result[q[0]] = match ? match.replace(q[1], q[2]) : undefined; + this[q[0]] = match ? match.replace(q[1], q[2]) : undefined; } } else if (q.length == 4) { - result[q[0]] = match ? q[3].call(this, match.replace(q[1], q[2])) : undefined; + this[q[0]] = match ? q[3].call(this, match.replace(q[1], q[2])) : undefined; } } else { - result[q] = match ? match : undefined; + this[q] = match ? match : undefined; } } } } i += 2; } - return result; + //console.log(this); + //return this; }, str : function (str, map) { @@ -790,6 +792,20 @@ // Constructor //////////////// + var Browser = function (name, version) { + this[NAME] = name; + this[VERSION] = version; + }; + var CPU = function (arch) { + this[ARCHITECTURE] = arch; + }; + var Device = function (vendor, model, type) { + this[VENDOR] = vendor; + this[MODEL] = model; + this[TYPE] = type; + }; + var Engine = Browser; + var OS = Browser; var UAParser = function (uastring, extensions) { @@ -799,23 +815,32 @@ var ua = uastring || ((window && window.navigator && window.navigator.userAgent) ? window.navigator.userAgent : EMPTY); var rgxmap = extensions ? util.extend(regexes, extensions) : regexes; + var browser = new Browser(); + var cpu = new CPU(); + var device = new Device(); + var engine = new Engine(); + var os = new OS(); this.getBrowser = function () { - var browser = mapper.rgx.apply(this, rgxmap.browser); - browser.major = util.major(browser.version); + mapper.rgx.call(browser, ua, rgxmap.browser); + browser.major = util.major(browser.version); // deprecated return browser; }; this.getCPU = function () { - return mapper.rgx.apply(this, rgxmap.cpu); + mapper.rgx.call(cpu, ua, rgxmap.cpu); + return cpu; }; this.getDevice = function () { - return mapper.rgx.apply(this, rgxmap.device); + mapper.rgx.call(device, ua, rgxmap.device); + return device; }; this.getEngine = function () { - return mapper.rgx.apply(this, rgxmap.engine); + mapper.rgx.call(engine, ua, rgxmap.engine); + return engine; }; this.getOS = function () { - return mapper.rgx.apply(this, rgxmap.os); + mapper.rgx.call(os, ua, rgxmap.os); + return os; }; this.getResult = function() { return { @@ -832,6 +857,11 @@ }; this.setUA = function (uastring) { ua = uastring; + browser = new Browser(); + cpu = new CPU(); + device = new Device(); + engine = new Engine(); + os = new OS(); return this; }; return this; @@ -865,7 +895,7 @@ NAME : NAME, VERSION : VERSION }; - + //UAParser.Utils = util; /////////// // Export