Skip to content

Commit

Permalink
feat(deviceInfo): add isMobile flag
Browse files Browse the repository at this point in the history
  • Loading branch information
oljekechoro authored Aug 3, 2020
1 parent 243da82 commit bfd90b6
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 8 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
},
"homepage": "https://github.com/qiwi/event-transmitter#readme",
"dependencies": {
"@qiwi/substrate": "^1.18.35",
"@qiwi/substrate": "^1.18.37",
"@types/lodash.once": "^4.1.6",
"fast-luhn": "^1.1.1",
"lodash.once": "^4.1.1",
Expand All @@ -71,6 +71,7 @@
"@types/lodash.set": "^4.3.6",
"@types/platform": "^1.3.2",
"@types/safe-json-stringify": "^1.1.1",
"@types/user-agents": "^1.0.0",
"@typescript-eslint/eslint-plugin": "^3.6.1",
"@typescript-eslint/parser": "^3.6.1",
"coveralls": "^3.1.0",
Expand All @@ -87,7 +88,8 @@
"typedoc": "^0.17.8",
"typedoc-plugin-external-module-name": "^4.0.3",
"typescript": "^3.9.6",
"typescript-eslint-parser": "^22.0.0"
"typescript-eslint-parser": "^22.0.0",
"user-agents": "^1.0.559"
},
"release": {
"branch": "master",
Expand Down
3 changes: 2 additions & 1 deletion src/main/ts/pipes/deviceInfo.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IPipe, IPipeOutput, ITransmittable } from '../interfaces'
import { IPromise, IDeviceInfo } from '@qiwi/substrate'
import platform from 'platform'
import { set, clone } from '../utils'
import { set, clone, isMobile } from '../utils'

export const type = 'device-info'

Expand All @@ -14,6 +14,7 @@ export const getDeviceInfo = (userAgent?: string): IDeviceInfo => {
version,
layout,
},
isMobile: isMobile(userAgent || window.navigator.userAgent),
model: {
name: product,
manufacturer,
Expand Down
10 changes: 10 additions & 0 deletions src/main/ts/utils/deviceTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const isMobile = (userAgent: string): boolean => {
const ua = userAgent.toLowerCase()
return (
// detecting mobile and tablet https://stackoverflow.com/questions/11381673/detecting-a-mobile-browser
/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino|android|ipad|playbook|silk/i
.test(ua) ||
/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55\/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[23]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk\/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i
.test(ua.substr(0, 4))
)
}
1 change: 1 addition & 0 deletions src/main/ts/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export { deepMap } from './deepmap'
export { executeFailproof } from './executeFailproof'
export { clone } from './clone'
export { set }
export { isMobile } from './deviceTypes'

export const identity = <T>(v:T):T => v
3 changes: 2 additions & 1 deletion src/test/ts/pipes/deviceInfo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createDeviceInfoPipe, getDeviceInfo } from '../../../main/ts'
import { IDeviceInfo } from '@qiwi/substrate-types'
import { IDeviceInfo } from '@qiwi/substrate'

