Skip to content

Instantly share code, notes, and snippets.

@tadeaspetak
Last active July 29, 2021 14:36
Show Gist options
  • Save tadeaspetak/ecb3d3f11dc9dc4dbf03d2d9da0f46df to your computer and use it in GitHub Desktop.
Save tadeaspetak/ecb3d3f11dc9dc4dbf03d2d9da0f46df to your computer and use it in GitHub Desktop.
Get OS, basic info
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