Skip to content

Commit 9807910

Browse files
plauclairmikeal
authored andcommitted
Make caseless.del() work ok on duplicates, update readme and add tests (#29)
1 parent a3edd69 commit 9807910

4 files changed

Lines changed: 50 additions & 4 deletions

File tree

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Caseless -- wrap an object to set and get property with caseless semantics but also preserve caseing.
22

3-
This library is incredibly useful when working with HTTP headers. It allows you to get/set/check for headers in a caseless manner while also preserving the caseing of headers the first time they are set.
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.
44

55
## Usage
66

@@ -43,3 +43,23 @@ c.swap('a-HEADER')
4343
c.has('a-header') === 'a-HEADER'
4444
headers === {'a-HEADER': 'fdas'}
4545
```
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+
```

index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,17 @@ Caseless.prototype.swap = function (name) {
4242
delete this.dict[has]
4343
}
4444
Caseless.prototype.del = function (name) {
45-
var has = this.has(name)
46-
return delete this.dict[has || name]
45+
name = String(name).toLowerCase()
46+
var deleted = false
47+
var changed = 0
48+
var dict = this.dict
49+
Object.keys(this.dict).forEach(function(key) {
50+
if (name === String(key).toLowerCase()) {
51+
deleted = delete dict[key]
52+
changed += 1
53+
}
54+
})
55+
return changed === 0 ? true : deleted
4756
}
4857

4958
module.exports = function (dict) {return new Caseless(dict)}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "caseless",
33
"version": "0.12.1",
4-
"description": "Caseless object set/get/has, very useful when working with HTTP headers.",
4+
"description": "Caseless object set/get/has/del, very useful when working with HTTP headers.",
55
"main": "index.js",
66
"scripts": {
77
"test": "node test.js"

test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,20 @@ tape('swap', function (t) {
6565
c.swap('content-type')
6666
})
6767
})
68+
69+
tape('del', function (t) {
70+
var headers = {
71+
'X-Random-Header': 'X',
72+
'Content-Type': 'A',
73+
'content-type': 'B'
74+
}
75+
var c = caseless(headers)
76+
t.plan(4)
77+
// Both headers should still be there
78+
t.ok(c.has('X-Random-Header'))
79+
t.ok(c.has('Content-Type'))
80+
// Del should delete them all
81+
c.del('Content-type')
82+
t.ok(!c.has('Content-type'))
83+
t.deepEqual(headers, {'X-Random-Header': 'X'})
84+
});

0 commit comments

Comments
 (0)