Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add helpers for determining OS #8

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions src/browser-info.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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();
Expand Down
36 changes: 36 additions & 0 deletions src/browser-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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.
*
Expand Down
Loading