Skip to content

Commit 850646f

Browse files
committed
fix checkbox value comparison and use looseEqual in comparisons
1 parent 04f3a4d commit 850646f

File tree

5 files changed

+35
-20
lines changed

5 files changed

+35
-20
lines changed

src/directives/model/checkbox.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var _ = require('../../util')
2+
13
module.exports = {
24

35
bind: function () {
@@ -7,11 +9,11 @@ module.exports = {
79
var falseExp = this._checkParam('false-exp')
810

911
this._matchValue = function (value) {
10-
var trueValue = true
1112
if (trueExp !== null) {
12-
trueValue = self.vm.$eval(trueExp)
13+
return _.looseEqual(value, self.vm.$eval(trueExp))
14+
} else {
15+
return !!value
1316
}
14-
return trueValue === value
1517
}
1618

1719
function getValue () {

src/directives/model/radio.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ module.exports = {
2828
},
2929

3030
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())
3432
}
3533
}

src/directives/model/select.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ module.exports = {
6565
/* eslint-disable eqeqeq */
6666
op.selected = multi
6767
? indexOf(value, val) > -1
68-
: equals(value, val)
68+
: _.looseEqual(value, val)
6969
/* eslint-enable eqeqeq */
7070
}
7171
},
@@ -222,21 +222,9 @@ function getValue (el, multi) {
222222
function indexOf (arr, val) {
223223
var i = arr.length
224224
while (i--) {
225-
if (equals(arr[i], val)) {
225+
if (_.looseEqual(arr[i], val)) {
226226
return i
227227
}
228228
}
229229
return -1
230230
}
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-
}

src/util/lang.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,22 @@ exports.cancellable = function (fn) {
287287
}
288288
return cb
289289
}
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+
}

test/unit/specs/util/lang_spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,12 @@ describe('Util - Language Enhancement', function () {
138138
done()
139139
}, 200)
140140
})
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+
})
141149
})

0 commit comments

Comments
 (0)