-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringify.ts
41 lines (38 loc) · 1.04 KB
/
stringify.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
32
33
34
35
36
37
38
39
40
41
// Copyright 2023-latest the httpland authors. All rights reserved. MIT license.
// This module is browser compatible.
/** Special character. */
const enum Char {
LF = "\n",
Colon = ":",
Space = " ",
}
/** Serialize {@link Headers} into string.
*
* @example
* ```ts
* 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`,
* );
* ```
*/
export function stringifyHeaders(headers: Headers): string {
return [...headers.entries()]
.map(stringifyFieldLine)
.join(Char.LF);
}
function stringifyFieldLine(
entry: readonly [fieldName: unknown, fieldValue: unknown],
): string {
return entry[0] + Char.Colon + Char.Space + entry[1];
}