From f338045df72f8f17800a9b2215729816d398ff57 Mon Sep 17 00:00:00 2001 From: Robert Sandell Date: Sun, 30 Dec 2018 09:03:08 +0200 Subject: [PATCH] Support Android version names, Recognise Huawei devices --- README.md | 4 +-- src/parser-os.js | 10 +++++-- src/parser-platforms.js | 16 ++++++++++ src/utils.js | 45 ++++++++++++++++++++++++++++ test/acceptance/useragentstrings.yml | 35 ++++++++++++++++++++++ 5 files changed, 106 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c6e5651..c8222d3 100644 --- a/README.md +++ b/README.md @@ -130,8 +130,8 @@ More of API and possibilities you will find in the `docs` folder. If you'd like to contribute a change to bowser, modify the files in `src/`, then run the following (you'll need node + npm installed): ``` sh -$ npm install -$ npm test +$ npm install #build +$ npm test #run tests ``` ### Adding tests diff --git a/src/parser-os.js b/src/parser-os.js index ad94a42..d48e61a 100644 --- a/src/parser-os.js +++ b/src/parser-os.js @@ -1,6 +1,7 @@ import { getFirstMatch, getWindowsVersionName, + getAndroidVersionName, } from './utils'; export default [ @@ -65,10 +66,15 @@ export default [ }, describe(ua) { const version = getFirstMatch(/android[\s/-](\d+(\.\d+)*)/i, ua); - return { + const versionName = getAndroidVersionName(version); + const os = { name: 'Android', - version, + version }; + if (versionName) { + os.versionName = versionName; + } + return os; }, }, diff --git a/src/parser-platforms.js b/src/parser-platforms.js index 3280e26..3435676 100644 --- a/src/parser-platforms.js +++ b/src/parser-platforms.js @@ -12,6 +12,22 @@ const TYPES_LABELS = { */ export default [ + /* Huawei */ + { + test: [/huawei/i], + describe(ua) { + const model = getFirstMatch(/(can\-l01)/i, ua) && "Nova"; + const platform = { + type: TYPES_LABELS.mobile, + vendor: 'Huawei' + }; + if (model) { + platform.model = model; + } + return platform; + }, + }, + /* Nexus Tablet */ { test: [/nexus\s*(?:7|8|9|10).*/i], diff --git a/src/utils.js b/src/utils.js index b903ba9..65895b4 100644 --- a/src/utils.js +++ b/src/utils.js @@ -51,6 +51,51 @@ class Utils { } } + /** + * Get Android version name + * 1.5 - Cupcake + * 1.6 - Donut + * 2.0 - Eclair + * 2.1 - Eclair + * 2.2 - Froyo + * 2.x - Gingerbread + * 3.x - Honeycomb + * 4.0 - Ice Cream Sandwich + * 4.1 - Jelly Bean + * 4.4 - KitKat + * 5.x - Lollipop + * 6.x - Marshmallow + * 7.x - Nougat + * 8.x - Oreo + * 9.x - ? + * + * @example + * getAndroidVersionName("7.0") // 'Nougat' + * + * @param {string} version + * @return {string} versionName + */ + static getAndroidVersionName(version) { + const v = version.split('.').splice(0, 2).map(function(s) { + return parseInt(s) || 0; + }).push(0); + if (v[0] == 1 && v[1] < 5) return undefined; + if (v[0] == 1 && v[1] < 6) return "Cupcake"; + if (v[0] == 1 && v[1] >= 6) return "Donut"; + if (v[0] == 2 && v[1] < 2) return "Eclair"; + if (v[0] == 2 && v[1] == 2) return "Froyo"; + if (v[0] == 2 && v[1] > 2) return "Gingerbread"; + if (v[0] == 3) return "Honeycomb"; + if (v[0] == 4 && v[1] < 1) return "Ice Cream Sandwich"; + if (v[0] == 4 && v[1] < 4) return "Jelly Bean"; + if (v[0] == 4 && v[1] >= 4) return "KitKat"; + if (v[0] == 5) return "Lollipop"; + if (v[0] == 6) return "Marshmallow"; + if (v[0] == 7) return "Nougat"; + if (v[0] == 8) return "Oreo"; + return undefined; + } + /** * Get version precisions count * diff --git a/test/acceptance/useragentstrings.yml b/test/acceptance/useragentstrings.yml index adc270a..71047d2 100644 --- a/test/acceptance/useragentstrings.yml +++ b/test/acceptance/useragentstrings.yml @@ -1,5 +1,21 @@ --- Chrome: + - + ua: "Mozilla/5.0 (Linux; Android 7.0; HUAWEI CAN-L01) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.99 Mobile Safari/537.36" + spec: + browser: + name: "Chrome" + version: "71.0.3578.99" + os: + name: "Android" + version: "7.0" + versionName: "Nougat" + platform: + type: "mobile" + vendor: "Huawei" + model: "Nova" + engine: + name: "Blink" - ua: "Mozilla/5.0 (Linux; Android 5.1.1; Nexus 9 Build/LMY48T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.83 Safari/537.36" spec: @@ -9,6 +25,7 @@ os: name: "Android" version: "5.1.1" + versionName: "Lollipop" platform: type: "tablet" vendor: "Nexus" @@ -23,6 +40,7 @@ os: name: "Android" version: "4.4.2" + versionName: "KitKat" platform: type: "tablet" vendor: "Nexus" @@ -37,6 +55,7 @@ os: name: "Android" version: "4.3" + versionName: "Jelly Bean" platform: type: "mobile" vendor: "Nexus" @@ -51,6 +70,7 @@ os: name: "Android" version: "4.1" + versionName: "Jelly Bean" platform: type: "mobile" vendor: "Nexus" @@ -66,6 +86,7 @@ os: name: "Android" version: "4.0.3" + versionName: "Ice Cream Sandwich" platform: type: "tablet" engine: @@ -80,6 +101,7 @@ os: name: "Android" version: "4.0.3" + versionName: "Ice Cream Sandwich" platform: type: "mobile" vendor: "Nexus" @@ -237,6 +259,7 @@ os: name: "Android" version: "5.0.2" + versionName: "Lollipop" platform: type: "tablet" engine: @@ -250,6 +273,7 @@ os: name: "Android" version: "6.0.99" + versionName: "Marshmallow" platform: type: "tablet" engine: @@ -263,6 +287,7 @@ os: name: "Android" version: "7.0" + versionName: "Nougat" platform: type: "mobile" vendor: "Nexus" @@ -277,6 +302,7 @@ os: name: "Android" version: "8.0.0" + versionName: "Oreo" platform: type: "mobile" engine: @@ -291,6 +317,7 @@ os: name: "Android" version: "4.0.3" + versionName: "Ice Cream Sandwich" platform: type: "tablet" vendor: "Amazon" @@ -322,6 +349,7 @@ os: name: "Android" version: "2.3.4" + versionName: "Gingerbread" platform: type: "tablet" vendor: "Amazon" @@ -353,6 +381,7 @@ os: name: "Android" version: "4.4.2" + versionName: "KitKat" platform: type: "tablet" vendor: "Nexus" @@ -367,6 +396,7 @@ os: name: "Android" version: "4.3" + versionName: "Jelly Bean" platform: type: "mobile" vendor: "Nexus" @@ -395,6 +425,7 @@ os: name: "Android" version: "4.3" + versionName: "Jelly Bean" platform: type: "mobile" engine: @@ -409,6 +440,7 @@ os: name: "Android" version: "4.4.2" + versionName: "KitKat" platform: type: "tablet" engine: @@ -588,6 +620,7 @@ os: name: "Android" version: "6.0" + versionName: "Marshmallow" platform: type: "mobile" engine: @@ -633,6 +666,7 @@ os: name: "Android" version: "5.1.1" + versionName: "Lollipop" platform: type: "mobile" vendor: "Nexus" @@ -1309,6 +1343,7 @@ os: name: "Android" version: "8.0" + versionName: "Oreo" platform: type: "mobile" engine: