Skip to content

Commit

Permalink
feat(etag): add etag serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Mar 20, 2023
1 parent 6c18914 commit 7ac8005
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
14 changes: 14 additions & 0 deletions etag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ETag } from "./types.ts";
import { quote, weakPrefix } from "./utils.ts";

export type WeakETagFormat = `W/"${string}"`;
export type StrongETagFormat = `"${string}"`;
export type ETagFormat = StrongETagFormat | WeakETagFormat;

/** Serialize {@link ETag} into string. */
export function stringify(etag: ETag): ETagFormat {
const opaqueTag = quote(etag.tag);
const etagFormat = etag.weak ? weakPrefix(opaqueTag) : opaqueTag;

return etagFormat;
}
29 changes: 29 additions & 0 deletions etag_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { stringify } from "./etag.ts";
import { ETag } from "./types.ts";
import { assertEquals, describe, it } from "./_dev_deps.ts";

describe("stringify", () => {
it("return weak etag format", () => {
const table: [ETag, string][] = [
[{ weak: true, tag: "" }, `W/""`],
[{ weak: true, tag: "abc" }, `W/"abc"`],
[{ weak: true, tag: `"abc"` }, `W/""abc""`],
];

table.forEach(([etag, expected]) => {
assertEquals(stringify(etag), expected);
});
});

it("return strong etag format", () => {
const table: [ETag, string][] = [
[{ weak: false, tag: "" }, `""`],
[{ weak: false, tag: "abc" }, `"abc"`],
[{ weak: false, tag: `"abc"` }, `""abc""`],
];

table.forEach(([etag, expected]) => {
assertEquals(stringify(etag), expected);
});
});
});

0 comments on commit 7ac8005

Please sign in to comment.