├── package.json ├── README.md ├── index.js ├── test.js └── LICENSE /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "caseless", 3 | "version": "0.12.1", 4 | "description": "Caseless object set/get/has/del, very useful when working with HTTP headers.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "node test.js" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/mikeal/caseless" 12 | }, 13 | "keywords": [ 14 | "headers", 15 | "http", 16 | "caseless" 17 | ], 18 | "test": "node test.js", 19 | "author": "Mikeal Rogers ", 20 | "license": "Apache-2.0", 21 | "bugs": { 22 | "url": "https://github.com/mikeal/caseless/issues" 23 | }, 24 | "devDependencies": { 25 | "tape": "^2.10.2" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Caseless -- wrap an object to set and get property with caseless semantics but also preserve caseing. 2 | 3 | This library is incredibly useful when working with HTTP headers. It allows you to get/set/check/delete headers in a caseless manner while also preserving the headers' case when they are first set. 4 | 5 | ## Usage 6 | 7 | ```javascript 8 | var headers = {} 9 | , c = caseless(headers) 10 | ; 11 | c.set('a-Header', 'asdf') 12 | c.get('a-header') === 'asdf' 13 | ``` 14 | 15 | ## has(key) 16 | 17 | Has takes a name and if it finds a matching header will return that header name with the preserved caseing it was set with. 18 | 19 | ```javascript 20 | c.has('a-header') === 'a-Header' 21 | ``` 22 | 23 | ## set(key, value[, clobber=true]) 24 | 25 | Set is fairly straight forward except that if the header exists and clobber is disabled it will add `','+value` to the existing header. 26 | 27 | ```javascript 28 | c.set('a-Header', 'fdas') 29 | c.set('a-HEADER', 'more', false) 30 | c.get('a-header') === 'fdsa,more' 31 | ``` 32 | 33 | ## swap(key) 34 | 35 | Swaps the casing of a header with the new one that is passed in. 36 | 37 | ```javascript 38 | var headers = {} 39 | , c = caseless(headers) 40 | ; 41 | c.set('a-Header', 'fdas') 42 | c.swap('a-HEADER') 43 | c.has('a-header') === 'a-HEADER' 44 | headers === {'a-HEADER': 'fdas'} 45 | ``` 46 | 47 | ## del(key) 48 | 49 | Deletes a key and, if there's many instances of the key with multiple cases, all of them. 50 | 51 | ```javascript 52 | 53 | var headers = { 54 | 'a-Header': true, 55 | 'content-length': 312, 56 | 'Content-Length': 312 57 | } 58 | var c = caseless(headers); 59 | 60 | c.del('Content-length'); 61 | headers === { 62 | 'a-Header': true 63 | }; 64 | 65 | ``` 66 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 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