-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringify.ts
37 lines (31 loc) · 1.23 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
// Copyright 2023-latest the httpland authors. All rights reserved. MIT license.
// This module is browser compatible.
import { quoted, sentence, weakPrefix } from "./utils.ts";
import { assertEtagcFormat } from "./validate.ts";
import { Msg } from "./constants.ts";
import type { ETag, ETagFormat } from "./types.ts";
/** Serialize {@link ETag} into string.
*
* @example
* ```ts
* import { stringifyETag } from "https://deno.land/x/etag_parser@$VERSION/stringify.ts";
* import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
*
* assertEquals(stringifyETag({ weak: true, tag: "123456789" }), `W/"123456789"`);
* assertEquals(stringifyETag({ weak: false, tag: "123456789" }), `"123456789"`);
* ```
*
* @throws {TypeError} If the {@link ETag.tag} is not [`<etagc>`](https://www.rfc-editor.org/rfc/rfc9110#field.etag) format.
*/
export function stringifyETag(etag: ETag): ETagFormat {
const { tag, weak } = etag;
const message = sentence(Msg.InvalidEtagc, quoted(tag));
assertEtagcFormat(tag, message);
const opaqueTag = quoted(tag);
const etagFormat = weak ? weakPrefix(opaqueTag) : opaqueTag;
return etagFormat;
}
/**
* @deprecated Rename to {@link stringifyETag}.
*/
export const stringify = stringifyETag;