-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathjsShrink.js
27 lines (27 loc) · 1.28 KB
/
jsShrink.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
/** Remove spaces and comments from JavaScript code
* @param string code with commands terminated by semicolon
* @return string shrinked code
* @link http://vrana.github.com/JsShrink/
* @author Jakub Vrana, http://www.vrana.cz/
* @copyright 2007 Jakub Vrana
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 (one or other)
*/
function jsShrink(input) {
var last = '';
return ('\n' + input + '\n').replace(/(?:(^|[-+\([{}=,:;!%^&*|?~]|\/(?![/*])|return|throw)(?:\s|\/\/[^\n]*\n|\/\*(?:[^*]|\*(?!\/))*\*\/)*(\/(?![/*])(?:\\[^\n]|[^[\n\/\\]|\[(?:\\[^\n]|[^\]])+)+\/)|(^|'(?:\\[\s\S]|[^\n'\\])*'|"(?:\\[\s\S]|[^\n"\\])*"|([0-9A-Za-z_$]+)|([-+]+)|.))(?:\s|\/\/[^\n]*\n|\/\*(?:[^*]|\*(?!\/))*\*\/)*/g, function (str, context, regexp, result, word, operator) {
if (word) {
result = (last == 'word' ? '\n' : (last == 'return' ? ' ' : '')) + result;
last = (word == 'return' || word == 'throw' || word == 'break' ? 'return' : 'word');
} else if (operator) {
result = (last == operator.charAt(0) ? '\n' : '') + result;
last = operator.charAt(0);
} else {
if (regexp) {
result = context + (context == '/' ? '\n' : '') + regexp;
}
last = '';
}
return result;
});
}