Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jsonlint

Package Overview
Dependencies
Maintainers
0
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jsonlint - npm Package Compare versions

Comparing version 1.0.1 to 1.1.0

70

lib/cli.js
#!/usr/bin/env node
var sys = require("sys");
var fs = require("fs");
var parser = require("./jsonlint").parser;
var options = require("nomnom")
.scriptName("jsonlint")
.opts({
file: {
position: 0,
help: "file to parse; otherwise uses stdin"
},
version: {
string: '-v, --version',
help: 'print version and exit',
callback: function() {
return JSON.parse(fs.readFileSync(__dirname + "/../package.json", "utf8")).version;
}
},
sort : {
string: '-s, --sort-keys',
help: 'sort object keys'
},
inplace : {
string: '-i, --in-place',
help: 'overwrite the file'
},
indent : {
string: '-t CHAR, --indent CHAR',
default: " ",
help: 'character(s) to use for indentation'
}
})
.parseArgs();
function parse (source) {
try {
sys.puts(JSON.stringify(parser.parse(source),null," "));
var parsed = options.sort ?
sortObject(parser.parse(source)) :
parser.parse(source);
return JSON.stringify(parsed,null,options.indent);
} catch (e) {

@@ -17,5 +51,10 @@ sys.puts(e);

var source = '';
if (args[1]) {
source = require('fs').readFileSync(require('path').join(process.cwd(), args[1]), "utf8");
parse(source);
if (options.file) {
var path = require('path').join(process.cwd(), options.file);
source = fs.readFileSync(path, "utf8");
if (options.inplace) {
fs.writeSync(fs.openSync(path,'w+'), parse(source), 0, "utf8");
} else {
sys.puts(parse(source));
}
} else {

@@ -29,3 +68,3 @@ var stdin = process.openStdin();

stdin.on('end', function () {
parse(source);
sys.puts(parse(source));
});

@@ -35,2 +74,23 @@ }

// from http://stackoverflow.com/questions/1359761/sorting-a-json-object-in-javascript
function sortObject(o) {
if (Object.prototype.toString.call(o) != '[object Object]')
return o;
var sorted = {},
key, a = [];
for (key in o) {
if (o.hasOwnProperty(key)) {
a.push(key);
}
}
a.sort();
for (key = 0; key < a.length; key++) {
sorted[a[key]] = sortObject(o[a[key]]);
}
return sorted;
}
main(process.argv.slice(1));

6

package.json

@@ -11,3 +11,3 @@ {

],
"version": "1.0.1",
"version": "1.1.0",
"preferGlobal": true,

@@ -28,3 +28,5 @@ "repository": {

},
"dependencies": {},
"dependencies": {
"nomnom": ">= 0.4.3"
},
"devDependencies": {

@@ -31,0 +33,0 @@ "test": "*",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc