Skip to content

Commit

Permalink
optimized minify operation
Browse files Browse the repository at this point in the history
  • Loading branch information
ddesantis committed Feb 23, 2017
1 parent b42b02b commit 02deac0
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 60 deletions.
Binary file modified djson-viewer.crx
Binary file not shown.
2 changes: 1 addition & 1 deletion extension/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
}

// Record current version (in case future update wants to know)
localStorage.djsonVersion = '0.2.3';
localStorage.djsonVersion = '0.2.4';

// Template elements
var baseSpan = document.createElement('span');
Expand Down
59 changes: 1 addition & 58 deletions extension/js/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,62 +35,6 @@
}
};

JSON.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
;

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;
}
from = tokenizer.lastIndex;

if (tmp[0] == "\"" && !in_multiline_comment && !in_singleline_comment) {
tmp2 = lc.match(/(\\)*$/);
if (!in_string || !tmp2 || (tmp2[0].length % 2) == 0) { // start of string with ", or unescaped " character found to end string
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("");
};

document.getElementById("callBeautify").addEventListener("click", beautifyJSON);
document.getElementById("callMinify").addEventListener("click", minifyJSON);
document.getElementById("callView").addEventListener("click", function(){ tabView(false) });
Expand Down Expand Up @@ -120,8 +64,7 @@
try {
var formatted = dumpTextArea.value;
if (formatted.length > 0) {
JSON.parse(formatted);
dumpTextArea.value = JSON.minify(formatted);
dumpTextArea.value = JSON.stringify(JSON.parse(formatted));
copyMe(dumpTextArea);
infoArea.innerHTML = "JSON Minified and copied in your clipboard";
} else {
Expand Down
2 changes: 1 addition & 1 deletion extension/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "DJSON Viewer and Formatter",
"short_name": "DJSON Viewer",
"version": "0.2.3",
"version": "0.2.4",
"manifest_version": 2,
"description": "Extension to format and view JSON, from Web or File.",
"homepage_url": "https://github.com/dardesantis/DJSON-Viewer",
Expand Down

0 comments on commit 02deac0

Please sign in to comment.