source: trip-planner-front/node_modules/negotiator/index.js@ b738035

Last change on this file since b738035 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.3 KB
RevLine 
[6a3a178]1/*!
2 * negotiator
3 * Copyright(c) 2012 Federico Romero
4 * Copyright(c) 2012-2014 Isaac Z. Schlueter
5 * Copyright(c) 2015 Douglas Christopher Wilson
6 * MIT Licensed
7 */
8
9'use strict';
10
11/**
12 * Cached loaded submodules.
13 * @private
14 */
15
16var modules = Object.create(null);
17
18/**
19 * Module exports.
20 * @public
21 */
22
23module.exports = Negotiator;
24module.exports.Negotiator = Negotiator;
25
26/**
27 * Create a Negotiator instance from a request.
28 * @param {object} request
29 * @public
30 */
31
32function Negotiator(request) {
33 if (!(this instanceof Negotiator)) {
34 return new Negotiator(request);
35 }
36
37 this.request = request;
38}
39
40Negotiator.prototype.charset = function charset(available) {
41 var set = this.charsets(available);
42 return set && set[0];
43};
44
45Negotiator.prototype.charsets = function charsets(available) {
46 var preferredCharsets = loadModule('charset').preferredCharsets;
47 return preferredCharsets(this.request.headers['accept-charset'], available);
48};
49
50Negotiator.prototype.encoding = function encoding(available) {
51 var set = this.encodings(available);
52 return set && set[0];
53};
54
55Negotiator.prototype.encodings = function encodings(available) {
56 var preferredEncodings = loadModule('encoding').preferredEncodings;
57 return preferredEncodings(this.request.headers['accept-encoding'], available);
58};
59
60Negotiator.prototype.language = function language(available) {
61 var set = this.languages(available);
62 return set && set[0];
63};
64
65Negotiator.prototype.languages = function languages(available) {
66 var preferredLanguages = loadModule('language').preferredLanguages;
67 return preferredLanguages(this.request.headers['accept-language'], available);
68};
69
70Negotiator.prototype.mediaType = function mediaType(available) {
71 var set = this.mediaTypes(available);
72 return set && set[0];
73};
74
75Negotiator.prototype.mediaTypes = function mediaTypes(available) {
76 var preferredMediaTypes = loadModule('mediaType').preferredMediaTypes;
77 return preferredMediaTypes(this.request.headers.accept, available);
78};
79
80// Backwards compatibility
81Negotiator.prototype.preferredCharset = Negotiator.prototype.charset;
82Negotiator.prototype.preferredCharsets = Negotiator.prototype.charsets;
83Negotiator.prototype.preferredEncoding = Negotiator.prototype.encoding;
84Negotiator.prototype.preferredEncodings = Negotiator.prototype.encodings;
85Negotiator.prototype.preferredLanguage = Negotiator.prototype.language;
86Negotiator.prototype.preferredLanguages = Negotiator.prototype.languages;
87Negotiator.prototype.preferredMediaType = Negotiator.prototype.mediaType;
88Negotiator.prototype.preferredMediaTypes = Negotiator.prototype.mediaTypes;
89
90/**
91 * Load the given module.
92 * @private
93 */
94
95function loadModule(moduleName) {
96 var module = modules[moduleName];
97
98 if (module !== undefined) {
99 return module;
100 }
101
102 // This uses a switch for static require analysis
103 switch (moduleName) {
104 case 'charset':
105 module = require('./lib/charset');
106 break;
107 case 'encoding':
108 module = require('./lib/encoding');
109 break;
110 case 'language':
111 module = require('./lib/language');
112 break;
113 case 'mediaType':
114 module = require('./lib/mediaType');
115 break;
116 default:
117 throw new Error('Cannot find module \'' + moduleName + '\'');
118 }
119
120 // Store to prevent invoking require()
121 modules[moduleName] = module;
122
123 return module;
124}
Note: See TracBrowser for help on using the repository browser.