@@ -160,7 +160,125 @@ var JSONResponse = {
160160 fullName = fullName . substring ( index + 1 ) ;
161161 }
162162 return fullName ;
163+ } ,
164+
165+
166+
167+ COMPARE_NO_STANDARD : - 1 ,
168+ COMPARE_EQUAL : 0 ,
169+ COMPARE_KEY_MORE : 1 ,
170+ COMPARE_VALUE_CHANGE : 2 ,
171+ COMPARE_KEY_LESS : 3 ,
172+ COMPARE_TYPE_CHANGE : 4 ,
173+ COMPARE_NUMBER_TYPE_CHANGE : 3 ,
174+ COMPARE_CODE_CHANGE : 4 ,
175+
176+ /**测试compare: 对比 新的请求与上次请求的结果
177+ 0-相同,无颜色;
178+ 1-对象新增字段或数组新增值,绿色;
179+ 2-值改变,蓝色;
180+ 3-对象缺少字段/整数变小数,黄色;
181+ 4-类型/code改变,红色;
182+ */
183+ compareResponse : function ( target , real ) {
184+ if ( target == null || target . code == null ) {
185+ return JSONResponse . COMPARE_NO_STANDARD ; //未上传对比标准(正确的结果)
186+ }
187+ if ( target . code != real . code ) {
188+ return JSONResponse . COMPARE_CODE_CHANGE ;
189+ }
190+
191+ delete target . code ;
192+ delete real . code ;
193+
194+ delete target . message ;
195+ delete real . message ;
196+
197+ return JSONResponse . compare ( target , real ) ;
198+ } ,
199+
200+ /**测试compare: 对比 新的请求与上次请求的结果
201+ 0-相同,无颜色;
202+ 1-对象新增字段或数组新增值,绿色;
203+ 2-值改变,蓝色;
204+ 3-对象缺少字段/整数变小数,黄色;
205+ 4-类型/success/errCode改变,红色;
206+ */
207+ compare : function ( target , real ) {
208+ if ( target == null ) {
209+ return real == null ? JSONResponse . COMPARE_EQUAL : JSONResponse . COMPARE_KEY_MORE ;
210+ }
211+ if ( real == null ) { //少了key
212+ return JSONResponse . COMPARE_KEY_LESS ;
213+ }
214+
215+ var type = typeof target ;
216+ if ( type != typeof real ) { //类型改变
217+ return JSONResponse . COMPARE_TYPE_CHANGE ;
218+ }
219+
220+ var max = JSONResponse . COMPARE_EQUAL ;
221+ var each = JSONResponse . COMPARE_EQUAL ;
222+ if ( target instanceof Array ) { // JSONArray
223+ if ( target . length != real . length ) {
224+ max = JSONResponse . COMPARE_VALUE_CHANGE ;
225+ }
226+
227+ var length = Math . min ( target . length , real . length ) ; //多或少都不影响
228+
229+ //TODO 需要把所有值合并,得到最全的,至于类型,Java是很稳定的,同样的key在所有Object的type都相同。
230+ for ( var i = 0 ; i < length ; i ++ ) { //遍历并递归下一层
231+ each = JSONResponse . compare ( target [ i ] , real [ i ] ) ;
232+ if ( max < each ) {
233+ max = each ;
234+ }
235+ if ( max >= JSONResponse . COMPARE_TYPE_CHANGE ) {
236+ break ;
237+ }
238+ }
239+ }
240+ else if ( target instanceof Object ) { // JSONObject
241+ var tks = Object . keys ( target ) ;
242+ var key ;
243+ for ( var i = 0 ; i < tks . length ; i ++ ) { //遍历并递归下一层
244+ key = tks [ i ] ;
245+ if ( key == null ) {
246+ continue ;
247+ }
248+
249+ each = JSONResponse . compare ( target [ key ] , real [ key ] ) ;
250+ if ( max < each ) {
251+ max = each ;
252+ }
253+ if ( max >= JSONResponse . COMPARE_TYPE_CHANGE ) {
254+ break ;
255+ }
256+ }
257+
258+
259+ if ( max < JSONResponse . COMPARE_KEY_MORE ) { //多出key
260+ for ( var k in real ) {
261+ if ( k != null && tks . indexOf ( k ) < 0 ) {
262+ max = JSONResponse . COMPARE_KEY_MORE ;
263+ }
264+ }
265+ }
266+ }
267+ else { // Boolean, Number, String
268+ if ( type == 'number' ) { //数字类型由整数变为小数
269+ if ( String ( target ) . indexOf ( '.' ) < 0 && String ( real ) . indexOf ( '.' ) >= 0 ) {
270+ return JSONResponse . COMPARE_NUMBER_TYPE_CHANGE ;
271+ }
272+ }
273+
274+ if ( target !== real ) { //值不同
275+ return JSONResponse . COMPARE_VALUE_CHANGE ;
276+ }
277+ }
278+
279+ return max ;
163280 }
164281
165282
283+
166284}
0 commit comments