From 159d4aee8c3d87989c2a1144414a0c4b5b3965dd Mon Sep 17 00:00:00 2001 From: Nathan Yocum Date: Wed, 15 Apr 2020 12:07:23 -0700 Subject: [PATCH] Linux platform support --- src/user-agent-parser.test.ts | 12 ++++++++++++ src/user-agent-parser.ts | 14 ++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/user-agent-parser.test.ts b/src/user-agent-parser.test.ts index 6a89db2..1ce3154 100644 --- a/src/user-agent-parser.test.ts +++ b/src/user-agent-parser.test.ts @@ -36,6 +36,18 @@ test('It should get OSX platform', () => { expect(result.version).toBe('10.15.3'); }); +test('It should get Linux platform', () => { + // Given + const userAgent = 'Chrome 80.0.3987.132 / Linux 0.0'; + + // When + const result = getPlatformFrom(userAgent); + + // Then + expect(result.name).toBe('linux'); + expect(result.version).toBe('0.0'); +}); + test('It should get Windows platform on Windows 10', () => { // Given const userAgent = 'Chrome 80.0.3987.149 / Windows 10'; diff --git a/src/user-agent-parser.ts b/src/user-agent-parser.ts index 1655a42..9c62ba6 100644 --- a/src/user-agent-parser.ts +++ b/src/user-agent-parser.ts @@ -51,6 +51,12 @@ export function getPlatformFrom(userAgent: string | undefined): Platform { version: platformInfo.version, }; } + if (isLinux(platformInfo.name)) { + return { + name: 'linux', + version: platformInfo.version, + }; + } if (isWindows(platformInfo.name)) { return { name: 'windows', @@ -98,6 +104,14 @@ export function isMacOsX(platformName: string | undefined): boolean { return result; } +export function isLinux(platformName: string | undefined): boolean { + if (platformName === undefined) { + return false; + } + const result = platformName.toLowerCase().includes('linux'); + return result; +} + export function isWindows(platformName: string | undefined): boolean { if (platformName === undefined) { return false;