Update formFactor to be a list

This commit is contained in:
Faisal Salman 2023-09-30 15:42:18 +07:00
parent a9247154e0
commit f6fbf170e3
3 changed files with 39 additions and 15 deletions

View File

@ -131,8 +131,12 @@
var arr = []; var arr = [];
var tokens = strip(/\\?\"/g, header).split(','); var tokens = strip(/\\?\"/g, header).split(',');
for (var i = 0; i < tokens.length; i++) { for (var i = 0; i < tokens.length; i++) {
var token = tokens[i].split(';v='); if (tokens[i].indexOf(';') > -1) {
var token = trim(tokens[i]).split(';v=');
arr[i] = { brand : token[0], version : token[1] }; arr[i] = { brand : token[0], version : token[1] };
} else {
arr[i] = tokens[i];
}
} }
return arr; return arr;
}, },
@ -157,7 +161,7 @@
return str.replace(pattern, EMPTY); return str.replace(pattern, EMPTY);
}, },
stripQuotes = function (val) { stripQuotes = function (val) {
return typeof val === STR_TYPE ? strip(/\"/g, val) : val; return typeof val === STR_TYPE ? strip(/\\?\"/g, val) : val;
}, },
trim = function (str, len) { trim = function (str, len) {
if (typeof(str) === STR_TYPE) { if (typeof(str) === STR_TYPE) {
@ -239,7 +243,7 @@
return (i === UNKNOWN) ? undefined : i; return (i === UNKNOWN) ? undefined : i;
} }
} }
return str; return map.hasOwnProperty('*') ? map['*'] : str;
}; };
/////////////// ///////////////
@ -263,10 +267,11 @@
formFactorMap = { formFactorMap = {
'embedded' : 'Automotive', 'embedded' : 'Automotive',
'mobile' : 'Mobile', 'mobile' : 'Mobile',
'tablet' : 'Tablet', 'tablet' : ['Tablet', 'EInk'],
'smarttv' : 'TV', 'smarttv' : 'TV',
'wearable' : ['VR', 'XR'], 'wearable' : ['VR', 'XR', 'Watch'],
'?' : 'Unknown' '?' : ['Desktop', 'Unknown'],
'*' : undefined
}; };
////////////// //////////////
@ -947,7 +952,7 @@
[PLATFORM, stripQuotes(uach[CH_HEADER_PLATFORM])], [PLATFORM, stripQuotes(uach[CH_HEADER_PLATFORM])],
[PLATFORMVER, stripQuotes(uach[CH_HEADER_PLATFORM_VER])], [PLATFORMVER, stripQuotes(uach[CH_HEADER_PLATFORM_VER])],
[ARCHITECTURE, stripQuotes(uach[CH_HEADER_ARCH])], [ARCHITECTURE, stripQuotes(uach[CH_HEADER_ARCH])],
[FORMFACTOR, stripQuotes(uach[CH_HEADER_FORM_FACTOR])], [FORMFACTOR, itemListToArray(uach[CH_HEADER_FORM_FACTOR])],
[BITNESS, stripQuotes(uach[CH_HEADER_BITNESS])] [BITNESS, stripQuotes(uach[CH_HEADER_BITNESS])]
]); ]);
} else { } else {
@ -1029,8 +1034,7 @@
}; };
this.parseCH = function () { this.parseCH = function () {
var ua = this.ua, var uaCH = this.uaCH,
uaCH = this.uaCH,
rgxMap = this.rgxMap; rgxMap = this.rgxMap;
switch (this.itemType) { switch (this.itemType) {
@ -1063,7 +1067,16 @@
this.set(MODEL, uaCH[MODEL]); this.set(MODEL, uaCH[MODEL]);
} }
if (uaCH[FORMFACTOR]) { if (uaCH[FORMFACTOR]) {
this.set(TYPE, strMapper(uaCH[FORMFACTOR], formFactorMap)); var ff;
if (typeof uaCH[FORMFACTOR] !== 'string') {
var idx = 0;
while (!ff && idx < uaCH[FORMFACTOR].length) {
ff = strMapper(uaCH[FORMFACTOR][idx++], formFactorMap);
}
} else {
ff = strMapper(uaCH[FORMFACTOR], formFactorMap);
}
this.set(TYPE, ff);
} }
break; break;
case UA_OS: case UA_OS:

View File

@ -466,17 +466,26 @@ describe('Map UA-CH headers', function () {
it('Can detect form-factor from client-hints', function () { it('Can detect form-factor from client-hints', function () {
const FFVR = { const FFVR = {
'sec-ch-ua-form-factor' : 'VR' 'sec-ch-ua-form-factor' : '"VR"'
};
const FFEInk = {
'sec-ch-ua-form-factor' : '"Tablet", "EInk"'
}; };
const FFUnknown = { const FFUnknown = {
'sec-ch-ua-form-factor' : 'Unknown' 'sec-ch-ua-form-factor' : '"Unknown"'
}; };
UAParser(FFVR).withClientHints().then(function (ua) { UAParser(FFVR).withClientHints().then(function (ua) {
assert.strictEqual(ua.device.type, 'wearable'); assert.strictEqual(ua.device.type, 'wearable');
}); });
UAParser(FFEInk).withClientHints().then(function (ua) {
assert.strictEqual(ua.device.type, 'tablet');
});
UAParser(FFUnknown).withClientHints().then(function (ua) { UAParser(FFUnknown).withClientHints().then(function (ua) {
assert.strictEqual(ua.device.type, undefined); assert.strictEqual(ua.device.type, undefined);
}); });

View File

@ -40,7 +40,8 @@ test('read client hints data', async ({ page }) => {
version: '110' version: '110'
} }
], ],
platform: 'New OS' platform: 'New OS',
formFactor: 'New Form Factor'
}); });
} }
} }
@ -54,6 +55,7 @@ test('read client hints data', async ({ page }) => {
expect(uap).toHaveProperty('browser.name', 'New Browser'); expect(uap).toHaveProperty('browser.name', 'New Browser');
expect(uap).toHaveProperty('os.name', 'New OS'); expect(uap).toHaveProperty('os.name', 'New OS');
expect(uap).toHaveProperty('device.type', undefined);
}); });
test('detect Brave', async ({ page }) => { test('detect Brave', async ({ page }) => {