UI for Zipcoin Blue

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. if (!clobber && has) this.dict[has] = this.dict[has] + ',' + value
  13. else this.dict[has || name] = value
  14. return has
  15. }
  16. }
  17. Caseless.prototype.has = function (name) {
  18. var keys = Object.keys(this.dict)
  19. , name = name.toLowerCase()
  20. ;
  21. for (var i=0;i<keys.length;i++) {
  22. if (keys[i].toLowerCase() === name) return keys[i]
  23. }
  24. return false
  25. }
  26. Caseless.prototype.get = function (name) {
  27. name = name.toLowerCase()
  28. var result, _key
  29. var headers = this.dict
  30. Object.keys(headers).forEach(function (key) {
  31. _key = key.toLowerCase()
  32. if (name === _key) result = headers[key]
  33. })
  34. return result
  35. }
  36. Caseless.prototype.swap = function (name) {
  37. var has = this.has(name)
  38. if (!has) throw new Error('There is no header than matches "'+name+'"')
  39. this.dict[name] = this.dict[has]
  40. delete this.dict[has]
  41. }
  42. Caseless.prototype.del = function (name) {
  43. var has = this.has(name)
  44. return delete this.dict[has || name]
  45. }
  46. module.exports = function (dict) {return new Caseless(dict)}
  47. module.exports.httpify = function (resp, headers) {
  48. var c = new Caseless(headers)
  49. resp.setHeader = function (key, value, clobber) {
  50. if (typeof value === 'undefined') return
  51. return c.set(key, value, clobber)
  52. }
  53. resp.hasHeader = function (key) {
  54. return c.has(key)
  55. }
  56. resp.getHeader = function (key) {
  57. return c.get(key)
  58. }
  59. resp.removeHeader = function (key) {
  60. return c.del(key)
  61. }
  62. resp.headers = c.dict
  63. return c
  64. }