diff --git a/src/object.ts b/src/object.ts index 59e5e48..02ad09c 100644 --- a/src/object.ts +++ b/src/object.ts @@ -46,5 +46,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 65b00c2..a3f9c72 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -120,6 +120,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: [ @@ -136,6 +137,12 @@ const genObjectFromRawTests = [ ' foo: "bar"', " }", " },", + " \"obj 3\": {", + " literal: () => \"test\",", + " anotherLiteral: function () {", + " return \"test\";", + " }", + " },", " arr: [", " 1,", " 2,",