From efbe61f5c9df5c4cad825235774dd3731266a049 Mon Sep 17 00:00:00 2001 From: Bryce Tham Date: Mon, 29 Jul 2024 16:10:33 -0400 Subject: [PATCH] feat: add helpers for determining OS --- src/browser-info.spec.ts | 32 ++++++++++++++++++++++++++++++-- src/browser-info.ts | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/src/browser-info.spec.ts b/src/browser-info.spec.ts index ffe66ca..b3f0129 100644 --- a/src/browser-info.spec.ts +++ b/src/browser-info.spec.ts @@ -14,8 +14,7 @@ const mockUserAgent = (browserName: BrowserName): void => { 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36'; break; case BrowserName.FIREFOX: - userAgent = - 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/118.0'; + userAgent = 'Mozilla/5.0 (X11; Linux i686; rv:128.0) Gecko/20100101 Firefox/128.0'; break; case BrowserName.EDGE: userAgent = @@ -108,6 +107,35 @@ describe('BrowserInfo', () => { }); }); + describe('identifying operating systems', () => { + it('should identify a Windows OS', () => { + expect.hasAssertions(); + mockUserAgent(BrowserName.CHROME); + + expect(BrowserInfo.isWindows()).toBeTruthy(); + expect(BrowserInfo.isMac()).toBeFalsy(); + expect(BrowserInfo.isLinux()).toBeFalsy(); + }); + + it('should identify a Mac OS', () => { + expect.hasAssertions(); + mockUserAgent(BrowserName.SAFARI); + + expect(BrowserInfo.isWindows()).toBeFalsy(); + expect(BrowserInfo.isMac()).toBeTruthy(); + expect(BrowserInfo.isLinux()).toBeFalsy(); + }); + + it('should identify a Linux OS', () => { + expect.hasAssertions(); + mockUserAgent(BrowserName.FIREFOX); + + expect(BrowserInfo.isWindows()).toBeFalsy(); + expect(BrowserInfo.isMac()).toBeFalsy(); + expect(BrowserInfo.isLinux()).toBeTruthy(); + }); + }); + describe('comparing versions', () => { it('should compare browser versions correctly', () => { expect.hasAssertions(); diff --git a/src/browser-info.ts b/src/browser-info.ts index e44a080..0adab99 100644 --- a/src/browser-info.ts +++ b/src/browser-info.ts @@ -10,6 +10,15 @@ export enum BrowserName { SAFARI = 'Safari', } +/** + * Used by the {@link BrowserInfo} class to check the name of the OS. + */ +export enum OSName { + WINDOWS = 'Windows', + MAC = 'macOS', + LINUX = 'Linux', +} + /** * A class that retrieves and checks certain information about the browser. */ @@ -88,6 +97,33 @@ export class BrowserInfo { return this.browser.getBrowserName() === BrowserName.SAFARI; } + /** + * Check if current OS is Windows. + * + * @returns True if Windows, false otherwise. + */ + static isWindows(): boolean { + return this.browser.getOSName() === OSName.WINDOWS; + } + + /** + * Check if current OS is Mac. + * + * @returns True if Mac, false otherwise. + */ + static isMac(): boolean { + return this.browser.getOSName() === OSName.MAC; + } + + /** + * Check if current OS is Linux. + * + * @returns True if Linux, false otherwise. + */ + static isLinux(): boolean { + return this.browser.getOSName() === OSName.LINUX; + } + /** * Check if current browser version is greater than the given version. *