Skip to content

Instantly share code, notes, and snippets.

@oliveiraev
Created November 26, 2014 15:59
Show Gist options
  • Save oliveiraev/a900f84c0158fc985dbb to your computer and use it in GitHub Desktop.
Save oliveiraev/a900f84c0158fc985dbb to your computer and use it in GitHub Desktop.

Revisions

  1. oliveiraev created this gist Nov 26, 2014.
    32 changes: 32 additions & 0 deletions pretty_json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    #!/usr/bin/env node
    /**
    * @name JSON Prettifier
    * @summary Given an input (filename or stdin), prints out it space-indented
    * @license [LGPLv3]{@link https://www.gnu.org/licenses/lgpl-3.0.txt}
    * @version 0.0.1a
    *
    */
    (function () {
    "use strict";
    var buffer, output;
    /**
    * Stores formatted JSON into <code>shared</code> output var
    * @param {String} input Unformatted json
    */
    function prettify(input) {
    input = JSON.parse(input);
    output = JSON.stringify(input, null, 4);
    }
    /**
    * Send data stored into <code>shared</code> to standard output
    */
    function print() {
    process.stdout.write(output + "\r\n");
    }
    buffer = process.stdin;
    if (process.argv.length > 2) {
    buffer = require("fs").createReadStream(process.argv[2]);
    }
    buffer.on("data", prettify);
    buffer.on("end", print);
    }());