describe('deviceInfoPipe', () => {
it('is returned by factory', () => {
Expand All @@ -23,6 +23,7 @@ describe('getDeviceInfo', () => {
name: null,
manufacturer: null,
},
isMobile: false,
os: {
architecture: 64,
family: 'OS X',
Expand Down
2 changes: 1 addition & 1 deletion src/test/ts/pipes/http.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HttpMethod } from '@qiwi/substrate-types'
import { HttpMethod } from '@qiwi/substrate'
import { createHttpPipe, ITransmittable } from '../../../main/ts'

import 'cross-fetch/polyfill'
Expand Down
2 changes: 1 addition & 1 deletion src/test/ts/pipes/httpFallback.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HttpMethod } from '@qiwi/substrate-types'
import { HttpMethod } from '@qiwi/substrate'
import { createHttpPipeFallback, ITransmittable } from '../../../main/ts'

import 'cross-fetch/polyfill'
Expand Down
2 changes: 1 addition & 1 deletion src/test/ts/transmitter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createTransmitter } from '../../main/ts/transmitter'
import { createHttpPipe } from '../../main/ts/pipes/http'
import { HttpMethod } from '@qiwi/substrate-types'
import { HttpMethod } from '@qiwi/substrate'

import 'cross-fetch/polyfill'

Expand Down
49 changes: 49 additions & 0 deletions src/test/ts/utils/deviceTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import UserAgent from 'user-agents'
import { isMobile } from '../../../main/ts/utils'

interface ITestCase {
description: string
userAgents: string[]
expected: boolean[]
}

const testCasesCount = 100

const checkTestCase = (functionToTest: (ua: string) => boolean) =>
({ description, userAgents, expected }: ITestCase) => {
describe(description, () => {
userAgents.forEach((userAgent, i) => {
test(userAgent, () => expect(functionToTest(userAgent)).toEqual(expected[i]))
})
})
}

const mobileUaFactory = new UserAgent({ deviceCategory: 'mobile' }).random
const desktopUaFactory = new UserAgent({ deviceCategory: 'desktop' }).random
const tabletUaFactory = new UserAgent({ deviceCategory: 'tablet' }).random

const mobileUserAgents = [...new Array(testCasesCount)].map(() => mobileUaFactory().data.userAgent)
const desktopUserAgents = [...new Array(testCasesCount)].map(() => desktopUaFactory().data.userAgent)
const tabletUserAgents = [...new Array(testCasesCount)].map(() => tabletUaFactory().data.userAgent)

describe('isMobile', () => {
const testCases: ITestCase[] = [
{
description: 'returns true for all mobile userAgents',
userAgents: mobileUserAgents,
expected: new Array(testCasesCount).fill(true),
},
{
description: 'returns false for all desktop userAgents',
userAgents: desktopUserAgents,
expected: new Array(testCasesCount).fill(false),
},
{
description: 'returns true for all tablet userAgents',
userAgents: tabletUserAgents,
expected: new Array(testCasesCount).fill(true),
},
]

testCases.forEach(checkTestCase(isMobile))
})
74 changes: 73 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -637,19 +637,38 @@
dependencies:
"@qiwi/substrate-types" "1.43.0"

"@qiwi/[email protected]":
version "1.17.50"
resolved "https://registry.yarnpkg.com/@qiwi/substrate-abstract/-/substrate-abstract-1.17.50.tgz#b3681e3534d72113b77e1e5d5ef547633c39285e"
integrity sha512-N6gLtWA1LNJ4akO24qddvZWn+u0lA32YSEsjagm/X0ET1qAPVt1FFJVdznVb/rcG57BLZAhzYDNryKaHJ+sfRg==
dependencies:
"@qiwi/substrate-types" "1.44.0"

"@qiwi/[email protected]":
version "1.0.36"
resolved "https://registry.yarnpkg.com/@qiwi/substrate-std/-/substrate-std-1.0.36.tgz#8a40f03c5aca1245b319b0991b4cee279c17cf45"
integrity sha512-bR4xihATZmegzlJct0E5HmJxXYm5WvIkZalmne11wIraM4qearG5q516OhKb9HArZpiVYyl4YD7I5Hi9c/dXdw==
dependencies:
"@qiwi/substrate-types" "1.43.0"

"@qiwi/[email protected]":
version "1.0.38"
resolved "https://registry.yarnpkg.com/@qiwi/substrate-std/-/substrate-std-1.0.38.tgz#c7822e249db21fad1fe695a505eb12a529a15f7a"
integrity sha512-aYUfOXXPG3yFNHnGCi81WeNAPaJJHKUcSAt94gA6Haaw9U0wu+1vEaiJXgsC5JZpAJu6gINDuqR6RdIl0aiX9w==
dependencies:
"@qiwi/substrate-types" "1.44.0"

"@qiwi/[email protected]":
version "1.43.0"
resolved "https://registry.yarnpkg.com/@qiwi/substrate-types/-/substrate-types-1.43.0.tgz#aeb6ea4e5c71b6295273b0912f6c96521447efc9"
integrity sha512-MhOaoc36ERjD32wmf07XlWpVm+ZKqU6ObSwKAkcHPl/HO/JI7XxrX8TpjuqJXuER+DmCxUFmwFMveqe9o80yUQ==

"@qiwi/substrate@^1.18.28", "@qiwi/substrate@^1.18.35":
"@qiwi/[email protected]":
version "1.44.0"
resolved "https://registry.yarnpkg.com/@qiwi/substrate-types/-/substrate-types-1.44.0.tgz#76874fc73b55f57c8c9ba07c592463dfa26ea92a"
integrity sha512-6EA4U3Hq9od/jUNUaxJH+7FEO/LEMo+PNT2zRA9j1PSVRSstGcjDWcxOHBCDsb40p+0ZzyNWpGIyuwqufsrQHg==

"@qiwi/substrate@^1.18.28":
version "1.18.35"
resolved "https://registry.yarnpkg.com/@qiwi/substrate/-/substrate-1.18.35.tgz#5de0f71b6b227c1c1d03bdbbc33fe088c6cb114e"
integrity sha512-CWBXwjeIjebp28/OFDHX7BopXq2O+M8XsU1ywxwBlRGOAtnu2FpymmxZNohCE87zyLQNF7PjovfxDK80Z2TVTQ==
Expand All @@ -658,6 +677,15 @@
"@qiwi/substrate-std" "1.0.36"
"@qiwi/substrate-types" "1.43.0"

"@qiwi/substrate@^1.18.37":
version "1.18.37"
resolved "https://registry.yarnpkg.com/@qiwi/substrate/-/substrate-1.18.37.tgz#057e9a6f50aed84242450eec95847a95e68eaee2"
integrity sha512-96Gb6Ti1TlHCAoy1wxfpETFLhCPltoELo1fqW0RciaYtn7IBkFMhYVwXNCWyVdr/m42+jyDHwLI0wxi0yubVCw==
dependencies:
"@qiwi/substrate-abstract" "1.17.50"
"@qiwi/substrate-std" "1.0.38"
"@qiwi/substrate-types" "1.44.0"

"@semantic-release/changelog@^5.0.1":
version "5.0.1"
resolved "https://registry.yarnpkg.com/@semantic-release/changelog/-/changelog-5.0.1.tgz#50a84b63e5d391b7debfe021421589fa2bcdafe4"
Expand Down Expand Up @@ -991,6 +1019,11 @@
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-1.0.1.tgz#0a851d3bd96498fa25c33ab7278ed3bd65f06c3e"
integrity sha512-l42BggppR6zLmpfU6fq9HEa2oGPEI8yrSPL3GITjfRInppYFahObbIQOQK3UGxEnyQpltZLaPe75046NOZQikw==

"@types/user-agents@^1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@types/user-agents/-/user-agents-1.0.0.tgz#aeb7546f50cea28358a019a70ab3ce8827ecf937"
integrity sha512-oOSdQ9CULdFN2SJ9NNHvPrkP9aJ6oAz7BiiAeMC4vca+AzFqktlGdetMjlbCA0J3AemMn7ToU3U74VgrMecHtA==

"@types/yargs-parser@*":
version "15.0.0"
resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-15.0.0.tgz#cb3f9f741869e20cce330ffbeb9271590483882d"
Expand Down Expand Up @@ -2307,6 +2340,11 @@ detect-indent@~5.0.0:
resolved "https://registry.yarnpkg.com/detect-indent/-/detect-indent-5.0.0.tgz#3871cc0a6a002e8c3e5b3cf7f336264675f06b9d"
integrity sha1-OHHMCmoALow+Wzz38zYmRnXwa50=

detect-indent@~6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/detect-indent/-/detect-indent-6.0.0.tgz#0abd0f549f69fc6659a254fe96786186b6f528fd"
integrity sha512-oSyFlqaTHCItVRGK5RmrmjB+CmaMOW7IaNA/kdxqhoa6d17j/5ce9O9eWXmV/KEdRwqpQA+Vqe8a8Bsybu4YnA==

detect-newline@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/detect-newline/-/detect-newline-2.1.0.tgz#f41f1c10be4b00e87b5f13da680759f2c5bfd3e2"
Expand Down Expand Up @@ -2342,6 +2380,11 @@ dir-glob@^3.0.0, dir-glob@^3.0.1:
dependencies:
path-type "^4.0.0"

docopt@~0.6.2:
version "0.6.2"
resolved "https://registry.yarnpkg.com/docopt/-/docopt-0.6.2.tgz#b28e9e2220da5ec49f7ea5bb24a47787405eeb11"
integrity sha1-so6eIiDaXsSffqW7JKR3h0Be6xE=

[email protected]:
version "1.5.0"
resolved "https://registry.yarnpkg.com/doctrine/-/doctrine-1.5.0.tgz#379dce730f6166f76cefa4e6707a159b02c5a6fa"
Expand Down Expand Up @@ -2371,6 +2414,15 @@ domexception@^2.0.1:
dependencies:
webidl-conversions "^5.0.0"

dot-json@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/dot-json/-/dot-json-1.2.0.tgz#16f62bbb15b0b834f5c8de299cfb9e4d494f2ac1"
integrity sha512-4bEM7KHFl/U9gAI5nIvU0/fwVzNnE713K339vcxAMtxd2D9mZP6o65UwlcXigJL4rfk90UM0J+D7IPIFYZMQ8Q==
dependencies:
detect-indent "~6.0.0"
docopt "~0.6.2"
underscore-keypath "~0.0.22"

dot-prop@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/dot-prop/-/dot-prop-3.0.0.tgz#1b708af094a49c9a0e7dbcad790aba539dac1177"
Expand Down Expand Up @@ -8174,6 +8226,18 @@ umask@^1.1.0, umask@~1.1.0:
resolved "https://registry.yarnpkg.com/umask/-/umask-1.1.0.tgz#f29cebf01df517912bb58ff9c4e50fde8e33320d"
integrity sha1-8pzr8B31F5ErtY/5xOUP3o4zMg0=

underscore-keypath@~0.0.22:
version "0.0.22"
resolved "https://registry.yarnpkg.com/underscore-keypath/-/underscore-keypath-0.0.22.tgz#48a528392bb6efc424be1caa56da4b5faccf264d"
integrity sha1-SKUoOSu278QkvhyqVtpLX6zPJk0=
dependencies:
underscore "*"

underscore@*:
version "1.10.2"
resolved "https://registry.yarnpkg.com/underscore/-/underscore-1.10.2.tgz#73d6aa3668f3188e4adb0f1943bd12cfd7efaaaf"
integrity sha512-N4P+Q/BuyuEKFJ43B9gYuOj4TQUHXX+j2FqguVOpjkssLUUrnJofCcBccJSCoeturDoZU6GorDTHSvUDlSQbTg==

union-value@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/union-value/-/union-value-1.0.1.tgz#0b6fe7b835aecda61c6ea4d4f02c14221e109847"
Expand Down Expand Up @@ -8292,6 +8356,14 @@ use@^3.1.0:
resolved "https://registry.yarnpkg.com/use/-/use-3.1.1.tgz#d50c8cac79a19fbc20f2911f56eb973f4e10070f"
integrity sha512-cwESVXlO3url9YWlFW/TA9cshCEhtu7IKJ/p5soJ/gGpj7vbvFrAY/eIioQ6Dw23KjZhYgiIo8HOs1nQ2vr/oQ==

user-agents@^1.0.559:
version "1.0.559"
resolved "https://registry.yarnpkg.com/user-agents/-/user-agents-1.0.559.tgz#389817f79dff667d9e5c8803631c41043e8eccd8"
integrity sha512-HdAlNS3vDxOGMRwmv8or05xL96MV3CEwQhUSFTCRoOvTOEnWhTEBPAHRry/xZpVTTOtx77UHMal8YKcx6fs7Lg==
dependencies:
dot-json "^1.2.0"
lodash.clonedeep "^4.5.0"

util-deprecate@^1.0.1, util-deprecate@~1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"
Expand Down

0 comments on commit bfd90b6

Please sign in to comment.