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

Commit 13fa830

Browse files
author
Shane Tomlinson
committed
Add support for toVersionString on result.os and result.ua
1 parent 621836e commit 13fa830

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

index.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,52 @@ function readYAML(fileName) {
99
return yaml.eval(data);
1010
}
1111

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

0 commit comments

Comments
 (0)