Skip to content

Commit 54de489

Browse files
committed
Performance, optimization and readability + minor bug fixes:
* Merge var statements into a comma-separated list on top of blocks * Add some whitespace for readability (we've got min anyway) * Removing underscores from variable name (they have no meaning in javascript). They local variables already in their function closure * Removing 'var' from 'var type' on line 109. 'var type' was already defined in this scope on line 46 (there is no block scope in javascript, just function scope) * Merging the filter.replace functions into a single line, using the prototype chaining. * Rebuilding .min.js * Fixing typo in README
1 parent 6b28743 commit 54de489

File tree

3 files changed

+62
-63
lines changed

3 files changed

+62
-63
lines changed

libs/README

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
To run the minifier, run this command from the root directory of the repository:
22

3-
python libs/jsmin.py < src/jquery.json.js > src/jquey.json.min.js
3+
python libs/jsmin.py < src/jquery.json.js > src/jquery.json.min.js

src/jquery.json.js

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,25 @@
1515
*/
1616

1717
(function( $ ) {
18+
19+
var escapeable = /["\\\x00-\x1f\x7f-\x9f]/g,
20+
meta = {
21+
'\b': '\\b',
22+
'\t': '\\t',
23+
'\n': '\\n',
24+
'\f': '\\f',
25+
'\r': '\\r',
26+
'"' : '\\"',
27+
'\\': '\\\\'
28+
};
29+
1830
/**
1931
* jQuery.toJSON
2032
* Converts the given argument into a JSON respresentation.
2133
*
2234
* @param o {Mixed} The json-serializble *thing* to be converted
2335
*
24-
* If an object has a "toJSON" function, that will be used to get the representation.
36+
* If an object has a toJSON prototype, that will be used to get the representation.
2537
* Non-integer/string keys are skipped in the object, as are keys that point to a
2638
* function.
2739
*
@@ -50,37 +62,38 @@
5062
return $.toJSON( o.toJSON() );
5163
}
5264
if ( o.constructor === Date ) {
53-
var month = o.getUTCMonth() + 1;
65+
var month = o.getUTCMonth() + 1,
66+
day = o.getUTCDate(),
67+
year = o.getUTCFullYear(),
68+
hours = o.getUTCHours(),
69+
minutes = o.getUTCMinutes(),
70+
seconds = o.getUTCSeconds(),
71+
milli = o.getUTCMilliseconds();
72+
5473
if ( month < 10 ) {
5574
month = '0' + month;
5675
}
57-
var day = o.getUTCDate();
5876
if ( day < 10 ) {
5977
day = '0' + day;
6078
}
61-
var year = o.getUTCFullYear();
62-
var hours = o.getUTCHours();
6379
if ( hours < 10 ) {
6480
hours = '0' + hours;
6581
}
66-
var minutes = o.getUTCMinutes();
6782
if ( minutes < 10 ) {
6883
minutes = '0' + minutes;
6984
}
70-
var seconds = o.getUTCSeconds();
7185
if ( seconds < 10 ) {
7286
seconds = '0' + seconds;
7387
}
74-
var milli = o.getUTCMilliseconds();
7588
if ( milli < 100 ) {
7689
milli = '0' + milli;
7790
}
7891
if ( milli < 10 ) {
7992
milli = '0' + milli;
8093
}
8194
return '"' + year + '-' + month + '-' + day + 'T' +
82-
hours + ':' + minutes + ':' + seconds +
83-
'.' + milli + 'Z"';
95+
hours + ':' + minutes + ':' + seconds +
96+
'.' + milli + 'Z"';
8497
}
8598
if ( o.constructor === Array ) {
8699
var ret = [];
@@ -89,10 +102,11 @@
89102
}
90103
return '[' + ret.join(',') + ']';
91104
}
92-
var pairs = [];
105+
var name,
106+
val,
107+
pairs = [];
93108
for ( var k in o ) {
94-
var name;
95-
var type = typeof k;
109+
type = typeof k;
96110
if ( type === 'number' ) {
97111
name = '"' + k + '"';
98112
} else if (type === 'string') {
@@ -106,7 +120,7 @@
106120
//skip pairs where the value is a function.
107121
continue;
108122
}
109-
var val = $.toJSON( o[k] );
123+
val = $.toJSON( o[k] );
110124
pairs.push(name + ':' + val);
111125
}
112126
return '{' + pairs.join(', ') + '}';
@@ -137,29 +151,19 @@
137151
return JSON.parse( src );
138152
}
139153

140-
var filtered = src;
141-
filtered = filtered.replace(/\\["\\\/bfnrtu]/g, '@');
142-
filtered = filtered.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']');
143-
filtered = filtered.replace(/(?:^|:|,)(?:\s*\[)+/g, '');
154+
var filtered =
155+
src
156+
.replace( /\\["\\\/bfnrtu]/g, '@' )
157+
.replace( /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']')
158+
.replace( /(?:^|:|,)(?:\s*\[)+/g, '');
144159

145160
if ( /^[\],:{}\s]*$/.test( filtered ) ) {
146-
return eval('(' + src + ')');
161+
return eval( '(' + src + ')' );
147162
} else {
148163
throw new SyntaxError( 'Error parsing JSON, source is not valid.' );
149164
}
150165
};
151166

152-
var _escapeable = /["\\\x00-\x1f\x7f-\x9f]/g;
153-
var _meta = {
154-
'\b': '\\b',
155-
'\t': '\\t',
156-
'\n': '\\n',
157-
'\f': '\\f',
158-
'\r': '\\r',
159-
'"' : '\\"',
160-
'\\': '\\\\'
161-
};
162-
163167
/**
164168
* jQuery.quoteString
165169
* Returns a string-repr of a string, escaping quotes intelligently.
@@ -172,9 +176,9 @@
172176
* "\"Where are we going?\", she asked."
173177
*/
174178
$.quoteString = function( string ) {
175-
if ( string.match( _escapeable ) ) {
176-
return '"' + string.replace( _escapeable, function( a ) {
177-
var c = _meta[a];
179+
if ( string.match( escapeable ) ) {
180+
return '"' + string.replace( escapeable, function( a ) {
181+
var c = meta[a];
178182
if ( typeof c === 'string' ) {
179183
return c;
180184
}

src/jquery.json.min.js

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,26 @@
11

2-
(function($){$.toJSON=function(o)
3-
{if(typeof(JSON)=='object'&&JSON.stringify)
4-
return JSON.stringify(o);var type=typeof(o);if(o===null)
5-
return"null";if(type=="undefined")
6-
return undefined;if(type=="number"||type=="boolean")
7-
return o+"";if(type=="string")
8-
return $.quoteString(o);if(type=='object')
9-
{if(typeof o.toJSON=="function")
10-
return $.toJSON(o.toJSON());if(o.constructor===Date)
11-
{var month=o.getUTCMonth()+1;if(month<10)month='0'+month;var day=o.getUTCDate();if(day<10)day='0'+day;var year=o.getUTCFullYear();var hours=o.getUTCHours();if(hours<10)hours='0'+hours;var minutes=o.getUTCMinutes();if(minutes<10)minutes='0'+minutes;var seconds=o.getUTCSeconds();if(seconds<10)seconds='0'+seconds;var milli=o.getUTCMilliseconds();if(milli<100)milli='0'+milli;if(milli<10)milli='0'+milli;return'"'+year+'-'+month+'-'+day+'T'+
2+
(function($){var escapeable=/["\\\x00-\x1f\x7f-\x9f]/g,meta={'\b':'\\b','\t':'\\t','\n':'\\n','\f':'\\f','\r':'\\r','"':'\\"','\\':'\\\\'};$.toJSON=function(o){if(typeof JSON==='object'&&JSON.stringify){return JSON.stringify(o);}
3+
var type=typeof o;if(o===null){return'null';}
4+
if(type==='undefined'){return undefined;}
5+
if(type==='number'||type==='boolean'){return o+'';}
6+
if(type==='string'){return $.quoteString(o);}
7+
if(type==='object'){if(typeof o.toJSON==='function'){return $.toJSON(o.toJSON());}
8+
if(o.constructor===Date){var month=o.getUTCMonth()+1,day=o.getUTCDate(),year=o.getUTCFullYear(),hours=o.getUTCHours(),minutes=o.getUTCMinutes(),seconds=o.getUTCSeconds(),milli=o.getUTCMilliseconds();if(month<10){month='0'+month;}
9+
if(day<10){day='0'+day;}
10+
if(hours<10){hours='0'+hours;}
11+
if(minutes<10){minutes='0'+minutes;}
12+
if(seconds<10){seconds='0'+seconds;}
13+
if(milli<100){milli='0'+milli;}
14+
if(milli<10){milli='0'+milli;}
15+
return'"'+year+'-'+month+'-'+day+'T'+
1216
hours+':'+minutes+':'+seconds+'.'+milli+'Z"';}
13-
if(o.constructor===Array)
14-
{var ret=[];for(var i=0;i<o.length;i++)
15-
ret.push($.toJSON(o[i])||"null");return"["+ret.join(",")+"]";}
16-
var pairs=[];for(var k in o){var name;var type=typeof k;if(type=="number")
17-
name='"'+k+'"';else if(type=="string")
18-
name=$.quoteString(k);else
19-
continue;if(typeof o[k]=="function")
20-
continue;var val=$.toJSON(o[k]);pairs.push(name+":"+val);}
21-
return"{"+pairs.join(", ")+"}";}};$.evalJSON=function(src)
22-
{if(typeof(JSON)=='object'&&JSON.parse)
23-
return JSON.parse(src);return eval("("+src+")");};$.secureEvalJSON=function(src)
24-
{if(typeof(JSON)=='object'&&JSON.parse)
25-
return JSON.parse(src);var filtered=src;filtered=filtered.replace(/\\["\\\/bfnrtu]/g,'@');filtered=filtered.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,']');filtered=filtered.replace(/(?:^|:|,)(?:\s*\[)+/g,'');if(/^[\],:{}\s]*$/.test(filtered))
26-
return eval("("+src+")");else
27-
throw new SyntaxError("Error parsing JSON, source is not valid.");};$.quoteString=function(string)
28-
{if(_escapeable.test(string))
29-
{return'"'+string.replace(_escapeable,function(a)
30-
{var c=_meta[a];if(typeof c==='string')return c;c=a.charCodeAt();return'\\u00'+Math.floor(c/16).toString(16)+(c%16).toString(16);})+'"';}
31-
return'"'+string+'"';};var _escapeable=/["\\\x00-\x1f\x7f-\x9f]/g;var _meta={'\b':'\\b','\t':'\\t','\n':'\\n','\f':'\\f','\r':'\\r','"':'\\"','\\':'\\\\'};})(jQuery);
17+
if(o.constructor===Array){var ret=[];for(var i=0;i<o.length;i++){ret.push($.toJSON(o[i])||'null');}
18+
return'['+ret.join(',')+']';}
19+
var name,val,pairs=[];for(var k in o){type=typeof k;if(type==='number'){name='"'+k+'"';}else if(type==='string'){name=$.quoteString(k);}else{continue;}
20+
if(typeof o[k]==='function'){continue;}
21+
val=$.toJSON(o[k]);pairs.push(name+':'+val);}
22+
return'{'+pairs.join(', ')+'}';}};$.evalJSON=function(src){if(typeof JSON==='object'&&JSON.parse){return JSON.parse(src);}
23+
return eval('('+src+')');};$.secureEvalJSON=function(src){if(typeof JSON==='object'&&JSON.parse){return JSON.parse(src);}
24+
var filtered=src.replace(/\\["\\\/bfnrtu]/g,'@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,']').replace(/(?:^|:|,)(?:\s*\[)+/g,'');if(/^[\],:{}\s]*$/.test(filtered)){return eval('('+src+')');}else{throw new SyntaxError('Error parsing JSON, source is not valid.');}};$.quoteString=function(string){if(string.match(escapeable)){return'"'+string.replace(escapeable,function(a){var c=meta[a];if(typeof c==='string'){return c;}
25+
c=a.charCodeAt();return'\\u00'+Math.floor(c/16).toString(16)+(c%16).toString(16);})+'"';}
26+
return'"'+string+'"';};})(jQuery);

0 commit comments

Comments
 (0)