Skip to content

Commit 84c402c

Browse files
committed
feat(header): add filtering by header field name function
1 parent 44c719f commit 84c402c

File tree

4 files changed

+77
-1
lines changed

4 files changed

+77
-1
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,28 @@ assertEquals(isSingletonField("Origin"), true);
139139
assertEquals(isSingletonField("Vary"), false);
140140
```
141141

142+
## filterKeys
143+
144+
Returns a new {@link Headers} with all entries of the given headers except the
145+
ones that have a key(header name or field name) that does not match the given
146+
predicate.
147+
148+
```ts
149+
import { filterKeys } from "https://deno.land/x/http_utils@$VERSION/mod.ts";
150+
import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";
151+
152+
const headers = filterKeys(
153+
new Headers({
154+
"date": "<date>",
155+
"content-type": "<content-type>",
156+
}),
157+
(key) => key.startsWith("content"),
158+
);
159+
160+
assert(headers.has("content-type"));
161+
assert(!headers.has("date"));
162+
```
163+
142164
## MessageMetadataHeader
143165

144166
HTTP Message Metadata header fields.

header.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,32 @@ export function parseFieldValue(fieldValue: string): string[] {
173173
[]).map((value) => value.trim()).filter(Boolean);
174174
}
175175

176+
/** Returns a new {@link Headers} with all entries of the given headers except the ones that have a key(header name or field name) that does not match the given predicate.
177+
*
178+
* @example
179+
* ```ts
180+
* import { filterKeys } from "https://deno.land/x/http_utils@$VERSION/mod.ts";
181+
* import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";
182+
*
183+
* const headers = filterKeys(
184+
* new Headers({
185+
* "date": "<date>",
186+
* "content-type": "<content-type>",
187+
* }),
188+
* (key) => key.startsWith("content"),
189+
* );
190+
*
191+
* assert(headers.has("content-type"));
192+
* assert(!headers.has("date"));
193+
* ```
194+
*/
195+
export function filterKeys(
196+
headers: Headers,
197+
predicate: (key: string) => boolean,
198+
): Headers {
199+
return new Headers([...headers].filter(([key]) => predicate(key)));
200+
}
201+
176202
/** HTTP Message Metadata header fields.
177203
* @see [RFC 9110, 6.6. Message Metadata](https://www.rfc-editor.org/rfc/rfc9110.html#section-6.6)
178204
*

header_test.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import {
22
equalsHeaders,
3+
filterKeys,
34
isSingletonField,
45
mergeHeaders,
56
parseFieldValue,
67
} from "./header.ts";
7-
import { describe, expect, Fn, it } from "./dev_deps.ts";
8+
import { assert, describe, expect, Fn, it } from "./dev_deps.ts";
89

910
describe("equalsHeaders", () => {
1011
it("should pass", () => {
@@ -212,3 +213,29 @@ Deno.test("parseFieldValue should pass", () => {
212213
expect(parseFieldValue(actual)).toEqual(expected);
213214
});
214215
});
216+
217+
describe("filterKeys", () => {
218+
it("should return new headers filtered by predicate", () => {
219+
const table: [Headers, (key: string) => boolean, Headers][] = [
220+
[
221+
new Headers({ "x-test": "test", "date": "xxx" }),
222+
() => true,
223+
new Headers({ "x-test": "test", "date": "xxx" }),
224+
],
225+
[
226+
new Headers({ "x-test": "test", "date": "xxx" }),
227+
() => false,
228+
new Headers(),
229+
],
230+
[
231+
new Headers({ "x-test": "test", "date": "xxx" }),
232+
(key) => key === "date",
233+
new Headers({ date: "xxx" }),
234+
],
235+
];
236+
237+
table.forEach(([headers, predicate, expected]) => {
238+
assert(equalsHeaders(filterKeys(headers, predicate), expected));
239+
});
240+
});
241+
});

mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export {
88
ConditionalHeader,
99
ContentNegotiationHeader,
1010
equalsHeaders,
11+
filterKeys,
1112
type HttpFieldName,
1213
isSingletonField,
1314
mergeHeaders,

0 commit comments

Comments
 (0)