': 'other', bar: 'with "double" quotes' };
expect(prettyPrint(json)).toBe('{
"foo": "hello <span> world",
"the<key>": "other",
"bar": "with "double" quotes"
}
');
});
+
+test('unexpectedTypes', () => {
+ const json = { foo: 'test', bar: () => { const test = 'test'; } };
+ expect(prettyPrint(json)).toBe('{
"foo": "test",
"bar": function
}
');
+});
diff --git a/src/prettyPrint.ts b/src/prettyPrint.ts
index d5e75c5..87e5223 100755
--- a/src/prettyPrint.ts
+++ b/src/prettyPrint.ts
@@ -169,7 +169,11 @@ const printObject = (object: object, out: PrintWriter, idt: number, selection: o
out.print('undefined');
break;
default:
- throw new Error(`Don''t know what to do with ${typeof value}`);
+ if (options.silent) {
+ out.print(typeof value);
+ } else {
+ throw new Error(`Don''t know what to do with ${typeof value}`);
+ }
}
if (i < keys.length - 1) {
out.print(',');
@@ -214,7 +218,11 @@ const printArray = (array: {}[], out: PrintWriter, idt: number, selection: objec
out.print('undefined');
break;
default:
- throw new Error(`Don''t know what to do with ${typeof value}`);
+ if (options.silent) {
+ out.print(typeof value);
+ } else {
+ throw new Error(`Don''t know what to do with ${typeof value}`);
+ }
}
if (i < array.length - 1) {
out.print(',');
@@ -230,7 +238,7 @@ const printArray = (array: {}[], out: PrintWriter, idt: number, selection: objec
export interface Options {
indent?: string;
-
+ silent?: boolean;
}
const prettyPrint = (object: object, selection?: object, options?: Options): string => {