|
| 1 | +# headers-utils |
| 2 | + |
| 3 | +[](https://deno.land/x/headers_utils) |
| 4 | +[](https://github.com/httpland/headers-utils/releases) |
| 5 | +[](https://codecov.io/gh/httpland/headers-utils) |
| 6 | +[](https://github.com/httpland/headers-utils/blob/main/LICENSE) |
| 7 | + |
| 8 | +[](https://github.com/httpland/headers-utils/actions/workflows/test.yaml) |
| 9 | +[](https://nodei.co/npm/@httpland/headers-utils/) |
| 10 | + |
| 11 | +Headers utility collection. |
| 12 | + |
| 13 | +## equalHeaders |
| 14 | + |
| 15 | +Check two `Headers` field name and field value equality. |
| 16 | + |
| 17 | +```ts |
| 18 | +import { equalsHeaders } from "https://deno.land/x/headers_utils@$VERSION/equal.ts"; |
| 19 | +import { assert, assertFalse } from "https://deno.land/std/testing/asserts.ts"; |
| 20 | + |
| 21 | +assert(equalsHeaders(new Headers({ a: "b" }), new Headers({ a: "b" }))); |
| 22 | +assertFalse(equalsHeaders(new Headers({ a: "b" }), new Headers({ c: "d" }))); |
| 23 | +``` |
| 24 | + |
| 25 | +## filterHeadersEntries |
| 26 | + |
| 27 | +Returns a new `Headers` with all entries of the given headers except the ones |
| 28 | +that do not match the given predicate. |
| 29 | + |
| 30 | +```ts |
| 31 | +import { filterHeadersEntries } from "https://deno.land/x/headers_utils@$VERSION/filter_entries.ts"; |
| 32 | +import { assert, assertFalse } from "https://deno.land/std/testing/asserts.ts"; |
| 33 | + |
| 34 | +declare const isIMFDate: (input: string) => boolean; |
| 35 | + |
| 36 | +const headers = filterHeadersEntries( |
| 37 | + new Headers({ |
| 38 | + "date": "<date>", |
| 39 | + "content-type": "<content-type>", |
| 40 | + }), |
| 41 | + ([key, value]) => isIMFDate(value), |
| 42 | +); |
| 43 | + |
| 44 | +assert(headers.has("date")); |
| 45 | +assert(headers.has("content-type")); |
| 46 | +``` |
| 47 | + |
| 48 | +## filterHeadersKeys |
| 49 | + |
| 50 | +Returns a new `Headers` with all entries of the given headers except the ones |
| 51 | +that have a key(header name or field name) that does not match the given |
| 52 | +predicate. |
| 53 | + |
| 54 | +```ts |
| 55 | +import { filterHeadersKeys } from "https://deno.land/x/headers_utils@$VERSION/filter_keys.ts"; |
| 56 | +import { assert, assertFalse } from "https://deno.land/std/testing/asserts.ts"; |
| 57 | + |
| 58 | +const headers = filterHeadersKeys( |
| 59 | + new Headers({ |
| 60 | + "date": "<date>", |
| 61 | + "content-type": "<content-type>", |
| 62 | + }), |
| 63 | + (key) => key.startsWith("content"), |
| 64 | +); |
| 65 | + |
| 66 | +assert(headers.has("content-type")); |
| 67 | +assertFalse(headers.has("date")); |
| 68 | +``` |
| 69 | + |
| 70 | +## filterHeadersValues |
| 71 | + |
| 72 | +Returns a new `Headers` with all entries of the given headers except the ones |
| 73 | +that have a value(field value) that does not match the given predicate. |
| 74 | + |
| 75 | +```ts |
| 76 | +import { filterHeadersValues } from "https://deno.land/x/headers_utils@$VERSION/filter_values.ts"; |
| 77 | +import { assert, assertFalse } from "https://deno.land/std/testing/asserts.ts"; |
| 78 | + |
| 79 | +declare const isIMFDate: (input: string) => boolean; |
| 80 | + |
| 81 | +const headers = filterHeadersValues( |
| 82 | + new Headers({ |
| 83 | + "date": "<date>", |
| 84 | + "content-type": "<content-type>", |
| 85 | + }), |
| 86 | + isIMFDate, |
| 87 | +); |
| 88 | + |
| 89 | +assert(headers.has("date")); |
| 90 | +assertFalse(headers.has("content-type")); |
| 91 | +``` |
| 92 | + |
| 93 | +## isHeaders |
| 94 | + |
| 95 | +Whether the input is `Headers` or not. |
| 96 | + |
| 97 | +```ts |
| 98 | +import { isHeaders } from "https://deno.land/x/headers_utils@$VERSION/is.ts"; |
| 99 | +import { assert, assertFalse } from "https://deno.land/std/testing/asserts.ts"; |
| 100 | + |
| 101 | +assert(isHeaders(new Headers())); |
| 102 | +assertFalse(isHeaders({})); |
| 103 | +``` |
| 104 | + |
| 105 | +## stringifyHeaders |
| 106 | + |
| 107 | +Serialize `Headers` into string. |
| 108 | + |
| 109 | +```ts |
| 110 | +import { stringifyHeaders } from "https://deno.land/x/headers_utils@$VERSION/stringify.ts"; |
| 111 | +import { assertEquals } from "https://deno.land/std/testing/asserts.ts"; |
| 112 | + |
| 113 | +assertEquals(stringifyHeaders(new Headers()), ""); |
| 114 | +assertEquals( |
| 115 | + stringifyHeaders( |
| 116 | + new Headers({ |
| 117 | + "content-type": "text/plain", |
| 118 | + "date": "Wed, 21 Oct 2015 07:28:00 GMT", |
| 119 | + }), |
| 120 | + ), |
| 121 | + `content-type: text/plain |
| 122 | +date: Wed, 21 Oct 2015 07:28:00 GMT`, |
| 123 | +); |
| 124 | +``` |
| 125 | + |
| 126 | +## License |
| 127 | + |
| 128 | +Copyright © 2023-present [httpland](https://github.com/httpland). |
| 129 | + |
| 130 | +Released under the [MIT](./LICENSE) license |
0 commit comments