Skip to content

Commit 5ea4700

Browse files
author
Shubham Kanodia
committed
Prettier, add support for Opera mobile
1 parent ee94302 commit 5ea4700

File tree

12 files changed

+394
-293
lines changed

12 files changed

+394
-293
lines changed

.babelrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
22
"presets": ["es2015", "stage-2"]
3-
}
3+
}

.github/workflows/main.yml

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,18 @@ on: [push]
33

44
jobs:
55
build:
6-
76
runs-on: ubuntu-latest
87

98
strategy:
109
matrix:
1110
node-version: [12.x, 14.x, 16.x]
1211

1312
steps:
14-
- uses: actions/checkout@v2
15-
- name: Use Node.js ${{ matrix.node-version }}
16-
uses: actions/setup-node@v2
17-
with:
18-
node-version: ${{ matrix.node-version }}
19-
- run: yarn install
20-
- run: yarn build
21-
- run: yarn test
13+
- uses: actions/checkout@v2
14+
- name: Use Node.js ${{ matrix.node-version }}
15+
uses: actions/setup-node@v2
16+
with:
17+
node-version: ${{ matrix.node-version }}
18+
- run: yarn install
19+
- run: yarn build
20+
- run: yarn test

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.yarn/cache

.prettierrc.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"trailingComma": "es5",
3+
"tabWidth": 2,
4+
"semi": false,
5+
"singleQuote": true
6+
}

.yarn/install-state.gz

-5.33 KB
Binary file not shown.

CHANGELOG.md

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,63 @@
11
# v3.0.0
2+
23
- **Fix**: Don't crash when `options` parameter is not specified
34
- **Fix**: Use `loose` option to parse browsers versions. Doesn't crash on versions like `6.00.1` or 6.0.0005` etc.
45

56
- **Feature**: Added support for Samsung Internet
67

7-
- **Breaking**: Safari on desktop and on iOS are now considered as separate browsers.
8-
You'll need to use separate browserslist queries to target both of them - `['Safari >= 10', 'iOS >= 10.1']`. See #23
9-
- **Breaking**: Removing support for undocumented option to `matchesUA` - `all`.
10-
This was used internally, and shouldn't be breaking change for you unless you poked around code to discover it.
8+
- **Breaking**: Safari on desktop and on iOS are now considered as separate browsers.
9+
You'll need to use separate browserslist queries to target both of them - `['Safari >= 10', 'iOS >= 10.1']`. See #23
10+
- **Breaking**: Removing support for undocumented option to `matchesUA` - `all`.
11+
This was used internally, and shouldn't be breaking change for you unless you poked around code to discover it.
1112
- **Breaking**: Remove deprecated `_allowHigherVersions` flag. This has been replaced by `allowHigherVersions` in the past.
1213

1314
# v2.0.1
14-
- Remove deprecated _allowHigherVersions option from README
15+
16+
- Remove deprecated \_allowHigherVersions option from README
1517

1618
# v2.0.0
19+
1720
- **Feature**: Added Yandex to list of supported browsers
1821
- **Feature**: Added support for Yandex Browser
1922
- **Deprecation**: Deprecated `_allowHigherVersions`
2023

2124
#v1.2.1
25+
2226
- Fixed build link
2327

2428
#v1.2.0
29+
2530
- **Feature**: Updated packages and added support for headless chrome and chromium
2631
- Fixed links in badges
2732

2833
#v1.1.0
34+
2935
- **Feature**: Added support for selecting environment
3036
- Updated browserslist version
3137

3238
#v1.0.4
39+
3340
- delete gitignore
3441

3542
#v1.0.3
43+
3644
- Update README.md
3745

3846
#v1.0.2
47+
3948
- Second try at fixing logo
4049

4150
#v1.0.1
51+
4252
- Updated browserslist version
4353

4454
#v1.0.0
55+
4556
- Create LICENSE
4657
- /browserlist/ => browserslist
4758
- Refactored code and rewrote Readme
4859

4960
#v0.0.2
61+
5062
- Added install instructions
5163
- Initial commit

README.md

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,22 @@
55

66
<img align="right" width="120" height="120"
77
src="https://cdn.rawgit.com/pastelsky/browserslist-useragent/master/logo.svg" alt="Browserslist Useragent logo (original by Anton Lovchikov)" />
8-
98

