Use constructors

This commit is contained in:
Faisal Salman 2016-12-06 12:41:48 +07:00
parent 3e2d5c1817
commit 5177c460a5

View File

@ -81,27 +81,28 @@
var mapper = { 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++) { for (p = 0; p < args[1].length; p++) {
q = args[1][p]; q = args[1][p];
result[typeof q === OBJ_TYPE ? q[0] : q] = undefined; result[typeof q === OBJ_TYPE ? q[0] : q] = undefined;
} }*/
// loop through all regexes maps // loop through all regexes maps
while (i < args.length && !matches) { while (i < arrays.length && !matches) {
var regex = args[i], // even sequence (0,2,4,..) var regex = arrays[i], // even sequence (0,2,4,..)
props = args[i + 1]; // odd sequence (1,3,5,..) props = arrays[i + 1]; // odd sequence (1,3,5,..)
j = k = 0; j = k = 0;
// try matching uastring with regexes // try matching uastring with regexes
while (j < regex.length && !matches) { while (j < regex.length && !matches) {
matches = regex[j++].exec(this.getUA()); matches = regex[j++].exec(ua);
if (!!matches) { if (!!matches) {
for (p = 0; p < props.length; p++) { for (p = 0; p < props.length; p++) {
@ -112,32 +113,33 @@
if (q.length == 2) { if (q.length == 2) {
if (typeof q[1] == FUNC_TYPE) { if (typeof q[1] == FUNC_TYPE) {
// assign modified match // assign modified match
result[q[0]] = q[1].call(this, match); this[q[0]] = q[1].call(this, match);
} else { } else {
// assign given value, ignore regex match // assign given value, ignore regex match
result[q[0]] = q[1]; this[q[0]] = q[1];
} }
} else if (q.length == 3) { } else if (q.length == 3) {
// check whether function or regex // check whether function or regex
if (typeof q[1] === FUNC_TYPE && !(q[1].exec && q[1].test)) { if (typeof q[1] === FUNC_TYPE && !(q[1].exec && q[1].test)) {
// call function (usually string mapper) // 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 { } else {
// sanitize match using given regex // 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) { } 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 { } else {
result[q] = match ? match : undefined; this[q] = match ? match : undefined;
} }
} }
} }
} }
i += 2; i += 2;
} }
return result; //console.log(this);
//return this;
}, },
str : function (str, map) { str : function (str, map) {
@ -790,6 +792,20 @@
// Constructor // 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) { var UAParser = function (uastring, extensions) {
@ -799,23 +815,32 @@
var ua = uastring || ((window && window.navigator && window.navigator.userAgent) ? window.navigator.userAgent : EMPTY); var ua = uastring || ((window && window.navigator && window.navigator.userAgent) ? window.navigator.userAgent : EMPTY);
var rgxmap = extensions ? util.extend(regexes, extensions) : regexes; 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 () { this.getBrowser = function () {
var browser = mapper.rgx.apply(this, rgxmap.browser); mapper.rgx.call(browser, ua, rgxmap.browser);
browser.major = util.major(browser.version); browser.major = util.major(browser.version); // deprecated
return browser; return browser;
}; };
this.getCPU = function () { this.getCPU = function () {
return mapper.rgx.apply(this, rgxmap.cpu); mapper.rgx.call(cpu, ua, rgxmap.cpu);
return cpu;
}; };
this.getDevice = function () { this.getDevice = function () {
return mapper.rgx.apply(this, rgxmap.device); mapper.rgx.call(device, ua, rgxmap.device);
return device;
}; };
this.getEngine = function () { this.getEngine = function () {
return mapper.rgx.apply(this, rgxmap.engine); mapper.rgx.call(engine, ua, rgxmap.engine);
return engine;
}; };
this.getOS = function () { this.getOS = function () {
return mapper.rgx.apply(this, rgxmap.os); mapper.rgx.call(os, ua, rgxmap.os);
return os;
}; };
this.getResult = function() { this.getResult = function() {
return { return {
@ -832,6 +857,11 @@
}; };
this.setUA = function (uastring) { this.setUA = function (uastring) {
ua = uastring; ua = uastring;
browser = new Browser();
cpu = new CPU();
device = new Device();
engine = new Engine();
os = new OS();
return this; return this;
}; };
return this; return this;
@ -865,7 +895,7 @@
NAME : NAME, NAME : NAME,
VERSION : VERSION VERSION : VERSION
}; };
//UAParser.Utils = util;
/////////// ///////////
// Export // Export