Last active
July 29, 2021 14:36
-
-
Save tadeaspetak/ecb3d3f11dc9dc4dbf03d2d9da0f46df to your computer and use it in GitHub Desktop.
Get OS, basic info
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 enum OS { | |
Android = "Android", | |
iOS = "iOS", | |
Linux = "Linux", | |
Mac = "Mac", | |
Unix = "Unix", | |
Windows = "Windows", | |
Other = "Other", | |
} | |
const patterns: { [key in OS]: string } = { | |
[OS.Android]: "Android", | |
[OS.iOS]: "like Mac", | |
[OS.Linux]: "Linux", | |
[OS.Mac]: "Mac", | |
[OS.Unix]: "X11", | |
[OS.Windows]: "Win", | |
[OS.Other]: "Other", | |
}; | |
export const getOs = (): OS => { | |
const agent = window.navigator?.userAgent ?? window.navigator?.appVersion; | |
if (!agent) return OS.Other; | |
for (const [key, pattern] of Object.entries(patterns)) { | |
if (agent.includes(pattern)) return OS[key as OS]; | |
} | |
return OS.Other; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment