ReformatCode Project on GitHub

By Xeon Xai  

About:

I have added an open source project to the GitHub.  The project, ReformatCode currently only supports reformatting JSON data to human readable format.  Though I hope in the future it can be extended to support CSS indentation structuring with intelligent parent – child relationship styles.   Formatting CSS can be a little complex if you want it to really show properly how the CSS works with other classes and what is a sub element or sub class of another.  So that would be a possible and welcome addition to the code base in the future if effort is put into it.

Download:

ReformatCode Project via GitHub

Usage:

Passing either a JSON object or string, will reformat the JSON to a human readable level.
Second param (optional) is an object of settings to override defaults.

Example:

ReformatCode.formatJSON({"key": {"sub_key": "sub_value"}}, {"indent": "spaces", "debug": true, "newline": "\r"});

Input:

{"key": {"sub_key": "sub_value"}

Ouput:

{
    "key": {
        "sub_key": "sub_value"
    }
}
VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.22_1171]
Rating: -1 (from 1 vote)

One Comment

  1. anon
    Posted December 16, 2010 at 1:30 PM | Permalink | Reply

    function DumpObjectIndented(obj, indent)
    {
    var result = “”;
    if (indent == null) indent = “”;

    for (var property in obj)
    {
    var value = obj[property];
    if (typeof value == ‘string’)
    value = “‘” + value + “‘”;
    else if (typeof value == ‘object’)
    {
    if (value instanceof Array)
    {
    // Just let JS convert the Array to a string!
    value = “[ " + value + " ]“;
    }
    else
    {
    // Recursive dump
    // (replace ” ” by “\t” or something else if you prefer)
    var od = DumpObjectIndented(value, indent + ” “);
    // If you like { on the same line as the key
    //value = “{\n” + od + “\n” + indent + “}”;
    // If you prefer { and } to be aligned
    value = “\n” + indent + “{\n” + od + “\n” + indent + “}”;
    }
    }
    result += indent + “‘” + property + “‘ : ” + value + “,\n”;
    }
    return result.replace(/,\n$/, “”);
    }

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: +1 (from 1 vote)

Post a Comment

Your email is never shared. Required fields are marked *

*
*