Skip to main content
Deno 2 is finally here 🎉️
Learn more

headers-utils

deno land GitHub release (latest by date) codecov GitHub

test NPM

Headers utility collection.

equalsHeaders

Check two Headers field name and field value equality.

import { equalsHeaders } from "https://deno.land/x/headers_utils@$VERSION/equal.ts";
import { assert, assertFalse } from "https://deno.land/std/testing/asserts.ts";

assert(equalsHeaders(new Headers({ a: "b" }), new Headers({ a: "b" })));
assertFalse(equalsHeaders(new Headers({ a: "b" }), new Headers({ c: "d" })));

filterHeadersEntries

Returns a new Headers with all entries of the given headers except the ones that do not match the given predicate.

import { filterHeadersEntries } from "https://deno.land/x/headers_utils@$VERSION/filter_entries.ts";
import { assert, assertFalse } from "https://deno.land/std/testing/asserts.ts";

declare const isIMFDate: (input: string) => boolean;

const headers = filterHeadersEntries(
  new Headers({
    "date": "<date>",
    "content-type": "<content-type>",
  }),
  ([key, value]) => isIMFDate(value),
);

assert(headers.has("date"));
assert(headers.has("content-type"));

filterHeadersKeys

Returns a new Headers with all entries of the given headers except the ones that have a key(field name) that does not match the given predicate.

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"));

filterHeadersValues

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.

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"));

isHeaders

Whether the input is Headers or not.

import { isHeaders } from "https://deno.land/x/headers_utils@$VERSION/is.ts";
import { assert, assertFalse } from "https://deno.land/std/testing/asserts.ts";

assert(isHeaders(new Headers()));
assertFalse(isHeaders({}));

stringifyHeaders

Serialize Headers into string.

import { stringifyHeaders } from "https://deno.land/x/headers_utils@$VERSION/stringify.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

assertEquals(stringifyHeaders(new Headers()), "");
assertEquals(
  stringifyHeaders(
    new Headers({
      "content-type": "text/plain",
      "date": "Wed, 21 Oct 2015 07:28:00 GMT",
    }),
  ),
  `content-type: text/plain
date: Wed, 21 Oct 2015 07:28:00 GMT`,
);

API

All APIs can be found in the deno doc.

License

Copyright © 2023-present httpland.

Released under the MIT license