From 6d97ba0a6dab589ce9f83e913b1dbaeac21a8f76 Mon Sep 17 00:00:00 2001 From: Bert Verhelst Date: Mon, 12 Nov 2018 13:34:12 +0100 Subject: [PATCH 1/2] Add silent option for unknown types --- .gitignore | 9 ++++++++- README.md | 12 ++++++++++++ __tests__/prettyPrint.spec.ts | 5 +++++ src/prettyPrint.ts | 8 ++++++-- 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index be735f3..3a7b841 100755 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,11 @@ dist lib lib-esm stats -_bundles \ No newline at end of file +_bundles +__tests__/*.d.ts +__tests__/*.js +__tests__/*.js.map +src/*.d.ts +src/*.js +src/*.js.map +build diff --git a/README.md b/README.md index e98ddf3..288ffae 100755 --- a/README.md +++ b/README.md @@ -35,6 +35,18 @@ With [a nice CSS](https://github.com/amelki/json-pretty-html/blob/master/style.c Result +## Options + +* indent: String to indent with. Defaults to 2 html spaces. +* silent: Suppresses exceptions for unknown types. For instance function. Defaults to false. When set to true the type will be outputted instead. + +```js +var html = prettyHtml(json, json.dimensions, { + indent: '  ', + silent: false +}); +``` + ## CSS You can use the default ['darcula' like stylesheet](https://github.com/amelki/json-pretty-html/blob/master/style.css). diff --git a/__tests__/prettyPrint.spec.ts b/__tests__/prettyPrint.spec.ts index 874b746..d9f1f3c 100755 --- a/__tests__/prettyPrint.spec.ts +++ b/__tests__/prettyPrint.spec.ts @@ -59,3 +59,8 @@ test('escapedHtml', () => { const json = { foo: 'hello world', 'the': '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..7a6ebe7 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(','); @@ -230,7 +234,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 => { From a0dfe02968a641e54331a36caa9c280ae2f9c2a3 Mon Sep 17 00:00:00 2001 From: Bert Verhelst Date: Mon, 12 Nov 2018 14:34:09 +0100 Subject: [PATCH 2/2] Also listen to silent option inside the printArray function --- src/prettyPrint.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/prettyPrint.ts b/src/prettyPrint.ts index 7a6ebe7..87e5223 100755 --- a/src/prettyPrint.ts +++ b/src/prettyPrint.ts @@ -218,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(',');