From 368fb1329af47597c1853a1e5b874217aa154de8 Mon Sep 17 00:00:00 2001 From: RefinedSoftwareLLC Date: Fri, 22 Mar 2013 17:17:42 -0600 Subject: [PATCH] beautifyJSON(files, options) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit beautifyJSON(files, options) files = a (single or array of) file names, JSON strings, or JSON Objects. options = UglifyJS minify options. NEW OPTIONS:     fromObject:true allows passing a JSON object or an array of JSON objects.     replacer:function(key, oldValue){ return newValue; }     replacer:["Array", "of", "kept", "properties", "all", "others", "discarded"] UNSUPPORTED: (Source Maps):true, compress:true, quote_keys:false. WARNING: All input is run through JSON.stringify() which will change some invalid     values (like NaN and infinity) and error on others (like incorrect syntax). --- tools/node.js | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/tools/node.js b/tools/node.js index c0dd3dbc..dbb70d97 100644 --- a/tools/node.js +++ b/tools/node.js @@ -115,6 +115,77 @@ exports.minify = function(files, options) { }; }; +exports.beautifyJSON = function(files, options) { + // files = a (single or array of) file names, JSON strings, or JSON Objects. + // options = UglifyJS minify options. + // NEW OPTIONS: + // fromObject:true allows passing a JSON object or an array of JSON objects. + // replacer:function(key, oldValue){ return newValue; } + // replacer:["Array", "of", "kept", "properties", "all", "others", "discarded"] + // UNSUPPORTED: (Source Maps):true, compress:true, quote_keys:false. + // WARNING: All input is run through JSON.stringify() which will change some invalid + // values (like NaN and infinity) and error on others (like incorrect syntax). + + // 1. updating options with standard beautify settings and overrides: + options = UglifyJS.defaults(options, { + fromString : false, + fromObject : false, + warnings : false, + compress : false, + output : { beautify : true, quote_keys : true } + }); + + // 2. override unsupported options that have compatibility errors: + options.outSourceMap = null; + options.sourceRoot = null; + options.inSourceMap = null; + options.compress = false; + if (typeof options.output === "undefined" || options.output === null) { + options.output = {}; + } + options.output.quote_keys = true; + + // 3. reading files, parsing, and (if needed) bundling in to array: (Arrays are legal JavaScript code) + var isArray; + if (Object.prototype.toString.call(files) === "[object Array]") { + isArray = true; + } else { + isArray = false; + files = [ files ]; + } + + var code; + if (options.fromObject) { + code = files; + } else if (options.fromString) { + code = []; + files.forEach(function(file, i){ + code[i] = JSON.parse(file); + }); + } else /* if (options.fromFile) */ { + code = []; + files.forEach(function(file, i){ + code[i] = JSON.parse( fs.readFileSync(file,"utf8") ); + }); + } + options.fromString = true; + + // 4. filturing (replacer), changing incorrect values, and stringifying: + code = JSON.stringify(code, options.replacer); + + // 5. beautifying: + code = UglifyJS.minify(code, options).code; + + // 6. removing trailing semicolon and (if needed) unbundling out of array: + if (isArray) { + code = code.substr(0, code.length - 1); + } else { + code = code.substr(2, code.length - 5); + } + + return code; +}; + // exports.describe_ast = function() { // function doitem(ctor) { // var sub = {};