minify.json.js

Maintainability

48.77

Lines of code

81

Created with Raphaël 2.1.002550751002013-9-302013-9-302013-9-302013-9-302013-9-302013-9-302013-9-30Maintainability: 48.77

Created with Raphaël 2.1.00204161812013-9-302013-9-302013-9-302013-9-302013-9-302013-9-302013-9-30Lines of Code: 81

Difficulty

32.34

Estimated Errors

0.62

Function weight

By Complexity

Created with Raphaël 2.1.0minify14

By SLOC

Created with Raphaël 2.1.0<anonymous>81
1
/*! JSON.minify()
2
    v0.1 (c) Kyle Simpson
3
    MIT License
4
*/
5
/**
6
 * @name minify.json.js
7
 * @author Kei Funagayama <kei.topaz@gmail.com
8
 * @overview JSON.minify
9
 */
10
 
11
/**
12
 * @namespace JSON
13
 */
14
(function(global){
15
    'use strict';
16
 
17
    /**
18
     * @function
19
     * @memberof JSON
20
     * @param {Object} Transformed data. format) json-like
21
     * @return {String}
22
     *
23
     * @example
24
     * var json = { // hoge
25
     *     "foo": "bar",// this is cool
26
     *     "bar": [
27
     *         "baz", "bum", "zam"   // this is cool
28
     *     ]
29
     * } // hoge
30
     *
31
     */
32
    var minify = function (json) {
33
 
34
        var tokenizer = /"|(\/\*)|(\*\/)|(\/\/)|\n|\r/g,
35
            in_string = false,
36
            in_multiline_comment = false,
37
            in_singleline_comment = false,
38
            tmp, tmp2, new_str = [], ns = 0, from = 0, lc, rc
39
        ;
40
 
41
        tokenizer.lastIndex = 0;
42
 
43
        while ( tmp = tokenizer.exec(json) ) {
Column: 44 "Expected a conditional expression and instead saw an assignment."
44
            lc = RegExp.leftContext;
45
            rc = RegExp.rightContext;
46
            if (!in_multiline_comment && !in_singleline_comment) {
47
                tmp2 = lc.substring(from);
48
                if (!in_string) {
49
                    tmp2 = tmp2.replace(/(\n|\r|\s)*/g,"");
50
                }
51
                new_str[ns++] = tmp2;
52
            }
53
            from = tokenizer.lastIndex;
54
 
55
            if (tmp[0] === "\"" && !in_multiline_comment && !in_singleline_comment) {
56
                tmp2 = lc.match(/(\\)*$/);
57
                if (!in_string || !tmp2 || (tmp2[0].length % 2) === 0) {    // start of string with ", or unescaped " character found to end string
58
                    in_string = !in_string;
59
                }
60
                from--; // include " character in next catch
61
                rc = json.substring(from);
62
            }
63
            else if (tmp[0] === "/*" && !in_string && !in_multiline_comment && !in_singleline_comment) {
64
                in_multiline_comment = true;
65
            }
66
            else if (tmp[0] === "*/" && !in_string && in_multiline_comment && !in_singleline_comment) {
67
                in_multiline_comment = false;
68
            }
69
            else if (tmp[0] === "//" && !in_string && !in_multiline_comment && !in_singleline_comment) {
70
                in_singleline_comment = true;
71
            }
72
            else if ((tmp[0] === "\n" || tmp[0] === "\r") && !in_string && !in_multiline_comment && in_singleline_comment) {
73
                in_singleline_comment = false;
74
            }
75
            else if (!in_multiline_comment && !in_singleline_comment && !(/\n|\r|\s/.test(tmp[0]))) {
76
                new_str[ns++] = tmp[0];
77
            }
78
        }
79
        new_str[ns++] = rc;
80
        return new_str.join("");
81
    };
82
 
83
    if (typeof module !== 'undefined' && module.exports) {
84
        // node
85
        module.exports = minify;
86
        JSON.minify = minify;
87
    } else {
88
        // others, export global
89
        if (typeof global.JSON === "undefined" || !global.JSON) {
90
            global.JSON = {};
91
        }
92
        global.JSON.minify = minify;
93
    }
94
})(this);