Skip to content

Commit 9f7b728

Browse files
author
yassh
committed
add -n/--insert-final-newline option
1 parent f5cb6e7 commit 9f7b728

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

lib/cli.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ var options = require("nomnom")
3636
"default": " ",
3737
help: 'character(s) to use for indentation'
3838
},
39+
insertFinalNewline : {
40+
flag : true,
41+
string: '-n, --insert-final-newline',
42+
help: 'ensure JSON string ends with a newline'
43+
},
3944
compact : {
4045
flag : true,
4146
string: '-c, --compact',
@@ -90,7 +95,7 @@ function parse (source) {
9095
}
9196
}
9297

93-
return JSON.stringify(parsed, null, options.indent);
98+
return JSON.stringify(parsed, null, options.indent) + (options.insertFinalNewline ? "\n" : "");
9499
} catch (e) {
95100
if (options.forcePrettyPrint) {
96101
/* From https://github.com/umbrae/jsonlintdotcom:
@@ -100,7 +105,7 @@ function parse (source) {
100105
*/
101106

102107
try {
103-
formatted = formatter.formatJson(source, options.indent);
108+
formatted = formatter.formatJson(source, options.indent, options.insertFinalNewline);
104109
// Re-parse so exception output gets better line numbers
105110
parsed = parser.parse(formatted);
106111
} catch (e) {

lib/formatter.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var formatter = (function () {
1616
return new Array(count + 1).join(s);
1717
}
1818

19-
function formatJson(json, indentChars) {
19+
function formatJson(json, indentChars, insertFinalNewline) {
2020
var i = 0,
2121
il = 0,
2222
tab = (typeof indentChars !== "undefined") ? indentChars : " ",
@@ -80,6 +80,8 @@ var formatter = (function () {
8080
}
8181
}
8282

83+
if (insertFinalNewline) newJson += "\n";
84+
8385
return newJson;
8486
}
8587

0 commit comments

Comments
 (0)