Name: js-handler/node_modules/restify/node_modules/negotiator/lib/encoding.js 
1:
module.exports = preferredEncodings;
2:
preferredEncodings.preferredEncodings = preferredEncodings;
3:
 
4:
function parseAcceptEncoding(accept) {
5:
  var acceptableEncodings;
6:
 
7:
  if (accept) {
8:
    acceptableEncodings = accept.split(',').map(function(e) {
9:
      return parseEncoding(e.trim());
10:
    });
11:
  } else {
12:
    acceptableEncodings = [];
13:
  }
14:
 
15:
  if (!acceptableEncodings.some(function(e) {
16:
    return e.encoding === 'identity';
17:
  })) {
18:
    acceptableEncodings.push({
19:
      encoding: 'identity',
20:
      q: 0.1
21:
    });
22:
  }
23:
 
24:
  return acceptableEncodings.filter(function(e) {
25:
    return e && e.q > 0;
26:
  });
27:
}
28:
 
29:
function parseEncoding(s) {
30:
  var match = s.match(/^\s*(\S+?)\s*(?:;(.*))?$/);
31:
 
32:
  if (!match) return null;
33:
 
34:
  var encoding = match[1];
35:
  var q = 1;
36:
  if (match[2]) {
37:
    var params = match[2].split(';');
38:
    for (var i = 0; i < params.length; i ++) {
39:
      var p = params[i].trim().split('=');
40:
      if (p[0] === 'q') {
41:
        q = parseFloat(p[1]);
42:
        break;
43:
      }
44:
    }
45:
  }
46:
 
47:
  return {
48:
    encoding: encoding,
49:
    q: q
50:
  };
51:
}
52:
 
53:
function getEncodingPriority(encoding, accepted) {
54:
  return (accepted.filter(function(a) {
55:
    return specify(encoding, a);
56:
  }).sort(function (a, b) {
57:
    // revsort
58:
    return a.q === b.q ? 0 : a.q > b.q ? -1 : 1;
59:
  })[0] || {q:0}).q;
60:
}
61:
 
62:
function specify(encoding, spec) {
63:
  if (spec.encoding === '*' || spec.encoding === encoding) {
64:
    return spec;
65:
  }
66:
}
67:
 
68:
function preferredEncodings(accept, provided) {
69:
  accept = parseAcceptEncoding(accept || '');
70:
  if (provided) {
71:
    return provided.map(function(type) {
72:
      return [type, getEncodingPriority(type, accept)];
73:
    }).filter(function(pair) {
74:
      return pair[1] > 0;
75:
    }).sort(function(a, b) {
76:
      // revsort
77:
      return a[1] === b[1] ? 0 : a[1] > b[1] ? -1 : 1;
78:
    }).map(function(pair) {
79:
      return pair[0];
80:
    });
81:
  } else {
82:
    return accept.map(function(type) {
83:
      return type.encoding;
84:
    });
85:
  }
86:
}