]>
git.r.bdr.sh - rbdr/dotfiles/blob - atom/packages/pretty-json/node_modules/jsonminify/minify.json.js
7 * @author Kei Funagayama <kei.topaz@gmail.com
8 * @overview JSON.minify
20 * @param {Object} Transformed data. format) json-like
24 * var json = { // hoge
25 * "foo": "bar",// this is cool
27 * "baz", "bum", "zam" // this is cool
32 var minify = function (json
) {
34 var tokenizer
= /"|(\/\*)|(\*\/)|(\/\/)|\n|\r/g,
36 in_multiline_comment
= false,
37 in_singleline_comment
= false,
38 tmp
, tmp2
, new_str
= [], ns
= 0, from = 0, lc
, rc
41 tokenizer
.lastIndex
= 0;
43 while ( tmp
= tokenizer
.exec(json
) ) {
44 lc
= RegExp
.leftContext
;
45 rc
= RegExp
.rightContext
;
46 if (!in_multiline_comment
&& !in_singleline_comment
) {
47 tmp2
= lc
.substring(from);
49 tmp2
= tmp2
.replace(/(\n|\r|\s)*/g,"");
53 from = tokenizer
.lastIndex
;
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
;
60 from--; // include " character in next catch
61 rc
= json
.substring(from);
63 else if (tmp
[0] === "/*" && !in_string
&& !in_multiline_comment
&& !in_singleline_comment
) {
64 in_multiline_comment
= true;
66 else if (tmp
[0] === "*/" && !in_string
&& in_multiline_comment
&& !in_singleline_comment
) {
67 in_multiline_comment
= false;
69 else if (tmp
[0] === "//" && !in_string
&& !in_multiline_comment
&& !in_singleline_comment
) {
70 in_singleline_comment
= true;
72 else if ((tmp
[0] === "\n" || tmp
[0] === "\r") && !in_string
&& !in_multiline_comment
&& in_singleline_comment
) {
73 in_singleline_comment
= false;
75 else if (!in_multiline_comment
&& !in_singleline_comment
&& !(/\n|\r|\s/.test(tmp
[0]))) {
76 new_str
[ns
++] = tmp
[0];
80 return new_str
.join("");
83 if (typeof module
!== 'undefined' && module
.exports
) {
85 module
.exports
= minify
;
88 // others, export global
89 if (typeof global
.JSON
=== "undefined" || !global
.JSON
) {
92 global
.JSON
.minify
= minify
;