Created
March 8, 2022 14:05
-
-
Save amerllica/ff4f29b5796ff9a331928ea2c09f6f37 to your computer and use it in GitHub Desktop.
Device Detector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export type NameVersion = { | |
name: string; | |
version: number; | |
}; | |
export type DeviceDetector = { | |
os: NameVersion; | |
browser: NameVersion; | |
}; | |
const deviceDetector = (): DeviceDetector => { | |
const module = { | |
options: [], | |
header: [ | |
navigator.platform, | |
navigator.userAgent, | |
navigator.appVersion, | |
navigator.vendor, | |
// @ts-ignore | |
window.opera, | |
], | |
dataOS: [ | |
{ name: 'Windows Phone', value: 'Windows Phone', version: 'OS' }, | |
{ name: 'Windows', value: 'Win', version: 'NT' }, | |
{ name: 'iPhone', value: 'iPhone', version: 'OS' }, | |
{ name: 'iPad', value: 'iPad', version: 'OS' }, | |
{ name: 'Kindle', value: 'Silk', version: 'Silk' }, | |
{ name: 'Android', value: 'Android', version: 'Android' }, | |
{ name: 'PlayBook', value: 'PlayBook', version: 'OS' }, | |
{ name: 'BlackBerry', value: 'BlackBerry', version: '/' }, | |
{ name: 'Macintosh', value: 'Mac', version: 'OS X' }, | |
{ name: 'Linux', value: 'Linux', version: 'rv' }, | |
{ name: 'Palm', value: 'Palm', version: 'PalmOS' }, | |
], | |
dataBrowser: [ | |
{ name: 'Chrome', value: 'Chrome', version: 'Chrome' }, | |
{ name: 'Firefox', value: 'Firefox', version: 'Firefox' }, | |
{ name: 'Safari', value: 'Safari', version: 'Version' }, | |
{ name: 'Internet Explorer', value: 'MSIE', version: 'MSIE' }, | |
{ name: 'Opera', value: 'Opera', version: 'Opera' }, | |
{ name: 'BlackBerry', value: 'CLDC', version: 'CLDC' }, | |
{ name: 'Mozilla', value: 'Mozilla', version: 'Mozilla' }, | |
], | |
init(): DeviceDetector { | |
const agent = this.header.join(' '); | |
return { | |
os: this.matchItem(agent, this.dataOS), | |
browser: this.matchItem(agent, this.dataBrowser), | |
}; | |
}, | |
matchItem(string: string, data: any): NameVersion { | |
let regex; | |
let regexv; | |
let match; | |
let matches; | |
let version; | |
for (let i = 0; i < data.length; i += 1) { | |
regex = new RegExp(data[i].value, 'i'); | |
match = regex.test(string); | |
if (match) { | |
regexv = new RegExp(`${data[i].version}[- /:;]([\\d._]+)`, 'i'); | |
matches = string.match(regexv); | |
version = ''; | |
if (matches) { | |
if (matches[1]) { | |
// eslint-disable-next-line | |
matches = matches[1]; | |
} | |
} | |
if (matches) { | |
matches = matches.split(/[._]+/); | |
for (let j = 0; j < matches.length; j += 1) { | |
if (j === 0) { | |
version += `${matches[j]}.`; | |
} else { | |
version += matches[j]; | |
} | |
} | |
} else { | |
version = '0'; | |
} | |
return { | |
name: data[i].name, | |
version: parseFloat(version), | |
}; | |
} | |
} | |
return { name: 'unknown', version: 0 }; | |
}, | |
}; | |
return module.init(); | |
}; | |
export { deviceDetector }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment