-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: expose cpu info * refactor: create cpu info class * fix: cleanup * refactor: update class and test description * refactor: add undefined return type * refactor: change name of tests * docs: update README.md --------- Co-authored-by: evujici <[email protected]>
- Loading branch information
Showing
6 changed files
with
47 additions
and
49 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,19 @@ | ||
import { getCpuInfo } from './cpu-info'; | ||
import { CpuInfo } from './cpu-info'; | ||
|
||
describe('getCpuInfo', () => { | ||
it('should get correct CPU information when available', () => { | ||
describe('CpuInfo', () => { | ||
it('should return the number of logical CPU cores when the information is available', () => { | ||
expect.assertions(1); | ||
|
||
jest.spyOn(Navigator.prototype, 'hardwareConcurrency', 'get').mockReturnValue(1); | ||
|
||
const cpuInfo = getCpuInfo(); | ||
expect(cpuInfo.numLogicalCores).toBe(1); | ||
expect(CpuInfo.getNumLogicalCores()).toBe(1); | ||
}); | ||
it('should get correct CPU information when not available', () => { | ||
|
||
it('should return undefined when the logical CPU cores information is not available', () => { | ||
expect.assertions(1); | ||
|
||
jest.spyOn(Navigator.prototype, 'hardwareConcurrency', 'get').mockImplementation(); | ||
|
||
const cpuInfo = getCpuInfo(); | ||
expect(cpuInfo.numLogicalCores).toBeUndefined(); | ||
expect(CpuInfo.getNumLogicalCores()).toBeUndefined(); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,13 @@ | ||
export type CpuInfo = { | ||
numLogicalCores?: number; | ||
}; | ||
|
||
/** | ||
* Get the available information about the machine's CPU. | ||
* | ||
* @returns The {@link CpuInfo}. | ||
* A class that provides information about the CPU. | ||
*/ | ||
export function getCpuInfo(): CpuInfo { | ||
const cpuInfo = {} as CpuInfo; | ||
|
||
if (navigator.hardwareConcurrency) { | ||
cpuInfo.numLogicalCores = navigator.hardwareConcurrency; | ||
export class CpuInfo { | ||
/** | ||
* Gets the number of logical CPU cores. | ||
* | ||
* @returns The number of logical CPU cores, or undefined if not available. | ||
*/ | ||
static getNumLogicalCores(): number | undefined { | ||
return navigator.hardwareConcurrency; | ||
} | ||
|
||
return cpuInfo; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './browser-info'; | ||
export * from './web-capabilities'; | ||
export * from './cpu-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
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