"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/caseless/index.js" (7 Feb 2017, 1714 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 function Caseless (dict) {
    2   this.dict = dict || {}
    3 }
    4 Caseless.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 }
   18 Caseless.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 }
   27 Caseless.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 }
   37 Caseless.prototype.swap = function (name) {
   38   var has = this.has(name)
   39   if (!has) throw new Error('There is no header than matches "'+name+'"')
   40   this.dict[name] = this.dict[has]
   41   delete this.dict[has]
   42 }
   43 Caseless.prototype.del = function (name) {
   44   var has = this.has(name)
   45   return delete this.dict[has || name]
   46 }
   47 
   48 module.exports = function (dict) {return new Caseless(dict)}
   49 module.exports.httpify = function (resp, headers) {
   50   var c = new Caseless(headers)
   51   resp.setHeader = function (key, value, clobber) {
   52     return c.set(key, value, clobber)
   53   }
   54   resp.hasHeader = function (key) {
   55     return c.has(key)
   56   }
   57   resp.getHeader = function (key) {
   58     return c.get(key)
   59   }
   60   resp.removeHeader = function (key) {
   61     return c.del(key)
   62   }
   63   resp.headers = c.dict
   64   return c
   65 }