Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ahbahpatch #132

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@ jsonlint will either report a syntax error with details or pretty print the sour
file file to parse; otherwise uses stdin

Options:
-v, --version print version and exit
-s, --sort-keys sort object keys
-i, --in-place overwrite the file
-t CHAR, --indent CHAR character(s) to use for indentation [ ]
-c, --compact compact error display
-V, --validate a JSON schema to use for validation
-e, --environment which specification of JSON Schema the validation file uses [json-schema-draft-03]
-q, --quiet do not print the parsed json to STDOUT [false]
-p, --pretty-print force pretty printing even if invalid
-v, --version print version and exit
-s, --sort-keys sort object keys
-i, --in-place overwrite the file
-t CHAR, --indent CHAR character(s) to use for indentation [ ]
-n, --insert-final-newline ensure JSON string ends with a newline
-c, --compact compact error display
-V, --validate a JSON schema to use for validation
-e, --environment which specification of JSON Schema the validation file uses [json-schema-draft-03]
-q, --quiet do not print the parsed json to STDOUT [false]
-p, --pretty-print force pretty printing even if invalid


## Module interface
Expand Down
9 changes: 7 additions & 2 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ var options = require("nomnom")
"default": " ",
help: 'character(s) to use for indentation'
},
insertFinalNewline : {
flag : true,
string: '-n, --insert-final-newline',
help: 'ensure JSON string ends with a newline'
},
compact : {
flag : true,
string: '-c, --compact',
Expand Down Expand Up @@ -90,7 +95,7 @@ function parse (source) {
}
}

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

try {
formatted = formatter.formatJson(source, options.indent);
formatted = formatter.formatJson(source, options.indent, options.insertFinalNewline);
// Re-parse so exception output gets better line numbers
parsed = parser.parse(formatted);
} catch (e) {
Expand Down
4 changes: 3 additions & 1 deletion lib/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var formatter = (function () {
return new Array(count + 1).join(s);
}

function formatJson(json, indentChars) {
function formatJson(json, indentChars, insertFinalNewline) {
var i = 0,
il = 0,
tab = (typeof indentChars !== "undefined") ? indentChars : " ",
Expand Down Expand Up @@ -80,6 +80,8 @@ var formatter = (function () {
}
}

if (insertFinalNewline) newJson += "\n";

return newJson;
}

Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
{
"author": "Zach Carter <[email protected]> (http://zaa.ch)",
"name": "jsonlint",
"name": "jsonlint-newline-fork",
"description": "Validate JSON",
"keywords": [
"json",
"validation",
"lint",
"jsonlint"
],
"version": "1.6.2",
"version": "1.6.3",
"preferGlobal": true,
"repository": {
"type": "git",
"url": "git://github.com/zaach/jsonlint.git"
"url": "git://github.com/kevcenteno/jsonlint.git"
},
"bugs": {
"url": "http://github.com/zaach/jsonlint/issues"
"url": "http://github.com/kevcenteno/jsonlint/issues"
},
"main": "lib/jsonlint.js",
"bin": {
Expand Down