Skip to content

Commit 3b5fe2c

Browse files
committed
code cleanup
1 parent 5ab4f69 commit 3b5fe2c

File tree

1 file changed

+59
-63
lines changed

1 file changed

+59
-63
lines changed

src/jquery.json.js

Lines changed: 59 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
2-
* jQuery JSON Plugin v2.4-edge (2011-09-25)
2+
* jQuery JSON Plugin v2.4-edge (2012-05-13)
33
*
44
* @author Brantley Harris, 2009-2011
5-
* @author Timo Tijhof, 2011
5+
* @author Timo Tijhof, 2011-2012
66
* @source This plugin is heavily influenced by MochiKit's serializeJSON, which is
77
* copyrighted 2005 by Bob Ippolito.
88
* @source Brantley Harris wrote this plugin. It is based somewhat on the JSON.org
@@ -11,10 +11,12 @@
1111
* I uphold.
1212
* @license MIT License <http://www.opensource.org/licenses/mit-license.php>
1313
*/
14+
/*global jQuery */
15+
/*jslint continue: true, plusplus: true */
16+
(function ($) {
17+
"use strict";
1418

15-
(function( $ ) {
16-
17-
var escapeable = /["\\\x00-\x1f\x7f-\x9f]/g,
19+
var escapeable = /["\\\x00-\x1f\x7f-\x9f]/g,
1820
meta = {
1921
'\b': '\\b',
2022
'\t': '\\t',
@@ -37,83 +39,83 @@
3739
* function.
3840
*
3941
*/
40-
$.toJSON = typeof JSON === 'object' && JSON.stringify
41-
? JSON.stringify
42-
: function( o ) {
43-
44-
if ( o === null ) {
42+
$.toJSON = typeof JSON === 'object' && JSON.stringify ? JSON.stringify : function (o) {
43+
if (o === null) {
4544
return 'null';
4645
}
4746

48-
var type = typeof o, $type = $.type( o );
47+
var pairs, i, k, name, val,
48+
type = typeof o,
49+
$type = $.type(o);
4950

50-
if ( type === 'undefined' ) {
51+
if (type === 'undefined') {
5152
return undefined;
5253
}
53-
if ( type === 'number' || type === 'boolean' ) {
54-
return '' + o;
54+
if (type === 'number' || type === 'boolean') {
55+
return String(o);
5556
}
56-
if ( type === 'string') {
57-
return $.quoteString( o );
57+
if (type === 'string') {
58+
return $.quoteString(o);
5859
}
59-
if ( type === 'object' ) {
60-
if ( typeof o.toJSON === 'function' ) {
61-
return $.toJSON( o.toJSON() );
60+
if (type === 'object') {
61+
if (typeof o.toJSON === 'function') {
62+
return $.toJSON(o.toJSON());
6263
}
63-
if ( $type === 'date' ) {
64-
var month = o.getUTCMonth() + 1,
64+
if ($type === 'date') {
65+
var month = o.getUTCMonth() + 1,
6566
day = o.getUTCDate(),
6667
year = o.getUTCFullYear(),
6768
hours = o.getUTCHours(),
6869
minutes = o.getUTCMinutes(),
6970
seconds = o.getUTCSeconds(),
7071
milli = o.getUTCMilliseconds();
7172

72-
if ( month < 10 ) {
73+
if (month < 10) {
7374
month = '0' + month;
7475
}
75-
if ( day < 10 ) {
76+
if (day < 10) {
7677
day = '0' + day;
7778
}
78-
if ( hours < 10 ) {
79+
if (hours < 10) {
7980
hours = '0' + hours;
8081
}
81-
if ( minutes < 10 ) {
82+
if (minutes < 10) {
8283
minutes = '0' + minutes;
8384
}
84-
if ( seconds < 10 ) {
85+
if (seconds < 10) {
8586
seconds = '0' + seconds;
8687
}
87-
if ( milli < 100 ) {
88+
if (milli < 100) {
8889
milli = '0' + milli;
8990
}
90-
if ( milli < 10 ) {
91+
if (milli < 10) {
9192
milli = '0' + milli;
9293
}
9394
return '"' + year + '-' + month + '-' + day + 'T' +
9495
hours + ':' + minutes + ':' + seconds +
9596
'.' + milli + 'Z"';
9697
}
97-
if ( $.isArray( o ) ) {
98-
var ret = [];
99-
for ( var i = 0; i < o.length; i++ ) {
100-
ret.push( $.toJSON( o[i] ) || 'null' );
98+
99+
pairs = [];
100+
101+
if ($.isArray(o)) {
102+
for (i = 0; i < o.length; i++) {
103+
pairs.push($.toJSON(o[i]) || 'null');
101104
}
102-
return '[' + ret.join(',') + ']';
105+
return '[' + pairs.join(',') + ']';
103106
}
104-
var name,
105-
val,
106-
pairs = [];
107-
for ( var k in o ) {
107+
108+
// Plain object
109+
for (k in o) {
108110
// Only include own properties,
109111
// Filter out inherited prototypes
110-
if ( !hasOwn.call( o, k ) ) {
112+
if (!hasOwn.call(o, k)) {
111113
continue;
112114
}
113115

114116
// Keys must be numerical or string. Skip others
115117
type = typeof k;
116-
if ( type === 'number' ) {
118+
if (type === 'number') {
117119
name = '"' + k + '"';
118120
} else if (type === 'string') {
119121
name = $.quoteString(k);
@@ -125,13 +127,13 @@
125127
// Invalid values like these return undefined
126128
// from toJSON, however those object members
127129
// shouldn't be included in the JSON string at all.
128-
if ( type === 'function' || type === 'undefined' ) {
130+
if (type === 'function' || type === 'undefined') {
129131
continue;
130132
}
131-
val = $.toJSON( o[k] );
132-
pairs.push( name + ':' + val );
133+
val = $.toJSON(o[k]);
134+
pairs.push(name + ':' + val);
133135
}
134-
return '{' + pairs.join( ',' ) + '}';
136+
return '{' + pairs.join(',') + '}';
135137
}
136138
};
137139

@@ -141,9 +143,7 @@
141143
*
142144
* @param src {String}
143145
*/
144-
$.evalJSON = typeof JSON === 'object' && JSON.parse
145-
? JSON.parse
146-
: function( src ) {
146+
$.evalJSON = typeof JSON === 'object' && JSON.parse ? JSON.parse : function (src) {
147147
return eval('(' + src + ')');
148148
};
149149

@@ -153,21 +153,17 @@
153153
*
154154
* @param src {String}
155155
*/
156-
$.secureEvalJSON = typeof JSON === 'object' && JSON.parse
157-
? JSON.parse
158-
: function( src ) {
159-
160-
var filtered =
156+
$.secureEvalJSON = typeof JSON === 'object' && JSON.parse ? JSON.parse : function (src) {
157+
var filtered =
161158
src
162-
.replace( /\\["\\\/bfnrtu]/g, '@' )
163-
.replace( /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']')
164-
.replace( /(?:^|:|,)(?:\s*\[)+/g, '');
159+
.replace(/\\["\\\/bfnrtu]/g, '@')
160+
.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']')
161+
.replace(/(?:^|:|,)(?:\s*\[)+/g, '');
165162

166-
if ( /^[\],:{}\s]*$/.test( filtered ) ) {
167-
return eval( '(' + src + ')' );
168-
} else {
169-
throw new SyntaxError( 'Error parsing JSON, source is not valid.' );
163+
if (/^[\],:{}\s]*$/.test(filtered)) {
164+
return eval('(' + src + ')');
170165
}
166+
throw new SyntaxError('Error parsing JSON, source is not valid.');
171167
};
172168

173169
/**
@@ -181,11 +177,11 @@
181177
* >>> jQuery.quoteString('"Where are we going?", she asked.')
182178
* "\"Where are we going?\", she asked."
183179
*/
184-
$.quoteString = function( string ) {
185-
if ( string.match( escapeable ) ) {
186-
return '"' + string.replace( escapeable, function( a ) {
180+
$.quoteString = function (string) {
181+
if (string.match(escapeable)) {
182+
return '"' + string.replace(escapeable, function (a) {
187183
var c = meta[a];
188-
if ( typeof c === 'string' ) {
184+
if (typeof c === 'string') {
189185
return c;
190186
}
191187
c = a.charCodeAt();
@@ -195,4 +191,4 @@
195191
return '"' + string + '"';
196192
};
197193

198-
})( jQuery );
194+
}(jQuery));

0 commit comments

Comments
 (0)