PageRenderTime 485ms CodeModel.GetById 282ms app.highlight 5ms RepoModel.GetById 196ms app.codeStats 0ms

/js/lib/Socket.IO-node/support/expresso/deps/jscoverage/report.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 76 lines | 67 code | 4 blank | 5 comment | 14 complexity | c5a58ebc0cbb9d9dfd86be069c8004b8 MD5 | raw file
 1if (! window.jscoverage_report) {
 2  window.jscoverage_report = function jscoverage_report(dir) {
 3    var createRequest = function () {
 4      if (window.XMLHttpRequest) {
 5        return new XMLHttpRequest();
 6      }
 7      else if (window.ActiveXObject) {
 8        return new ActiveXObject("Microsoft.XMLHTTP");
 9      }
10    };
11
12    var pad = function (s) {
13      return '0000'.substr(s.length) + s;
14    };
15
16    var quote = function (s) {
17      return '"' + s.replace(/[\u0000-\u001f"\\\u007f-\uffff]/g, function (c) {
18        switch (c) {
19        case '\b':
20          return '\\b';
21        case '\f':
22          return '\\f';
23        case '\n':
24          return '\\n';
25        case '\r':
26          return '\\r';
27        case '\t':
28          return '\\t';
29        // IE doesn't support this
30        /*
31        case '\v':
32          return '\\v';
33        */
34        case '"':
35          return '\\"';
36        case '\\':
37          return '\\\\';
38        default:
39          return '\\u' + pad(c.charCodeAt(0).toString(16));
40        }
41      }) + '"';
42    };
43
44    var json = [];
45    for (var file in top._$jscoverage) {
46      var coverage = top._$jscoverage[file];
47      var array = [];
48      var length = coverage.length;
49      for (var line = 0; line < length; line++) {
50        var value = coverage[line];
51        if (value === undefined || value === null) {
52          value = 'null';
53        }
54        array.push(value);
55      }
56      json.push(quote(file) + ':[' + array.join(',') + ']');
57    }
58    json = '{' + json.join(',') + '}';
59
60    var request = createRequest();
61    var url = '/jscoverage-store';
62    if (dir) {
63      url += '/' + encodeURIComponent(dir);
64    }
65    request.open('POST', url, false);
66    request.setRequestHeader('Content-Type', 'application/json');
67    request.setRequestHeader('Content-Length', json.length.toString());
68    request.send(json);
69    if (request.status === 200 || request.status === 201 || request.status === 204) {
70      return request.responseText;
71    }
72    else {
73      throw request.status;
74    }
75  };
76}