Skip to content
This repository was archived by the owner on Dec 20, 2020. It is now read-only.

Commit 81622f2

Browse files
author
Shane Tomlinson
committed
Add support for toVersionString on result.os and result.ua
1 parent 4657288 commit 81622f2

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

index.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,52 @@ function readYAML(file) {
1313
return yaml.eval(data);
1414
}
1515

16+
// uap-ref-impl does not provide a `toVersionString` function. Add it.
17+
function ensureToVersionString(context) {
18+
if (context && ! context.toVersionString) {
19+
context.toVersionString = toVersionString.bind(context);
20+
}
21+
}
22+
23+
// taken from https://github.com/tobie/ua-parser/blob/8bc22f74e3a4515633d975cd535e94e5ae9699d2/js/lib/os.js#L12
24+
function toVersionString () {
25+
var output = '';
26+
if (this.major != null) {
27+
output += this.major;
28+
if (this.minor != null) {
29+
output += '.' + this.minor;
30+
if (this.patch != null) {
31+
if (startsWithDigit(this.patch)) {
32+
output += '.';
33+
}
34+
output += this.patch;
35+
if (this.patchMinor != null) {
36+
if (startsWithDigit(this.patchMinor)) {
37+
output += '.';
38+
}
39+
output += this.patchMinor;
40+
}
41+
}
42+
}
43+
}
44+
45+
return output;
46+
}
47+
48+
function startsWithDigit(str) {
49+
return /^\d/.test(str);
50+
}
51+
52+
var _parse = refImpl.parse;
53+
refImpl.parse = function (ua) {
54+
var result = _parse.call(refImpl, ua);
55+
56+
// ua-parser results had a `toVersionString` function on `os` and `ua`.
57+
// Add the missing support.
58+
ensureToVersionString(result.os);
59+
ensureToVersionString(result.ua);
60+
61+
return result;
62+
};
63+
1664
module.exports = refImpl;

0 commit comments

Comments
 (0)