Skip to content

Commit

Permalink
fix: updated regex to Set
Browse files Browse the repository at this point in the history
  • Loading branch information
hasan-deriv committed Apr 17, 2024
1 parent c0ea93b commit bf3b97f
Showing 1 changed file with 127 additions and 5 deletions.
132 changes: 127 additions & 5 deletions packages/shared/src/utils/os/os_detect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,133 @@ export const mobileOSDetect = () => {
return 'unknown';
};

// Simple regular expression to match potential Huawei device codes
const huaweiDevicesRegex = /\b([A-Z]{3}-)\b/gi;

// Set of valid Huawei device codes
const validCodes = new Set([
'ALP-',
'AMN-',
'ANA-',
'ANE-',
'ANG-',
'AQM-',
'ARS-',
'ART-',
'ATU-',
'BAC-',
'BLA-',
'BRQ-',
'CAG-',
'CAM-',
'CAN-',
'CAZ-',
'CDL-',
'CDY-',
'CLT-',
'CRO-',
'CUN-',
'DIG-',
'DRA-',
'DUA-',
'DUB-',
'DVC-',
'ELE-',
'ELS-',
'EML-',
'EVA-',
'EVR-',
'FIG-',
'FLA-',
'FRL-',
'GLK-',
'HMA-',
'HW-',
'HWI-',
'INE-',
'JAT-',
'JEF-',
'JER-',
'JKM-',
'JNY-',
'JSC-',
'LDN-',
'LIO-',
'LON-',
'LUA-',
'LYA-',
'LYO-',
'MAR-',
'MED-',
'MHA-',
'MLA-',
'MRD-',
'MYA-',
'NCE-',
'NEO-',
'NOH-',
'NOP-',
'OCE-',
'PAR-',
'PIC-',
'POT-',
'PPA-',
'PRA-',
'RNE-',
'SEA-',
'SLA-',
'SNE-',
'SPN-',
'STK-',
'TAH-',
'TAS-',
'TET-',
'TRT-',
'VCE-',
'VIE-',
'VKY-',
'VNS-',
'VOG-',
'VTR-',
'WAS-',
'WKG-',
'WLZ-',
'JAD-',
'MLD-',
'RTE-',
'NAM-',
'NEN-',
'BAL-',
'JLN-',
'YAL-',
'MGA-',
'FGD-',
'XYAO-',
'BON-',
'ALN-',
'ALT-',
'BRA-',
'DBY2-',
'STG-',
'MAO-',
'LEM-',
'GOA-',
'FOA-',
'MNA-',
'LNA-',
]);

// Function to validate Huawei device codes from a string
function validateHuaweiCodes(inputString: string) {
const matches = inputString.match(huaweiDevicesRegex);
if (matches) {
return matches.filter(code => validCodes.has(code.toUpperCase())).length > 0;
}
return false;
}

export const mobileOSDetectAsync = async () => {
const userAgent = navigator.userAgent ?? window.opera ?? '';
const huaweiDevicesRegex =
/\b(AL[PN]|AMN|AN[AE]|A(N[EG]|QM|RS|RT|TU)|B([AL]|AC|LA)|BRQ|C(A[GMN]|AZ|DL|DY|LT|RO|UN)|D(IG|RA|UA|UB|VC)|E(L[ES]|ML|VA|VR)|F(IG|LA|RL)|G(LK|OA)|H(MA|WI?)|I(N[EY]|NE)|J(AT|EF|ER|KM|NY|SC)|L(DN|IO|ON|UA)|LY[AO]|M(A[ORD]|ED|HA|LA)|M(YA|R[DE]|NA)|N(AM|CE|EN|EO|OH|OP)|OCE|P([AIR]|AR|IC|OT|PA|P[AE]|RA)|R(NE|TE)|S(EA|LA|NE|PN)|STK|T(A[HST]|ET|RT)|V(CE|IE|KY|NS|OG|TR)|W(A[SZ]|KG|LZ)|XYAO|F(GD|OA)|DBY2|STG|LEM|MNA|LNA)\b/gi;

// Windows Phone must come first because its UA also contains "Android"
if (/windows phone/i.test(userAgent)) {
return 'Windows Phone';
Expand All @@ -120,10 +242,10 @@ export const mobileOSDetectAsync = async () => {
// Check if navigator.userAgentData is available for modern browsers
if (navigator?.userAgentData) {
const ua = await navigator.userAgentData.getHighEntropyValues(['model']);
if (huaweiDevicesRegex.test(ua?.model || '')) {
if (validateHuaweiCodes(ua?.model || '')) {
return 'huawei';
}
} else if (huaweiDevicesRegex.test(userAgent) || /huawei/i.test(userAgent)) {
} else if (validateHuaweiCodes(userAgent) || /huawei/i.test(userAgent)) {
return 'huawei';
}
return 'Android';
Expand Down

0 comments on commit bf3b97f

Please sign in to comment.