-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter_values.ts
31 lines (30 loc) · 985 Bytes
/
filter_values.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
29
30
31
// Copyright 2023-latest the httpland authors. All rights reserved. MIT license.
// This module is browser compatible.
/** Returns a new `Headers` with all entries of the given headers except the ones
* that have a value(field value) that does not match the given predicate.
*
* @example
* ```ts
* import { filterHeadersValues } from "https://deno.land/x/headers_utils@$VERSION/filter_values.ts";
* import { assert, assertFalse } from "https://deno.land/std/testing/asserts.ts";
*
* declare const isIMFDate: (input: string) => boolean;
*
* const headers = filterHeadersValues(
* new Headers({
* "date": "<date>",
* "content-type": "<content-type>",
* }),
* isIMFDate,
* );
*
* assert(headers.has("date"));
* assertFalse(headers.has("content-type"));
* ```
*/
export function filterHeadersValues(
headers: Headers,
predicate: (value: string) => boolean,
): Headers {
return new Headers([...headers].filter(([_, value]) => predicate(value)));
}