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

New matchesUA return signature #34

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,27 @@ const { matchesUA } = require('browserslist-useragent')

matchesUA(userAgentString, options)

// with browserslist config inferred
// With explicit browserslist config:
matchesUA('Mozilla/5.0 (Windows NT 10.0; rv:54.0) Gecko/20100101 Firefox/54.0', { browsers: ['Firefox > 53']})
// Returns:
// {
// matches: true,
// userAgent: {
// family: 'Firefox',
// version: '54.0.0'
// }
// }

// With inferred browserslist config:
matchesUA('Mozilla/5.0 (Windows NT 10.0; rv:54.0) Gecko/20100101 Firefox/54.0')
//returns boolean

// with explicit browserslist
matchesUA('Mozilla/5.0 (Windows NT 10.0; rv:54.0) Gecko/20100101 Firefox/54.0', { browsers: ['Firefox > 53']})
// returns true
// With an unrecognizable user agent:
matchesUA('unrecognizable')
// Returns:
// {
// matches: false,
// userAgent: null
// }
```

| Option | Default Value | Description |
Expand Down
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,13 @@ const matchesUA = (uaString, opts) => {
allowHigherVersions: opts._allowHigherVersions || opts.allowHigherVersions
}

return parsedBrowsers.some(browser => {
return (
return {
matches: parsedBrowsers.some(browser => (
browser.family.toLowerCase() === resolvedUserAgent.family.toLocaleLowerCase() &&
compareBrowserSemvers(resolvedUserAgent.version, browser.version, options)
)
})
)),
userAgent: resolvedUserAgent.family === 'Other' ? null : resolvedUserAgent
}
}

module.exports = {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
"node": ">= 6.x.x"
},
"scripts": {
"prepare": "mkdir -p lib && babel index.js --out-file lib/index.js",
"test": "jest",
"prepublish": "yarn run build",
"build": "mkdir -p lib && babel index.js --out-file lib/index.js"
"prepublishOnly": "npm run prepare && npm test"
},
"dependencies": {
"browserslist": "^4.0.1",
Expand Down
178 changes: 144 additions & 34 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ it('resolves chrome/android properly', () => {
})

expect(matchesUA(CustomUserAgentString.YANDEX, { browsers: ['Chrome >= 63']}))
.toBeTruthy()
.toMatchObject({ matches: true })
})

it('resolves firefox properly', () => {
Expand Down Expand Up @@ -166,22 +166,58 @@ it('resolves samsung browser properly', () => {

it('detects if browserslist matches UA', () => {
expect(matchesUA(ua.firefox.androidPhone('40.0.1'), {browsers: ['Firefox >= 40']}))
.toBeTruthy()
.toEqual({
matches: true,
userAgent: {
family: 'Firefox',
version: '40.0.0'
}
})

expect(matchesUA(ua.firefox('30.0.0'), {browsers: ['Firefox >= 10.0.0']}))
.toBeTruthy()
.toEqual({
matches: true,
userAgent: {
family: 'Firefox',
version: '30.0.0'
}
})

expect(matchesUA(ua.chrome.iOS('11.0.0'), {browsers: ['iOS >= 10.3.0']}))
.toBeTruthy()
.toEqual({
matches: true,
userAgent: {
family: 'iOS',
version: '11.0.0'
}
})

expect(matchesUA(ua.safari.iOS('11.0.0'), {browsers: ['iOS >= 10.3.0']}))
.toBeTruthy()
.toEqual({
matches: true,
userAgent: {
family: 'iOS',
version: '11.0.0'
}
})

expect(matchesUA(CustomUserAgentString.SAMSUNG_BROWSER_6_2, {browsers: ['Samsung >= 7']}))
.toBeFalsy()
.toEqual({
matches: false,
userAgent: {
family: 'Samsung',
version: '6.2.0'
}
})

expect(matchesUA(CustomUserAgentString.SAMSUNG_BROWSER_7_2, {browsers: ['Samsung >= 7']}))
.toBeTruthy()
.toEqual({
matches: true,
userAgent: {
family: 'Samsung',
version: '7.2.0'
}
})

const modernList = [
"Firefox >= 53",
Expand All @@ -191,102 +227,176 @@ it('detects if browserslist matches UA', () => {
]

expect(matchesUA(ua.safari.iOS(9), {browsers: modernList}))
.toBeFalsy()
.toEqual({
matches: false,
userAgent: {
family: 'iOS',
version: '9.0.0'
}
})

expect(matchesUA(ua.chrome.androidPhone(57), {browsers: modernList}))
.toBeFalsy()
.toEqual({
matches: false,
userAgent: {
family: 'Chrome',
version: '57.0.0'
}
})

expect(matchesUA(ua.firefox.androidPhone(52), {browsers: modernList}))
.toBeFalsy()
.toEqual({
matches: false,
userAgent: {
family: 'Firefox',
version: '52.0.0'
}
})

expect(matchesUA(ua.firefox(56), {browsers: modernList}))
.toBeTruthy()
.toEqual({
matches: true,
userAgent: {
family: 'Firefox',
version: '56.0.0'
}
})

expect(matchesUA(ua.edge(14), {browsers: modernList}))
.toBeFalsy()
.toEqual({
matches: false,
userAgent: {
family: 'Edge',
version: '14.0.0'
}
})

expect(matchesUA(ua.chrome(64), {browsers: modernList}))
.toBeTruthy()
.toEqual({
matches: true,
userAgent: {
family: 'Chrome',
version: '64.0.0'
}
})

expect(matchesUA(ua.chrome.androidWebview('4.3.3'), {browsers: modernList}))
.toBeFalsy()
.toEqual({
matches: false,
userAgent: {
family: 'Android',
version: '4.3.3'
}
})
})

it('can interpret various variations in specifying browser names', () => {
expect(matchesUA(ua.chrome(49), {browsers: ['and_chr >= 49']}))
.toBeTruthy()
.toEqual({
matches: true,
userAgent: {
family: 'Chrome',
version: '49.0.0'
}
})

expect(matchesUA(ua.safari.iOS('10.3.0'), {browsers: ['ios_saf >= 10.1.0']}))
.toBeTruthy()
.toEqual({
matches: true,
userAgent: {
family: 'iOS',
version: '10.3.0'
}
})

expect(matchesUA(ua.safari('10.3.0'), {browsers: ['ios_saf >= 10.1.0']}))
.toBeTruthy()
.toEqual({
matches: true,
userAgent: {
family: 'iOS',
version: '10.3.0'
}
})

expect(matchesUA(ua.firefox.androidPhone('46.0.0'), {browsers: ['FirefoxAndroid >= 41.1.0']}))
.toBeTruthy()
.toEqual({
matches: true,
userAgent: {
family: 'Firefox',
version: '46.0.0'
}
})
})

it('ignorePatch option works correctly', () => {
expect(matchesUA(ua.firefox('49.0.1'), {browsers: ['ff >= 44'], ignorePatch: false}))
.toBeFalsy()
.toMatchObject({ matches: false })

expect(matchesUA(ua.firefox('49.0.1'), {browsers: ['ff >= 44'], ignorePatch: true}))
.toBeTruthy()
.toMatchObject({ matches: true })

expect(
matchesUA(
ua.firefox('49.1.1'),
{browsers: ['ff >= 44'], ignorePatch: true, ignoreMinor: false},
),
)
.toBeFalsy()
.toMatchObject({ matches: false })
})

it('ignoreMinor option works correctly', () => {
expect(matchesUA(ua.firefox('49.1.0'), {browsers: ['ff >= 44'], ignoreMinor: false}))
.toBeFalsy()
.toMatchObject({ matches: false })

expect(matchesUA(ua.firefox('49.1.0'), {browsers: ['ff >= 44'], ignoreMinor: true}))
.toBeTruthy()
.toMatchObject({ matches: true })

expect(
matchesUA(
ua.firefox('49.1.3'),
{browsers: ['ff >= 44'], ignoreMinor: true, ignorePatch: false},
),
)
.toBeTruthy()
.toMatchObject({ matches: true })
})

it('_allowHigherVersions and allowHigherVersions work correctly', () => {
expect(matchesUA(ua.chrome('99'), {browsers: ['chrome >= 60'], _allowHigherVersions: false}))
.toBeFalsy()
.toMatchObject({ matches: false })

expect(matchesUA(ua.chrome('99'), {browsers: ['chrome >= 60'], allowHigherVersions: false}))
.toBeFalsy()
.toMatchObject({ matches: false })

expect(matchesUA(ua.chrome('66'), {browsers: ['chrome >= 60'], _allowHigherVersions: true}))
.toBeTruthy()
.toMatchObject({ matches: true })

expect(matchesUA(ua.chrome('66'), {browsers: ['chrome >= 60'], allowHigherVersions: true}))
.toBeTruthy()
.toMatchObject({ matches: true })

expect(matchesUA(CustomUserAgentString.SAMSUNG_BROWSER_6_2, {browsers: ['samsung >= 3'], allowHigherVersions: true}))
.toBeTruthy()
.toMatchObject({ matches: true })

expect(matchesUA(CustomUserAgentString.SAMSUNG_BROWSER_7_4, {browsers: ['samsung >= 3'], allowHigherVersions: true}))
.toBeTruthy()
.toMatchObject({ matches: true })

expect(matchesUA(CustomUserAgentString.SAMSUNG_BROWSER_8_2, {browsers: ['samsung >= 3'], allowHigherVersions: true}))
.toBeTruthy()
.toMatchObject({ matches: true })

// latest version of samsung browser reported by browserslist is 7.2.0
expect(matchesUA(CustomUserAgentString.SAMSUNG_BROWSER_6_2, {browsers: ['samsung >= 3'], allowHigherVersions: false}))
.toBeTruthy()
.toMatchObject({ matches: true })

expect(matchesUA(CustomUserAgentString.SAMSUNG_BROWSER_7_4, {browsers: ['samsung >= 3'], allowHigherVersions: false}))
.toBeFalsy()
.toMatchObject({ matches: false })

expect(matchesUA(CustomUserAgentString.SAMSUNG_BROWSER_8_2, {browsers: ['samsung >= 3'], allowHigherVersions: false}))
.toBeFalsy()
.toMatchObject({ matches: false })
})

it('handles an unrecognizable UA', () => {
expect(matchesUA('unrecognizable', {browsers: ['Chrome >= 60']}))
.toEqual({
matches: false,
userAgent: null
})
})