The Node.js® Website
1import type { UserOS } from '@/types/userOS';
2
3export const detectOsInUserAgent = (userAgent: string | undefined): UserOS => {
4 const osMatch = userAgent?.match(/(Win|Mac|Linux)/);
5 switch (osMatch && osMatch[1]) {
6 case 'Win':
7 return 'WIN';
8 case 'Mac':
9 return 'MAC';
10 case 'Linux':
11 return 'LINUX';
12 default:
13 return 'OTHER';
14 }
15};
16
17// Since `navigator.appVersion` is deprecated, we use the `userAgent``
18// https://developer.mozilla.org/en-US/docs/Web/API/Navigator/appVersion
19export const detectOS = (): UserOS => detectOsInUserAgent(navigator?.userAgent);