forked from getify/JSON.minify
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathminify.json.js
106 lines (98 loc) · 3.51 KB
/
minify.json.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*! JSON.minify()
v0.2.0 (c) Kyle Simpson
MIT License
*/
/**
* @name minify.json.js
* @author Kei Funagayama <[email protected]>
* @overview JSON.minify
*/
/**
* @namespace JSON
*/
(function(global){
/**
* @function
* @memberof JSON
* @param {Object} Transformed data. format) json-like
* @return {String}
*
* @example
* var json = { // hoge
* "foo": "bar",// this is cool
* "bar": [
* "baz", "bum", "zam" // this is cool
* ]
* } // hoge
*
*/
var minify = function (json) {
var tokenizer = /"|(\/\*)|(\*\/)|(\/\/)|\n|\r|\[|]/g,
in_string = false,
in_multiline_comment = false,
in_singleline_comment = false,
tmp, tmp2, new_str = [], ns = 0, from = 0, lc, rc,
prevFrom
;
tokenizer.lastIndex = 0;
while ( tmp = tokenizer.exec(json) ) {
lc = RegExp.leftContext;
rc = RegExp.rightContext;
if (!in_multiline_comment && !in_singleline_comment) {
tmp2 = lc.substring(from);
if (!in_string) {
tmp2 = tmp2.replace(/(\n|\r|\s)*/g,"");
}
new_str[ns++] = tmp2;
}
prevFrom = from;
from = tokenizer.lastIndex;
// found a " character, and we're not currently in
// a comment? check for previous `\` escaping immediately
// leftward adjacent to this match
if (tmp[0] === "\"" && !in_multiline_comment && !in_singleline_comment) {
// limit left-context matching to only go back
// to the position of the last token match
//
// see: https://github.com/getify/JSON.minify/issues/64
lc.lastIndex = prevFrom;
// perform leftward adjacent escaping match
tmp2 = lc.match(/(\\)*$/);
// start of string with ", or unescaped " character found to end string?
if (!in_string || !tmp2 || (tmp2[0].length % 2) === 0) {
in_string = !in_string;
}
from--; // include " character in next catch
rc = json.substring(from);
}
else if (tmp[0] === "/*" && !in_string && !in_multiline_comment && !in_singleline_comment) {
in_multiline_comment = true;
}
else if (tmp[0] === "*/" && !in_string && in_multiline_comment && !in_singleline_comment) {
in_multiline_comment = false;
}
else if (tmp[0] === "//" && !in_string && !in_multiline_comment && !in_singleline_comment) {
in_singleline_comment = true;
}
else if ((tmp[0] === "\n" || tmp[0] === "\r") && !in_string && !in_multiline_comment && in_singleline_comment) {
in_singleline_comment = false;
}
else if (!in_multiline_comment && !in_singleline_comment && !(/\n|\r|\s/.test(tmp[0]))) {
new_str[ns++] = tmp[0];
}
}
new_str[ns++] = rc;
return new_str.join("");
};
if (typeof module !== 'undefined' && module.exports) {
// node
module.exports = minify;
JSON.minify = minify;
} else {
// others, export global
if (typeof global.JSON === "undefined" || !global.JSON) {
global.JSON = {};
}
global.JSON.minify = minify;
}
})(this);