From 9217fa80f49d62c0b40cc9c5d5d6fc20bc6966b3 Mon Sep 17 00:00:00 2001 From: Dan Hudlow Date: Wed, 20 Mar 2024 06:13:45 -0500 Subject: [PATCH] fix(yaml): speciously restrictive type for stringify() --- yaml/stringify.ts | 4 ++-- yaml/stringify_test.ts | 44 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/yaml/stringify.ts b/yaml/stringify.ts index 679bb40748684..41767830ebce5 100644 --- a/yaml/stringify.ts +++ b/yaml/stringify.ts @@ -15,8 +15,8 @@ export type DumpOptions = DumperStateOptions; * You can disable exceptions by setting the skipInvalid option to true. */ export function stringify( - obj: Record, + data: unknown, options?: DumpOptions, ): string { - return dump(obj, options); + return dump(data, options); } diff --git a/yaml/stringify_test.ts b/yaml/stringify_test.ts index ce75dd7495c39..fc9e72c402c82 100644 --- a/yaml/stringify_test.ts +++ b/yaml/stringify_test.ts @@ -45,6 +45,50 @@ binary: ! SGVsbG8= }, }); +Deno.test({ + name: "arrays can be stringified directly", + fn() { + const array = [1, 2, 3] + + const expected = "- 1\n- 2\n- 3\n"; + + assertEquals(stringify(array), expected); + }, +}); + +Deno.test({ + name: "strings can be stringified directly", + fn() { + const string = 'Hello world'; + + const expected = 'Hello world\n'; + + assertEquals(stringify(string), expected); + }, +}); + +Deno.test({ + name: "numbers can be stringified directly", + fn() { + const number = 1.01; + + const expected = "1.01\n"; + + assertEquals(stringify(number), expected); + }, +}); + +Deno.test({ + name: "booleans can be stringified directly", + fn() { + const boolean = true; + + const expected = "true\n"; + + assertEquals(stringify(boolean), expected); + }, +}); + Deno.test({ name: "stringify() throws with `!!js/*` yaml types with default schemas", fn() {