This commit is contained in:
Faisal Salman 2023-02-26 18:27:55 +07:00
parent 18730452f2
commit f1449a8202
2 changed files with 136 additions and 85 deletions

View File

@ -56,8 +56,10 @@
SAMSUNG = 'Samsung', SAMSUNG = 'Samsung',
SHARP = 'Sharp', SHARP = 'Sharp',
SONY = 'Sony', SONY = 'Sony',
SWISS = 'Swiss',
XIAOMI = 'Xiaomi', XIAOMI = 'Xiaomi',
ZEBRA = 'Zebra', ZEBRA = 'Zebra',
ZTE = 'ZTE',
FACEBOOK = 'Facebook', FACEBOOK = 'Facebook',
CHROMIUM_OS = 'Chromium OS', CHROMIUM_OS = 'Chromium OS',
MAC_OS = 'Mac OS'; MAC_OS = 'Mac OS';
@ -87,15 +89,12 @@
has = function (str1, str2) { has = function (str1, str2) {
return typeof str1 === STR_TYPE ? lowerize(str2).indexOf(lowerize(str1)) !== -1 : false; return typeof str1 === STR_TYPE ? lowerize(str2).indexOf(lowerize(str1)) !== -1 : false;
}, },
lowerize = function (str) { lowerize = function (str, rgx) {
return typeof(str) === STR_TYPE ? str.toLowerCase() : UNDEF_TYPE; return typeof(str) === STR_TYPE ? str.toLowerCase().replace((rgx ? new RegExp(rgx, 'i') : EMPTY), EMPTY) : str;
}, },
majorize = function (version) { majorize = function (version) {
return typeof(version) === STR_TYPE ? version.replace(/[^\d\.]/g, EMPTY).split('.')[0] : undefined; return typeof(version) === STR_TYPE ? version.replace(/[^\d\.]/g, EMPTY).split('.')[0] : undefined;
}, },
sanitize = function (str, rgx) {
return rgx ? lowerize(str).replace(rgx, EMPTY) : lowerize(str);
},
trim = function (str, len) { trim = function (str, len) {
if (typeof(str) === STR_TYPE) { if (typeof(str) === STR_TYPE) {
str = str.replace(/^\s\s*/, EMPTY); str = str.replace(/^\s\s*/, EMPTY);
@ -346,7 +345,7 @@
], [NAME, VERSION], [ ], [NAME, VERSION], [
/(cobalt)\/([\w\.]+)/i // Cobalt /(cobalt)\/([\w\.]+)/i // Cobalt
], [NAME, [VERSION, /master.|lts./, ""]] ], [NAME, [VERSION, /[^\d\.]+./, EMPTY]]
], ],
cpu : [[ cpu : [[
@ -566,13 +565,13 @@
/\b(tm\d{3}\w+) b/i /\b(tm\d{3}\w+) b/i
], [MODEL, [VENDOR, 'NuVision'], [TYPE, TABLET]], [ ], [MODEL, [VENDOR, 'NuVision'], [TYPE, TABLET]], [
/\b(k88) b/i // ZTE K Series Tablet /\b(k88) b/i // ZTE K Series Tablet
], [MODEL, [VENDOR, 'ZTE'], [TYPE, TABLET]], [ ], [MODEL, [VENDOR, ZTE], [TYPE, TABLET]], [
/\b(nx\d{3}j) b/i // ZTE Nubia /\b(nx\d{3}j) b/i // ZTE Nubia
], [MODEL, [VENDOR, 'ZTE'], [TYPE, MOBILE]], [ ], [MODEL, [VENDOR, ZTE], [TYPE, MOBILE]], [
/\b(gen\d{3}) b.+49h/i // Swiss GEN Mobile /\b(gen\d{3}) b.+49h/i // Swiss GEN Mobile
], [MODEL, [VENDOR, 'Swiss'], [TYPE, MOBILE]], [ ], [MODEL, [VENDOR, SWISS], [TYPE, MOBILE]], [
/\b(zur\d{3}) b/i // Swiss ZUR Tablet /\b(zur\d{3}) b/i // Swiss ZUR Tablet
], [MODEL, [VENDOR, 'Swiss'], [TYPE, TABLET]], [ ], [MODEL, [VENDOR, SWISS], [TYPE, TABLET]], [
/\b((zeki)?tb.*\b) b/i // Zeki Tablets /\b((zeki)?tb.*\b) b/i // Zeki Tablets
], [MODEL, [VENDOR, 'Zeki'], [TYPE, TABLET]], [ ], [MODEL, [VENDOR, 'Zeki'], [TYPE, TABLET]], [
/\b([yr]\d{2}) b/i, /\b([yr]\d{2}) b/i,
@ -600,7 +599,7 @@
], [VENDOR, MODEL, [TYPE, MOBILE]], [ ], [VENDOR, MODEL, [TYPE, MOBILE]], [
/(kin\.[onetw]{3})/i // Microsoft Kin /(kin\.[onetw]{3})/i // Microsoft Kin
], [[MODEL, /\./g, ' '], [VENDOR, MICROSOFT], [TYPE, MOBILE]], [ ], [[MODEL, /\./g, ' '], [VENDOR, MICROSOFT], [TYPE, MOBILE]], [
/droid.+; (cc6666?|et5[16]|mc[239][23]x?|vc8[03]x?)\)/i // Zebra /droid.+; ([c6]+|et5[16]|mc[239][23]x?|vc8[03]x?)\)/i // Zebra
], [MODEL, [VENDOR, ZEBRA], [TYPE, TABLET]], [ ], [MODEL, [VENDOR, ZEBRA], [TYPE, TABLET]], [
/droid.+; (ec30|ps20|tc[2-8]\d[kx])\)/i /droid.+; (ec30|ps20|tc[2-8]\d[kx])\)/i
], [MODEL, [VENDOR, ZEBRA], [TYPE, MOBILE]], [ ], [MODEL, [VENDOR, ZEBRA], [TYPE, MOBILE]], [
@ -624,7 +623,7 @@
/\(dtv[\);].+(aquos)/i, /\(dtv[\);].+(aquos)/i,
/(aquos-tv[\w ]+)\)/i // Sharp /(aquos-tv[\w ]+)\)/i // Sharp
], [MODEL, [VENDOR, SHARP], [TYPE, SMARTTV]],[ ], [MODEL, [VENDOR, SHARP], [TYPE, SMARTTV]],[
/(bravia[\w ]+)( bui|\))/i // Sony /(bravia[\w ]+)( bui|\))/i // Sony
], [MODEL, [VENDOR, SONY], [TYPE, SMARTTV]], [ ], [MODEL, [VENDOR, SONY], [TYPE, SMARTTV]], [
/(mitv-\w{5}) bui/i // Xiaomi /(mitv-\w{5}) bui/i // Xiaomi
], [MODEL, [VENDOR, XIAOMI], [TYPE, SMARTTV]], [ ], [MODEL, [VENDOR, XIAOMI], [TYPE, SMARTTV]], [
@ -641,11 +640,11 @@
/////////////////// ///////////////////
/(ouya)/i, // Ouya /(ouya)/i, // Ouya
/(nintendo) ([wids3utch]+)/i // Nintendo /(nintendo) (\w+)/i // Nintendo
], [VENDOR, MODEL, [TYPE, CONSOLE]], [ ], [VENDOR, MODEL, [TYPE, CONSOLE]], [
/droid.+; (shield) bui/i // Nvidia /droid.+; (shield) bui/i // Nvidia
], [MODEL, [VENDOR, 'Nvidia'], [TYPE, CONSOLE]], [ ], [MODEL, [VENDOR, 'Nvidia'], [TYPE, CONSOLE]], [
/(playstation [345portablevi]+)/i // Playstation /(playstation \w+)/i // Playstation
], [MODEL, [VENDOR, SONY], [TYPE, CONSOLE]], [ ], [MODEL, [VENDOR, SONY], [TYPE, CONSOLE]], [
/\b(xbox(?: one)?(?!; xbox))[\); ]/i // Microsoft Xbox /\b(xbox(?: one)?(?!; xbox))[\); ]/i // Microsoft Xbox
], [MODEL, [VENDOR, MICROSOFT], [TYPE, CONSOLE]], [ ], [MODEL, [VENDOR, MICROSOFT], [TYPE, CONSOLE]], [
@ -759,7 +758,7 @@
/(nettv)\/(\d+\.[\w\.]+)/i, // NetTV /(nettv)\/(\d+\.[\w\.]+)/i, // NetTV
// Console // Console
/(nintendo|playstation) ([wids345portablevuch]+)/i, // Nintendo/Playstation /(nintendo|playstation) (\w+)/i, // Nintendo/Playstation
/(xbox); +xbox ([^\);]+)/i, // Microsoft Xbox (360, One, X, S, Series X, Series S) /(xbox); +xbox ([^\);]+)/i, // Microsoft Xbox (360, One, X, S, Series X, Series S)
// Other // Other
@ -787,73 +786,117 @@
// Constructor // Constructor
//////////////// ////////////////
function UAItem (propToString, propIs) { function UAItem () {}
this.propToString = propToString; UAItem.prototype.get = function (prop) {
this.propIs = propIs[0]; if (!prop) {
this.rgxIs = propIs[1]; return this.data;
}
UAItem.prototype.is = function (strCheck) {
var is = false;
for (var i in this.propIs) {
if (sanitize(this[this.propIs[i]], this.rgxIs) == sanitize(strCheck, this.rgxIs)) {
is = true;
if (strCheck != UNDEF_TYPE) break;
} else if (strCheck == UNDEF_TYPE && is) {
is = !is;
break;
}
} }
return is; return this.data.hasOwnProperty(prop) ? this.data[prop] : undefined;
}; };
UAItem.prototype.toString = function () { UAItem.prototype.parse = function (ua, rgxmap) {
var str = ''; rgxMapper.call(this.data, ua, rgxmap);
for (var i in this.propToString) { };
if (typeof(this[this.propToString[i]]) !== UNDEF_TYPE) { UAItem.prototype.set = function (prop, val) {
str += (str ? ' ' : '') + this[this.propToString[i]]; this.data[prop] = val;
} };
} UAItem.prototype.then = function (callback) {
return str ? str : UNDEF_TYPE; return callback(this.data);
};
var createUAData = function (data) {
return (function () {
var propIs = data.propIs;
var ignoreIs = data.ignoreIs;
var propToStr = data.propToStr;
var UAData = function () {
for (var i in data.props) {
this[data.props[i]] = undefined;
}
};
UAData.prototype.is = function (strToCheck) {
var is = false;
for (var i in propIs) {
if (lowerize(this[propIs[i]], ignoreIs) === lowerize(strToCheck, ignoreIs)) {
is = true;
if (strToCheck != UNDEF_TYPE) break;
} else if (strToCheck == UNDEF_TYPE && is) {
is = !is;
break;
}
}
return is;
};
UAData.prototype.toString = function () {
var str = EMPTY;
for (var i in propToStr) {
if (typeof(this[propToStr[i]]) !== UNDEF_TYPE) {
str += (str ? ' ' : EMPTY) + this[propToStr[i]];
}
}
return str ? str : UNDEF_TYPE;
};
return new UAData();
})(data);
}; };
function UABrowser () { function UABrowser () {
this[NAME] = undefined; this.data = createUAData({
this[VERSION] = undefined; props : [NAME, VERSION, MAJOR],
this[MAJOR] = undefined; propIs : [NAME],
ignoreIs : ' ?browser$',
propToStr : [NAME, VERSION]
});
} }
UABrowser.prototype = new UAItem([NAME, VERSION], [[NAME], /\s?browser$/i]); UABrowser.prototype = new UAItem();
function UACPU () { function UACPU () {
this[ARCHITECTURE] = undefined; this.data = createUAData({
props : [ARCHITECTURE],
propIs : [ARCHITECTURE],
propToStr : [ARCHITECTURE]
});
} }
UACPU.prototype = new UAItem([ARCHITECTURE], [[ARCHITECTURE]]); UACPU.prototype = new UAItem();
function UADevice () { function UADevice () {
this[VENDOR] = undefined; this.data = createUAData({
this[MODEL] = undefined; props : [TYPE, MODEL, VENDOR],
this[TYPE] = undefined; propIs : [TYPE, MODEL, VENDOR],
propToStr : [VENDOR, MODEL]
});
} }
UADevice.prototype = new UAItem([VENDOR, MODEL], [[TYPE, MODEL, VENDOR]]); UADevice.prototype = new UAItem();
function UAEngine () { function UAEngine () {
this[NAME] = undefined; this.data = createUAData({
this[VERSION] = undefined; props : [NAME, VERSION],
propIs : [NAME],
propToStr : [NAME, VERSION]
});
} }
UAEngine.prototype = new UAItem([NAME, VERSION], [[NAME]]); UAEngine.prototype = new UAItem();
function UAOS () { function UAOS () {
this[NAME] = undefined; this.data = createUAData({
this[VERSION] = undefined; props : [NAME, VERSION],
propIs : [NAME],
ignoreIs : ' ?os$',
propToStr : [NAME, VERSION]
});
} }
UAOS.prototype = new UAItem([NAME, VERSION], [[NAME], /\s?os$/i]); UAOS.prototype = new UAItem();
function UAResult (uap) { function UAResult () {
this.ua = uap.getUA(); this.data = {
this.browser = uap.getBrowser(); ua : '',
this.cpu = uap.getCPU(); browser : undefined,
this.device = uap.getDevice(); cpu : undefined,
this.engine = uap.getEngine(); device : undefined,
this.os = uap.getOS(); engine : undefined,
os : undefined
};
} }
UAResult.prototype = new UAItem();
function UAParser (ua, extensions) { function UAParser (ua, extensions) {
@ -861,7 +904,6 @@
extensions = ua; extensions = ua;
ua = undefined; ua = undefined;
} }
if (!(this instanceof UAParser)) { if (!(this instanceof UAParser)) {
return new UAParser(ua, extensions).getResult(); return new UAParser(ua, extensions).getResult();
} }
@ -874,49 +916,56 @@
// public methods // public methods
this.getBrowser = function () { this.getBrowser = function () {
var _browser = new UABrowser(); var _browser = new UABrowser();
rgxMapper.call(_browser, _ua, _rgxmap.browser); _browser.parse(_ua, _rgxmap.browser);
_browser[MAJOR] = majorize(_browser[VERSION]); _browser.set(MAJOR, majorize(_browser.get(VERSION)));
// Brave-specific detection // Brave-specific detection
if (_navigator && _navigator.brave && typeof _navigator.brave.isBrave == FUNC_TYPE) { if (_navigator && _navigator.brave && typeof _navigator.brave.isBrave == FUNC_TYPE) {
_browser[NAME] = 'Brave'; _browser.set(NAME, 'Brave');
} }
return _browser; return _browser.get();
}; };
this.getCPU = function () { this.getCPU = function () {
var _cpu = new UACPU(); var _cpu = new UACPU();
rgxMapper.call(_cpu, _ua, _rgxmap.cpu); _cpu.parse(_ua, _rgxmap.cpu);
return _cpu; return _cpu.get();
}; };
this.getDevice = function () { this.getDevice = function () {
var _device = new UADevice(); var _device = new UADevice();
rgxMapper.call(_device, _ua, _rgxmap.device); _device.parse(_ua, _rgxmap.device);
if (!_device[TYPE] && _uach && _uach.mobile) { if (!_device.get(TYPE) && _uach && _uach.mobile) {
_device[TYPE] = MOBILE; _device.set(TYPE, MOBILE);
} }
// iPadOS-specific detection: identified as Mac, but has some iOS-only properties // iPadOS-specific detection: identified as Mac, but has some iOS-only properties
if (_device[MODEL] == 'Macintosh' && _navigator && typeof _navigator.standalone !== UNDEF_TYPE && _navigator.maxTouchPoints && _navigator.maxTouchPoints > 2) { if (_device.get(NAME) == 'Macintosh' && _navigator && typeof _navigator.standalone !== UNDEF_TYPE && _navigator.maxTouchPoints && _navigator.maxTouchPoints > 2) {
_device[MODEL] = 'iPad'; _device.set(MODEL, 'iPad');
_device[TYPE] = TABLET; _device.set(TYPE, TABLET);
} }
return _device; return _device.get();
}; };
this.getEngine = function () { this.getEngine = function () {
var _engine = new UAEngine(); var _engine = new UAEngine();
rgxMapper.call(_engine, _ua, _rgxmap.engine); _engine.parse(_ua, _rgxmap.engine);
return _engine; return _engine.get();
}; };
this.getOS = function () { this.getOS = function () {
var _os = new UAOS(); var _os = new UAOS();
rgxMapper.call(_os, _ua, _rgxmap.os); _os.parse(_ua, _rgxmap.os);
if (!_os[NAME] && _uach && _uach.platform != 'Unknown') { if (!_os.get(NAME) && _uach && _uach.platform != 'Unknown') {
_os[NAME] = _uach.platform _os.set(NAME, _uach.platform
.replace(/chrome os/i, CHROMIUM_OS) .replace(/chrome os/i, CHROMIUM_OS)
.replace(/macos/i, MAC_OS); // backward compatibility .replace(/macos/i, MAC_OS)); // backward compatibility
} }
return _os; return _os.get();
}; };
this.getResult = function () { this.getResult = function () {
return new UAResult(this); var _result = new UAResult();
_result.set('ua', _ua);
_result.set('browser', this.getBrowser());
_result.set('cpu', this.getCPU());
_result.set('device', this.getDevice());
_result.set('engine', this.getEngine());
_result.set('os', this.getOS());
return _result.get();
}; };
this.getUA = function () { this.getUA = function () {
return _ua; return _ua;

View File

@ -45,7 +45,7 @@ var methods = [
describe('UAParser()', function () { describe('UAParser()', function () {
var ua = 'Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6'; var ua = 'Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6';
assert.deepStrictEqual(UAParser(ua), new UAParser().setUA(ua).getResult()); assert.deepEqual(UAParser(ua), new UAParser().setUA(ua).getResult());
}); });
describe('UAParser() constructor does not throw with undefined ua argument', function () { describe('UAParser() constructor does not throw with undefined ua argument', function () {
@ -227,7 +227,9 @@ describe('is() utility method', function () {
assert.strictEqual(uap.getDevice().is("Apple"), true); assert.strictEqual(uap.getDevice().is("Apple"), true);
uap.setUA(""); uap.setUA("");
assert.strictEqual(uap.getDevice().model, undefined);
assert.strictEqual(uap.getDevice().is("undefined"), true); assert.strictEqual(uap.getDevice().is("undefined"), true);
assert.strictEqual(uap.getDevice().is(undefined), true);
}); });
//it('Should accept arch equivalent name', function () { //it('Should accept arch equivalent name', function () {