-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter_keys.ts
28 lines (27 loc) · 934 Bytes
/
filter_keys.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Copyright 2023-latest the httpland authors. All rights reserved. MIT license.
// This module is browser compatible.
/** Returns a new {@link Headers} with all entries of the given headers except the ones that have a key(field name) that does not match the given predicate.
*
* @example
* ```ts
* import { filterHeadersKeys } from "https://deno.land/x/headers_utils@$VERSION/filter_keys.ts";
* import { assert, assertFalse } from "https://deno.land/std/testing/asserts.ts";
*
* const headers = filterHeadersKeys(
* new Headers({
* "date": "<date>",
* "content-type": "<content-type>",
* }),
* (key) => key.startsWith("content"),
* );
*
* assert(headers.has("content-type"));
* assertFalse(headers.has("date"));
* ```
*/
export function filterHeadersKeys(
headers: Headers,
predicate: (key: string) => boolean,
): Headers {
return new Headers([...headers].filter(([key]) => predicate(key)));
}