10-
Find if a given user agent string satisfies a [browserslist](https://github.com/ai/browserslist) query.
9+
Find if a given user agent string satisfies a [browserslist](https://github.com/ai/browserslist) query.
1110

12-
It automatically reads the browserslist configuration specified in your project,
11+
It automatically reads the browserslist configuration specified in your project,
1312
but you can also specify the same using the `options` parameter.
1413

1514
**If you wish to target modern browsers, read [this](#when-querying-for-modern-browsers).**
1615

1716
## Installation
17+
1818
```bash
1919
npm install browserslist-useragent
2020
```
2121

2222
## Usage
23+
2324
```js
2425
const { matchesUA } = require('browserslist-useragent')
2526

@@ -30,41 +31,49 @@ matchesUA('Mozilla/5.0 (Windows NT 10.0; rv:54.0) Gecko/20100101 Firefox/54.0')
3031
//returns boolean
3132

3233
// with explicit browserslist
33-
matchesUA('Mozilla/5.0 (Windows NT 10.0; rv:54.0) Gecko/20100101 Firefox/54.0', { browsers: ['Firefox > 53']})
34+
matchesUA(
35+
'Mozilla/5.0 (Windows NT 10.0; rv:54.0) Gecko/20100101 Firefox/54.0',
36+
{ browsers: ['Firefox > 53'] }
37+
)
3438
// returns true
3539
```
3640

37-
| Option | Default Value | Description |
38-
|--------|---------------|------------ |
39-
| browsers || Manually provide a browserslist query (or an array of queries). Specifying this overrides the browserslist configuration specified in your project. |
40-
| env || When multiple browserslist [environments](https://github.com/ai/browserslist#environments) are specified, pick the config belonging to this environment.|
41-
| path | `process.cwd()` | Specify a folder to search for the browserslist config (if it differs from the current working directory) |
42-
| ignorePatch | `true` | Ignore differences in patch browser numbers |
43-
| ignoreMinor | `false` | Ignore differences in minor browser versions |
44-
| allowHigherVersions | `false` | For all the browsers in the browserslist query, return a match if the user agent version is equal to or higher than the one specified in browserslist. See [why](#when-querying-for-modern-browsers) this might be useful.
41+
| Option | Default Value | Description |
42+
| ------------------- | --------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
43+
| browsers | | Manually provide a browserslist query (or an array of queries). Specifying this overrides the browserslist configuration specified in your project. |
44+
| env | | When multiple browserslist [environments](https://github.com/ai/browserslist#environments) are specified, pick the config belonging to this environment. |
45+
| path | `process.cwd()` | Specify a folder to search for the browserslist config (if it differs from the current working directory) |
46+
| ignorePatch | `true` | Ignore differences in patch browser numbers |
47+
| ignoreMinor | `false` | Ignore differences in minor browser versions |
48+
| allowHigherVersions | `false` | For all the browsers in the browserslist query, return a match if the user agent version is equal to or higher than the one specified in browserslist. See [why](#when-querying-for-modern-browsers) this might be useful. |
4549

4650
## Supported browsers
47-
- Chrome (Chrome / Chromium / Yandex) as `and_chr` | `ChromeAndroid` | `Chrome`
48-
- Samsung Internet as `Samsung`
49-
- Firefox as `ff` | `and_ff` | `FirefoxAndroid` | `Firefox`
50-
- Safari iOS as `ios_saf` | `iOS`
51-
- Safari Desktop as `Safari`
52-
- IE as `ie` | `ie_mob`
53-
- Edge as `Edge`
54-
55-
PRs to add more _browserslist supported_ browsers are welcome 👋
56-
51+
52+
- Chrome (Chrome / Chromium / Yandex) as `and_chr` | `ChromeAndroid` | `Chrome`
53+
- Samsung Internet as `Samsung`
54+
- Firefox as `ff` | `and_ff` | `FirefoxAndroid` | `Firefox`
55+
- Safari iOS as `ios_saf` | `iOS`
56+
- Safari Desktop as `Safari`
57+
- IE as `ie` | `ie_mob`
58+
- Edge as `Edge`
59+
- OperaMobile as `OperaMobile`
60+
61+
PRs to add more _browserslist supported_ browsers are welcome 👋
62+
5763
## Notes
58-
- All browsers on iOS (Chrome, Firefox etc) use Safari's WebKit as the underlying engine, and hence will be resolved to Safari. Since `browserslist` is usually used for
64+
65+
- All browsers on iOS (Chrome, Firefox etc) use Safari's WebKit as the underlying engine, and hence will be resolved to Safari. Since `browserslist` is usually used for
5966
transpiling / autoprefixing for browsers, this behaviour is what's intended in most cases, but might surprise you otherwise.
60-
61-
- Right now, Chrome for Android and Firefox for Android are resolved to their desktop equivalents. The `caniuse` database does not currently store historical data for these browsers separately (except the last version) See [#156](https://github.com/ai/browserslist/issues/156). However,
62-
safari for iOS and desktop can be matched separately, since this data is available for both.
67+
68+
- Right now, Chrome for Android and Firefox for Android are resolved to their desktop equivalents. The `caniuse` database does not currently store historical data for these browsers separately (except the last version) See [#156](https://github.com/ai/browserslist/issues/156). However,
69+
safari for iOS and desktop can be matched separately, since this data is available for both.
6370

6471
## When querying for modern browsers
65-
- It is a good idea to update this package often so that browser definitions are upto date.
66-
- It is also a good idea to add `unreleased versions` to your browserslist query, and set `ignoreMinor` and `ignorePatch` to true so that alpha / beta / canary versions of browsers are matched.
67-
- In case you're unable to keep this package up-to-date, you can set the `allowHigherVersions` to `true`. For all the browsers specified in your browserslist query, this will return a match if the user agent version is equal to or higher than those specified in your browserslist query. Use this with care though, since it's a wildcard, and only lightly tested.
68-
69-
## Further reads
70-
- [Smart Bundling: Shipping legacy code to only legacy browsers](https://www.smashingmagazine.com/2018/10/smart-bundling-legacy-code-browsers/)
72+
73+
- It is a good idea to update this package often so that browser definitions are upto date.
74+
- It is also a good idea to add `unreleased versions` to your browserslist query, and set `ignoreMinor` and `ignorePatch` to true so that alpha / beta / canary versions of browsers are matched.
75+
- In case you're unable to keep this package up-to-date, you can set the `allowHigherVersions` to `true`. For all the browsers specified in your browserslist query, this will return a match if the user agent version is equal to or higher than those specified in your browserslist query. Use this with care though, since it's a wildcard, and only lightly tested.
76+
77+
## Further reads
78+
79+
- [Smart Bundling: Shipping legacy code to only legacy browsers](https://www.smashingmagazine.com/2018/10/smart-bundling-legacy-code-browsers/)

index.js

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,19 @@ function resolveUserAgent(uaString) {
3030
// renderer
3131
// @see https://github.com/Financial-Times/polyfill-service/pull/416
3232

33-
let strippedUA = uaString.replace(/((CriOS|OPiOS)\/(\d+)\.(\d+)\.(\d+)\.(\d+))/, '')
33+
let strippedUA = uaString.replace(
34+
/((CriOS|OPiOS)\/(\d+)\.(\d+)\.(\d+)\.(\d+))/,
35+
''
36+
)
3437

3538
// Yandex Browser uses Chromium as the udnerlying engine
3639
strippedUA = strippedUA.replace(/YaBrowser\/(\d+\.?)+/g, '')
3740

3841
// Yandex Search uses Chromium as the udnerlying engine
39-
strippedUA = strippedUA.replace(/YandexSearch\/(\d+\.?)+/g, '');
42+
strippedUA = strippedUA.replace(/YandexSearch\/(\d+\.?)+/g, '')
4043

4144
// Facebook Webview
42-
strippedUA = strippedUA.replace(/FB_IAB/g, '').replace(/FBAN\/FBIOS/g, '');
45+
strippedUA = strippedUA.replace(/FB_IAB/g, '').replace(/FBAN\/FBIOS/g, '')
4346

4447
const parsedUA = useragent.parse(strippedUA)
4548

@@ -60,12 +63,12 @@ function resolveUserAgent(uaString) {
6063
if (parsedUA.os.family === 'iOS') {
6164
return {
6265
family: 'iOS',
63-
version: [parsedUA.os.major, parsedUA.os.minor,
64-
parsedUA.os.patch].join('.'),
66+
version: [parsedUA.os.major, parsedUA.os.minor, parsedUA.os.patch].join(
67+
'.'
68+
),
6569
}
6670
}
6771

68-
6972
// Case C: The caniuse database does not contain
7073
// historical browser versions for so called `minor`
7174
// browsers like Chrome for Android, Firefox for Android etc
@@ -84,6 +87,13 @@ function resolveUserAgent(uaString) {
8487
}
8588
}
8689

90+
if (parsedUA.family === 'Opera Mobile') {
91+
return {
92+
family: 'OperaMobile',
93+
version: [parsedUA.major, parsedUA.minor, parsedUA.patch].join('.'),
94+
}
95+
}
96+
8797
if (parsedUA.family === 'Samsung Internet') {
8898
return {
8999
family: 'Samsung',
@@ -120,28 +130,30 @@ function resolveUserAgent(uaString) {
120130

121131
// Convert version to a semver value.
122132
// 2.5 -> 2.5.0; 1 -> 1.0.0;
123-
const semverify = (version) => semver.coerce(version, { loose: true }).version;
133+
const semverify = (version) => semver.coerce(version, { loose: true }).version
124134

125135
function flatten(arr) {
126136
return arr.reduce(function (flat, toFlatten) {
127-
return flat.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten);
128-
}, []);
137+
return flat.concat(
138+
Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten
139+
)
140+
}, [])
129141
}
130142

131143
// 10.0-10.2 -> 10.0, 10.1, 10.2
132144
function generateSemversInRange(versionRange) {
133145
const [start, end] = versionRange.split('-')
134146
const startSemver = semverify(start)
135147
const endSemver = semverify(end)
136-
const versionsInRange = [];
137-
let curVersion = startSemver;
148+
const versionsInRange = []
149+
let curVersion = startSemver
138150

139151
while (semver.gte(endSemver, curVersion)) {
140152
versionsInRange.push(curVersion)
141153
curVersion = semver.inc(curVersion, 'minor')
142154
}
143155

144-
return versionsInRange;
156+
return versionsInRange
145157
}
146158

147159
function normalizeQuery(query) {
@@ -158,14 +170,13 @@ function normalizeQuery(query) {
158170

159171
const parseBrowsersList = (browsersList) => {
160172
const browsers = browsersList
161-
.map(browser => {
173+
.map((browser) => {
162174
const [name, version] = browser.split(' ')
163175
return { name, version }
164176
})
165177
// #38 Filter out non-numerical browser versions
166-
.filter(browser => browser.version !== 'TP')
167-
.map(browser => {
168-
178+
.filter((browser) => browser.version !== 'TP')
179+
.map((browser) => {
169180
let normalizedName = browser.name
170181
let normalizedVersion = browser.version
171182

@@ -176,7 +187,7 @@ const parseBrowsersList = (browsersList) => {
176187
// browserslist might return ranges (9.0-9.2), unwrap them
177188
// see https://github.com/browserslist/browserslist-useragent/issues/41
178189
if (browser.version.indexOf('-') > 0) {
179-
return generateSemversInRange(browser.version).map(version => ({
190+
return generateSemversInRange(browser.version).map((version) => ({
180191
family: normalizedName,
181192
version,
182193
}))
@@ -188,7 +199,7 @@ const parseBrowsersList = (browsersList) => {
188199
}
189200
})
190201

191-
return flatten(browsers);
202+
return flatten(browsers)
192203
}
193204

194205
const compareBrowserSemvers = (versionA, versionB, options) => {
@@ -214,7 +225,7 @@ const compareBrowserSemvers = (versionA, versionB, options) => {
214225
const matchesUA = (uaString, opts = {}) => {
215226
// bail out early if the user agent is invalid
216227
if (!uaString) {
217-
return false;
228+
return false
218229
}
219230

220231
let normalizedQuery
@@ -223,8 +234,9 @@ const matchesUA = (uaString, opts = {}) => {
223234
}
224235
const browsers = browserslist(normalizedQuery, {
225236
env: opts.env,
226-
path: opts.path || process.cwd()
237+
path: opts.path || process.cwd(),
227238
})
239+
228240
const parsedBrowsers = parseBrowsersList(browsers)
229241

230242
const resolvedUserAgent = resolveUserAgent(uaString)
@@ -235,9 +247,10 @@ const matchesUA = (uaString, opts = {}) => {
235247
...opts,
236248
}
237249

238-
return parsedBrowsers.some(browser => {
250+
return parsedBrowsers.some((browser) => {
239251
return (
240-
browser.family.toLowerCase() === resolvedUserAgent.family.toLocaleLowerCase() &&
252+
browser.family.toLowerCase() ===
253+
resolvedUserAgent.family.toLocaleLowerCase() &&
241254
compareBrowserSemvers(resolvedUserAgent.version, browser.version, options)
242255
)
243256
})
@@ -248,4 +261,3 @@ module.exports = {
248261
resolveUserAgent,
249262
normalizeQuery,
250263
}
251-

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"babel-preset-es2015": "^6.24.1",
3232
"babel-preset-stage-2": "^6.24.1",
3333
"jest": "^23.6.0",
34+
"prettier": "^2.5.1",
3435
"useragent-generator": "^1.1.0"
3536
},
3637
"jest": {

0 commit comments

Comments
 (0)