Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,11 @@ dist
lib
lib-esm
stats
_bundles
_bundles
__tests__/*.d.ts
__tests__/*.js
__tests__/*.js.map
src/*.d.ts
src/*.js
src/*.js.map
build
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ With [a nice CSS](https://github.com/amelki/json-pretty-html/blob/master/style.c

<img src="https://cdn.pbrd.co/images/GNTkTu9.png" alt="Result" width="350">

## 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: '&nbsp;&nbsp;',
silent: false
});
```

## CSS

You can use the default ['darcula' like stylesheet](https://github.com/amelki/json-pretty-html/blob/master/style.css).
Expand Down
5 changes: 5 additions & 0 deletions __tests__/prettyPrint.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,8 @@ test('escapedHtml', () => {
const json = { foo: 'hello <span> world', 'the<key>': 'other', bar: 'with "double" quotes' };
expect(prettyPrint(json)).toBe('<div class="json-pretty">{<br>&nbsp;&nbsp;"<span class="json-key">foo</span>":&nbsp;"<span class="json-string">hello &lt;span&gt; world</span>",<br>&nbsp;&nbsp;"<span class="json-key">the&lt;key&gt;</span>":&nbsp;"<span class="json-string">other</span>",<br>&nbsp;&nbsp;"<span class="json-key">bar</span>":&nbsp;"<span class="json-string">with &quot;double&quot; quotes</span>"<br>}</div>');
});

test('unexpectedTypes', () => {
const json = { foo: 'test', bar: () => { const test = 'test'; } };
expect(prettyPrint(json)).toBe('<div class="json-pretty">{<br>&nbsp;&nbsp;"<span class="json-key">foo</span>":&nbsp;"<span class="json-string">test</span>",<br>&nbsp;&nbsp;"<span class="json-key">bar</span>":&nbsp;function<br>}</div>');
});
14 changes: 11 additions & 3 deletions src/prettyPrint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(',');
Expand Down Expand Up @@ -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(',');
Expand All @@ -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 => {
Expand Down