Skip to content

Commit b490110

Browse files
committed
Merge branch 'develop' of github.com:faisalman/ua-parser-js
2 parents d141915 + cb5da5e commit b490110

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-6
lines changed

readme.md

+47-5
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,60 @@ JavaScript library to detect Browser, Engine, OS, CPU, and Device type/model fro
1919
* Source : https://github.com/faisalman/ua-parser-js
2020

2121
# Documentation
22+
### UAParser([user-agent][,extensions])
23+
typeof `user-agent` "string".
24+
25+
typeof `extensions` "array".
26+
27+
In The Browser environment you dont need to pass the user-agent string to the function, you can just call the funtion and it should automatically get the string from the `window.navigator.userAgent`, but that is not the case in nodejs. The user-agent string must be passed in nodejs for the function to work.
28+
Usually you can find the user agent in:
29+
`request.headers["user-agent"]`.
2230

23-
## Constructor
2431

32+
## Constructor
33+
When you call `UAParser` with the `new` keyword `UAParser` will return a new instance with an empty result object, you have to call one of the available methods to get the information from the user-agent string.
34+
Like so:
2535
* `new UAParser([uastring][,extensions])`
26-
* returns new instance
36+
```js
37+
let parser = new UAParser("user-agent"); // you need to pass the user-agent for nodejs
38+
console.log(parser); // {}
39+
let parserResults = parser.getResults();
40+
console.log(parserResults);
41+
/** {
42+
"ua": "",
43+
"browser": {},
44+
"engine": {},
45+
"os": {},
46+
"device": {},
47+
"cpu": {}
48+
} */
49+
```
2750

51+
When you call UAParser without the `new` keyword, it will automatically call `getResults()` function and return the parsed results.
2852
* `UAParser([uastring][,extensions])`
2953
* returns result object `{ ua: '', browser: {}, cpu: {}, device: {}, engine: {}, os: {} }`
3054

3155
## Methods
3256

57+
#### Methods table
58+
The methods are self explanatory, here's a small overview on all the available methods:
59+
* `getResult()` - returns all function object calls, user-agent string, browser info, cpu, device, engine, os:
60+
`{ ua: '', browser: {}, cpu: {}, device: {}, engine: {}, os: {} }`.
61+
62+
* `getBrowser()` - returns the browser name and version.
63+
* `getDevice()` - returns the device model, type, vendor.
64+
* `getEngine()` - returns the current browser engine name and version.
65+
* `getOS()` - returns the running operating system name and version.
66+
* `getCPU()` - returns CPU architectural design name.
67+
* `getUA()` - returns the user-agent string.
68+
* `setUA(user-agent)` - set a custom user-agent to be parsed.
69+
70+
71+
---
72+
73+
* `getResult()`
74+
* returns `{ ua: '', browser: {}, cpu: {}, device: {}, engine: {}, os: {} }`
75+
3376
* `getBrowser()`
3477
* returns `{ name: '', version: '' }`
3578

@@ -114,9 +157,6 @@ VectorLinux, WebOS, Windows [Phone/Mobile], Zenwalk, ...
114157
68k, amd64, arm[64/hf], avr, ia[32/64], irix[64], mips[64], pa-risc, ppc, sparc[64]
115158
```
116159

117-
* `getResult()`
118-
* returns `{ ua: '', browser: {}, cpu: {}, device: {}, engine: {}, os: {} }`
119-
120160
* `getUA()`
121161
* returns UA string of current instance
122162

@@ -200,6 +240,8 @@ VectorLinux, WebOS, Windows [Phone/Mobile], Zenwalk, ...
200240

201241
## Using node.js
202242

243+
Note: Device information is not available in the NodeJS environment.
244+
203245
```sh
204246
$ npm install ua-parser-js
205247
```

src/ua-parser.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@
373373
/////////////////////////
374374

375375
// Samsung
376-
/\b(sch-i[89]0\d|shw-m380s|sm-[pt]\w{2,4}|gt-[pn]\d{2,4}|sgh-t8[56]9|nexus 10)/i
376+
/\b(sch-i[89]0\d|shw-m380s|sm-[ptx]\w{2,4}|gt-[pn]\d{2,4}|sgh-t8[56]9|nexus 10)/i
377377
], [MODEL, [VENDOR, SAMSUNG], [TYPE, TABLET]], [
378378
/\b((?:s[cgp]h|gt|sm)-\w+|galaxy nexus)/i,
379379
/samsung[- ]([-\w]+)/i,

test/device-test.json

+9
Original file line numberDiff line numberDiff line change
@@ -1638,6 +1638,15 @@
16381638
"type": "tablet"
16391639
}
16401640
},
1641+
{
1642+
"desc": "Samsung Galaxy Tab S8",
1643+
"ua": "Mozilla/5.0 (Linux; Android 12; SM-X706B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.53 Safari/537.36",
1644+
"expect": {
1645+
"vendor": "Samsung",
1646+
"model": "SM-X706B",
1647+
"type": "tablet"
1648+
}
1649+
},
16411650
{
16421651
"desc": "Samsung Galaxy Tab S",
16431652
"ua": "Mozilla/5.0 (Linux; Android 4.4.2; SM-T700 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.135 Safari/537.36",

0 commit comments

Comments
 (0)