Skip to content

Commit 77a418f

Browse files
authored
Merge pull request #5 from mjurczyk/feature/4-missing-commas-in-arrays
Adjust output JSON
2 parents 43c4ba5 + 5a30f16 commit 77a418f

File tree

6 files changed

+51
-19
lines changed

6 files changed

+51
-19
lines changed

src/utils/console/pretty-print/index.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ import { prettyPrintString } from './pretty-print-string';
33
import { prettyPrintArray } from './pretty-print-array';
44
import { prettyPrintObject } from './pretty-print-object';
55

6-
export const prettyPrint = (key = null, value = '', indent = 0, colorOutput = false) => {
6+
export const prettyPrint = (
7+
key = null,
8+
value = '',
9+
indent = 0,
10+
colorOutput = false,
11+
addComma = false
12+
) => {
713
const isString = typeof value === 'string';
814
const isArray = value instanceof Array;
915
const isObject = value !== null && typeof value === 'object';
@@ -15,14 +21,14 @@ export const prettyPrint = (key = null, value = '', indent = 0, colorOutput = fa
1521
}
1622

1723
if (isString) {
18-
prettyPrintString(`"${value}"`, hasKey ? 0 : indent, colorOutput);
24+
prettyPrintString(`"${value}"`, hasKey ? 0 : indent, colorOutput, addComma);
1925
} else if (isArray) {
20-
prettyPrintArray(value, indent, colorOutput);
26+
prettyPrintArray(value, indent, colorOutput, addComma);
2127
} else if (isObject) {
22-
prettyPrintObject(value, indent, colorOutput);
28+
prettyPrintObject(value, indent, colorOutput, addComma);
2329
} else if (isSymbol) {
24-
prettyPrintString(Symbol.toString(), indent, colorOutput);
30+
prettyPrintString(Symbol.toString(), indent, colorOutput, addComma);
2531
} else {
26-
prettyPrintString(value, hasKey ? 0 : indent, colorOutput);
32+
prettyPrintString(value, hasKey ? 0 : indent, colorOutput, addComma);
2733
}
2834
};
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
import 'colors';
2+
import R from 'ramda';
23
import { print } from './print';
34
import { getIndentation, indentStep } from './indent';
45
import { prettyPrint } from './index';
56

6-
export const prettyPrintArray = (array = [], indent = 0, colorOutput = false) => {
7+
export const prettyPrintArray = (array = [], indent = 0, colorOutput = false, addComma = false) => {
78
const messageIndent = getIndentation(indent);
9+
const arrayLength = R.length(array) - 1;
810

911
print('[').thenAddNewLine();
1012

11-
array.forEach((child) => {
12-
prettyPrint(null, child, indent + indentStep, colorOutput);
13+
array.forEach((child, index) => {
14+
const isLast = index === arrayLength;
15+
16+
prettyPrint(null, child, indent + indentStep, colorOutput, !isLast);
1317
});
1418

15-
print(`${messageIndent}]`).thenAddNewLine();
19+
print(`${messageIndent}]`)
20+
.thenAddCommaIf(addComma)
21+
.thenAddNewLine();
1622
};

src/utils/console/pretty-print/pretty-print-object.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@ import { print } from './print';
44
import { getIndentation, indentStep } from './indent';
55
import { prettyPrint } from './index';
66

7-
export const prettyPrintObject = (object = {}, indent = 0, colorOutput = false) => {
7+
export const prettyPrintObject = (object = {}, indent = 0, colorOutput = false, addComma = false) => {
88
const messageIndent = getIndentation(indent);
9+
const objectKeysLength = R.compose(R.length, R.keys)(object) - 1;
910

1011
print('{').thenAddNewLine();
1112

12-
R.forEachObjIndexed((value, key) => {
13-
prettyPrint(key, value, indent + indentStep, colorOutput);
13+
R.addIndex(R.forEachObjIndexed)((value, key, object, index) => {
14+
const isLast = index === objectKeysLength;
15+
16+
prettyPrint(key, value, indent + indentStep, colorOutput, !isLast);
1417
})(object);
1518

16-
print(`${messageIndent}}`).thenAddNewLine();
19+
print(`${messageIndent}}`)
20+
.thenAddCommaIf(addComma)
21+
.thenAddNewLine();
1722
};

src/utils/console/pretty-print/pretty-print-string.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import 'colors';
22
import { print } from './print';
33
import { getIndentation } from './indent';
44

5-
export const prettyPrintString = (string = '', indent = 0, colorOutput = false) => {
5+
export const prettyPrintString = (string = '', indent = 0, colorOutput = false, addComma = false) => {
66
const messageIndent = getIndentation(indent);
77
const color = colorOutput ? 'green' : 'white';
88

9-
print(`${messageIndent}${string}`[color]).thenAddNewLine();
9+
print(`${messageIndent}${string}`[color])
10+
.thenAddCommaIf(addComma)
11+
.thenAddNewLine();
1012
};
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
export const newLine = () => process.stdout.write('\n');
1+
export const addNewLine = () => process.stdout.write('\n');
2+
3+
export const addComma = () => process.stdout.write(',');
24

35
export const print = (message = '') => {
46
process.stdout.write(`${message}`);
57

6-
return {
7-
thenAddNewLine: newLine
8+
const chainable = {
9+
thenAddNewLine: () => addNewLine() && chainable,
10+
thenAddCommaIf: (condition) => !(condition && addComma()) && chainable
811
};
12+
13+
return chainable;
914
};

test/unit/utils/cli.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,5 +223,13 @@ describe('utils/cli', () => {
223223

224224
expect(cliScript).to.contain('json-viewer');
225225
}).timeout(scriptTimeout);
226+
227+
it('should output nothing when asked to set always-color flag', () => {
228+
const cliScript = execSync(`cd ${process.cwd()} && cat package.json | babel-node "${mainScriptPath}" --always-color`, {
229+
encoding: 'utf-8'
230+
});
231+
232+
expect(cliScript).to.be.empty;
233+
}).timeout(scriptTimeout);
226234
});
227235
});

0 commit comments

Comments
 (0)