source: trip-planner-front/node_modules/caseless/index.js

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

initial commit

  • Property mode set to 100644
File size: 1.7 KB
Line 
1function Caseless (dict) {
2 this.dict = dict || {}
3}
4Caseless.prototype.set = function (name, value, clobber) {
5 if (typeof name === 'object') {
6 for (var i in name) {
7 this.set(i, name[i], value)
8 }
9 } else {
10 if (typeof clobber === 'undefined') clobber = true
11 var has = this.has(name)
12
13 if (!clobber && has) this.dict[has] = this.dict[has] + ',' + value
14 else this.dict[has || name] = value
15 return has
16 }
17}
18Caseless.prototype.has = function (name) {
19 var keys = Object.keys(this.dict)
20 , name = name.toLowerCase()
21 ;
22 for (var i=0;i<keys.length;i++) {
23 if (keys[i].toLowerCase() === name) return keys[i]
24 }
25 return false
26}
27Caseless.prototype.get = function (name) {
28 name = name.toLowerCase()
29 var result, _key
30 var headers = this.dict
31 Object.keys(headers).forEach(function (key) {
32 _key = key.toLowerCase()
33 if (name === _key) result = headers[key]
34 })
35 return result
36}
37Caseless.prototype.swap = function (name) {
38 var has = this.has(name)
39 if (has === name) return
40 if (!has) throw new Error('There is no header than matches "'+name+'"')
41 this.dict[name] = this.dict[has]
42 delete this.dict[has]
43}
44Caseless.prototype.del = function (name) {
45 var has = this.has(name)
46 return delete this.dict[has || name]
47}
48
49module.exports = function (dict) {return new Caseless(dict)}
50module.exports.httpify = function (resp, headers) {
51 var c = new Caseless(headers)
52 resp.setHeader = function (key, value, clobber) {
53 if (typeof value === 'undefined') return
54 return c.set(key, value, clobber)
55 }
56 resp.hasHeader = function (key) {
57 return c.has(key)
58 }
59 resp.getHeader = function (key) {
60 return c.get(key)
61 }
62 resp.removeHeader = function (key) {
63 return c.del(key)
64 }
65 resp.headers = c.dict
66 return c
67}
Note: See TracBrowser for help on using the repository browser.