Skip to content

Commit e8e2d3d

Browse files
authored
Merge pull request #68 from streamich/json-print
feat: 🎸 add printJson() method
2 parents ba79007 + e121481 commit e8e2d3d

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {printJson} from '../printJson';
2+
import {printTree} from '../printTree';
3+
4+
test('formats JSON object', () => {
5+
const data = {foo: 'bar'};
6+
const str = 'Node' + printTree('', [(tab) => printJson(tab, data)]);
7+
8+
expect(str).toBe(
9+
`Node
10+
└─ {
11+
"foo": "bar"
12+
}`,
13+
);
14+
});
15+
16+
test('prints literal on the same line', () => {
17+
const str = 'Node' + printTree('', [(tab) => printJson(tab, 123)]);
18+
19+
expect(str).toBe(
20+
`Node
21+
└─ 123`,
22+
);
23+
});

‎src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export type * from './types';
22
export * from './printTree';
33
export * from './printBinary';
4+
export * from './printJson';

‎src/printJson.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const printJson = (tab = '', json: unknown, space: number | string = 2): string =>
2+
(JSON.stringify(json, null, space) || 'nil').split('\n').join('\n' + tab);

0 commit comments

Comments
 (0)