~ubuntu-branches/ubuntu/vivid/node-negotiator/vivid

« back to all changes in this revision

Viewing changes to lib/language.js

  • Committer: Package Import Robot
  • Author(s): Jérémy Lal, Leo Iannacone, Jérémy Lal
  • Date: 2014-10-11 15:19:39 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20141011151939-9t2e4b46mnj9ub9o
Tags: 0.4.8-1
[ Leo Iannacone ]
* Imported Upstream version 0.4.8
* Add autopkgtest suite
* Bump Standard-Version 3.9.6
* debian/docs: update README.md filename

[ Jérémy Lal ]
* Move to pkg-javascript

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
preferredLanguages.preferredLanguages = preferredLanguages;
3
3
 
4
4
function parseAcceptLanguage(accept) {
5
 
  return accept.split(',').map(function(e) {
6
 
    return parseLanguage(e.trim());
 
5
  return accept.split(',').map(function(e, i) {
 
6
    return parseLanguage(e.trim(), i);
7
7
  }).filter(function(e) {
8
8
    return e;
9
9
  });
10
10
}
11
11
 
12
 
function parseLanguage(s) {
 
12
function parseLanguage(s, i) {
13
13
  var match = s.match(/^\s*(\S+?)(?:-(\S+?))?\s*(?:;(.*))?$/);
14
14
  if (!match) return null;
15
15
 
32
32
    prefix: prefix,
33
33
    suffix: suffix,
34
34
    q: q,
 
35
    i: i,
35
36
    full: full
36
37
  };
37
38
}
39
40
function getLanguagePriority(language, accepted) {
40
41
  return (accepted.map(function(a){
41
42
    return specify(language, a);
42
 
  }).filter(function(a){
43
 
    return a;
44
 
  }).sort(function(a, b){
45
 
    // revsort
46
 
    return a.s > b.s ? -1 : 1;
47
 
  })[0] || {q:0}).q;
 
43
  }).filter(Boolean).sort(function (a, b) {
 
44
    if(a.s == b.s) {
 
45
      return a.q > b.q ? -1 : 1;
 
46
    } else {
 
47
      return a.s > b.s ? -1 : 1;
 
48
    }
 
49
  })[0] || {s: 0, q: 0});
48
50
}
49
51
 
50
52
function specify(language, spec) {
51
53
  var p = parseLanguage(language)
 
54
  if (!p) return null;
52
55
  var s = 0;
53
 
  if(spec.full === p.full){
 
56
  if(spec.full.toLowerCase() === p.full.toLowerCase()){
54
57
    s |= 4;
55
 
  } else if (spec.prefix === p.full) {
 
58
  } else if (spec.prefix.toLowerCase() === p.full.toLowerCase()) {
56
59
    s |= 2;
57
 
  } else if (spec.full === p.prefix) {
 
60
  } else if (spec.full.toLowerCase() === p.prefix.toLowerCase()) {
58
61
    s |= 1;
59
62
  } else if (spec.full !== '*' ) {
60
63
    return null
67
70
};
68
71
 
69
72
function preferredLanguages(accept, provided) {
70
 
  accept = parseAcceptLanguage(accept || '');
 
73
  // RFC 2616 sec 14.4: no header = *
 
74
  accept = parseAcceptLanguage(accept === undefined ? '*' : accept || '');
71
75
  if (provided) {
72
76
 
73
77
    var ret = provided.map(function(type) {
74
78
      return [type, getLanguagePriority(type, accept)];
75
79
    }).filter(function(pair) {
76
 
      return pair[1] > 0;
 
80
      return pair[1].q > 0;
77
81
    }).sort(function(a, b) {
78
 
      // revsort
79
 
      return a[1] === b[1] ? 0 : a[1] > b[1] ? -1 : 1;
 
82
      var pa = a[1];
 
83
      var pb = b[1];
 
84
      return (pb.q - pa.q) || (pb.s - pa.s) || (pa.i - pb.i);
80
85
    }).map(function(pair) {
81
86
      return pair[0];
82
87
    });
85
90
  } else {
86
91
    return accept.sort(function (a, b) {
87
92
      // revsort
88
 
      return a.q < b.q ? 1 : -1;
 
93
      return (b.q - a.q) || (a.i - b.i);
89
94
    }).filter(function(type) {
90
95
      return type.q > 0;
91
96
    }).map(function(type) {