From 5e74e89652c107b9596370dd65656ed215a17bb3 Mon Sep 17 00:00:00 2001 From: Inesh Bose <2504266b@student.gla.ac.uk> Date: Fri, 10 Feb 2023 14:52:39 +0000 Subject: [PATCH] fix: add functions to genRawValue --- src/object.ts | 3 +++ test/index.test.ts | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/src/object.ts b/src/object.ts index c47458f..9970400 100644 --- a/src/object.ts +++ b/src/object.ts @@ -29,5 +29,8 @@ function genRawValue (value: unknown, indent = ""): string { if (value && typeof value === "object") { return genObjectFromRaw(value, indent); } + if (value && typeof value === "function") { + return value.toString().replace(new RegExp(`^${value.name}`), "function "); + } return value.toString(); } diff --git a/test/index.test.ts b/test/index.test.ts index b87a0a7..30173ad 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -85,6 +85,7 @@ const genObjectFromRawTests = [ 3: "true", "obj 1": "{ literal: () => \"test\" }", "obj 2": { nested: { foo: "\"bar\"" } }, + "obj 3": { literal: () => "test", anotherLiteral () { return "test"; } }, arr: ["1", "2", "3"] }, code: [ @@ -101,6 +102,12 @@ const genObjectFromRawTests = [ " foo: \"bar\"", " }", " },", + " \"obj 3\": {", + " literal: () => \"test\",", + " anotherLiteral: function () {", + " return \"test\";", + " }", + " },", " arr: [", " 1,", " 2,",