You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
1.1 KiB

7 years ago
  1. module.exports = Negotiator;
  2. Negotiator.Negotiator = Negotiator;
  3. function Negotiator(request) {
  4. if (!(this instanceof Negotiator)) return new Negotiator(request);
  5. this.request = request;
  6. }
  7. var set = { charset: 'accept-charset',
  8. encoding: 'accept-encoding',
  9. language: 'accept-language',
  10. mediaType: 'accept' };
  11. function capitalize(string){
  12. return string.charAt(0).toUpperCase() + string.slice(1);
  13. }
  14. Object.keys(set).forEach(function (k) {
  15. var header = set[k],
  16. method = require('./'+k+'.js'),
  17. singular = k,
  18. plural = k + 's';
  19. Negotiator.prototype[plural] = function (available) {
  20. return method(this.request.headers[header], available);
  21. };
  22. Negotiator.prototype[singular] = function(available) {
  23. var set = this[plural](available);
  24. if (set) return set[0];
  25. };
  26. // Keep preferred* methods for legacy compatibility
  27. Negotiator.prototype['preferred'+capitalize(plural)] = Negotiator.prototype[plural];
  28. Negotiator.prototype['preferred'+capitalize(singular)] = Negotiator.prototype[singular];
  29. })