File tree Expand file tree Collapse file tree 5 files changed +35
-20
lines changed Expand file tree Collapse file tree 5 files changed +35
-20
lines changed Original file line number Diff line number Diff line change
1
+ var _ = require ( '../../util' )
2
+
1
3
module . exports = {
2
4
3
5
bind : function ( ) {
@@ -7,11 +9,11 @@ module.exports = {
7
9
var falseExp = this . _checkParam ( 'false-exp' )
8
10
9
11
this . _matchValue = function ( value ) {
10
- var trueValue = true
11
12
if ( trueExp !== null ) {
12
- trueValue = self . vm . $eval ( trueExp )
13
+ return _ . looseEqual ( value , self . vm . $eval ( trueExp ) )
14
+ } else {
15
+ return ! ! value
13
16
}
14
- return trueValue === value
15
17
}
16
18
17
19
function getValue ( ) {
Original file line number Diff line number Diff line change @@ -28,8 +28,6 @@ module.exports = {
28
28
} ,
29
29
30
30
update : function ( value ) {
31
- /* eslint-disable eqeqeq */
32
- this . el . checked = value == this . getValue ( )
33
- /* eslint-enable eqeqeq */
31
+ this . el . checked = _ . looseEqual ( value , this . getValue ( ) )
34
32
}
35
33
}
Original file line number Diff line number Diff line change @@ -65,7 +65,7 @@ module.exports = {
65
65
/* eslint-disable eqeqeq */
66
66
op . selected = multi
67
67
? indexOf ( value , val ) > - 1
68
- : equals ( value , val )
68
+ : _ . looseEqual ( value , val )
69
69
/* eslint-enable eqeqeq */
70
70
}
71
71
} ,
@@ -222,21 +222,9 @@ function getValue (el, multi) {
222
222
function indexOf ( arr , val ) {
223
223
var i = arr . length
224
224
while ( i -- ) {
225
- if ( equals ( arr [ i ] , val ) ) {
225
+ if ( _ . looseEqual ( arr [ i ] , val ) ) {
226
226
return i
227
227
}
228
228
}
229
229
return - 1
230
230
}
231
-
232
- /**
233
- * Check if two values are loosely equal. If two objects
234
- * have the same shape, they are considered equal too:
235
- * equals({a: 1}, {a: 1}) => true
236
- */
237
-
238
- function equals ( a , b ) {
239
- /* eslint-disable eqeqeq */
240
- return a == b || JSON . stringify ( a ) == JSON . stringify ( b )
241
- /* eslint-enable eqeqeq */
242
- }
Original file line number Diff line number Diff line change @@ -287,3 +287,22 @@ exports.cancellable = function (fn) {
287
287
}
288
288
return cb
289
289
}
290
+
291
+ /**
292
+ * Check if two values are loosely equal - that is,
293
+ * if they are plain objects, do they have the same shape?
294
+ *
295
+ * @param {* } a
296
+ * @param {* } b
297
+ * @return {Boolean }
298
+ */
299
+
300
+ exports . looseEqual = function ( a , b ) {
301
+ /* eslint-disable eqeqeq */
302
+ return a == b || (
303
+ exports . isObject ( a ) && exports . isObject ( b )
304
+ ? JSON . stringify ( a ) === JSON . stringify ( b )
305
+ : false
306
+ )
307
+ /* eslint-enable eqeqeq */
308
+ }
Original file line number Diff line number Diff line change @@ -138,4 +138,12 @@ describe('Util - Language Enhancement', function () {
138
138
done ( )
139
139
} , 200 )
140
140
} )
141
+
142
+ it ( 'looseEqual' , function ( ) {
143
+ expect ( _ . looseEqual ( 1 , '1' ) ) . toBe ( true )
144
+ expect ( _ . looseEqual ( null , undefined ) ) . toBe ( true )
145
+ expect ( _ . looseEqual ( { a : 1 } , { a : 1 } ) ) . toBe ( true )
146
+ expect ( _ . looseEqual ( { a : 1 } , { a : 2 } ) ) . toBe ( false )
147
+ expect ( _ . looseEqual ( { } , [ ] ) ) . toBe ( false )
148
+ } )
141
149
} )
You can’t perform that action at this time.
0 commit comments