cerulea/lexicon

define atproto schemas in TypeScript

git clone https://git.t4t.associates/cerulea/lexicon

Charlotte Sominitial commit1945441

main
1007 B30 linesraw
1export function assert(value: unknown, message = "Assertion failed"): asserts value {
2  if (!value) throw new Error(message);
3}
4
5export function equal(actual: unknown, expected: unknown): void {
6  const canonical = (value: unknown): string | undefined =>
7    JSON.stringify(
8      value,
9      (_, item) =>
10        item !== null && typeof item === "object" && !Array.isArray(item)
11          ? Object.fromEntries(Object.entries(item).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0))
12          : item,
13    );
14  assert(
15    canonical(actual) === canonical(expected),
16    `Expected:\n${Deno.inspect(expected, { depth: Infinity })}\nReceived:\n${
17      Deno.inspect(actual, { depth: Infinity })
18    }`,
19  );
20}
21
22export function throws(fn: () => unknown, message: string): void {
23  try {
24    fn();
25  } catch (error) {
26    assert(error instanceof Error && error.message.includes(message), `Unexpected error: ${error}`);
27    return;
28  }
29  throw new Error(`Expected an error containing ${JSON.stringify(message)}`);
30}