Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update loose equal function, check toString value for primitive type (fix #4514) #4528

Merged
merged 1 commit into from
Dec 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/shared/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,15 @@ export function genStaticKeys (modules: Array<ModuleOptions>): string {
* if they are plain objects, do they have the same shape?
*/
export function looseEqual (a: mixed, b: mixed): boolean {
/* eslint-disable eqeqeq */
return a == b || (
isObject(a) && isObject(b)
? JSON.stringify(a) === JSON.stringify(b)
: false
)
/* eslint-enable eqeqeq */
const isObjectA = isObject(a)
const isObjectB = isObject(b)
if (isObjectA && isObjectB) {
return JSON.stringify(a) === JSON.stringify(b)
} else if (!isObjectA && !isObjectB) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.
(It may be better to check more strictly ... 😉 )

Copy link
Contributor

@dsonet dsonet Dec 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's loose equal. Which I think just add the 0 check is enough for primitive type cases. So we can skip unnecessary methods invoke here and keep it simple.

if(a === 0) {
  a = '0'
}
if(b === 0) {
  b = '0'
}
return a == b || (
     isObject(a) && isObject(b)
      ? JSON.stringify(a) === JSON.stringify(b)
      : false
 )

Copy link
Member Author

@defcc defcc Dec 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need strict equality for primitive type, e.g., '0' is equal to false. I'll update the test case like bellow

new Vue({
  data: {
    val: false,
    test: 0
  },
  template:
    '<div>' +
      '<input type="radio" value="" v-model="test">' +
      '<input type="radio" value="0" v-model="test">' +
      '<input type="radio" :value="val" v-model="test">' +
    '</div>'
}).$mount('#app')

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done via #4542

return String(a) === String(b)
} else {
return false
}
}

export function looseIndexOf (arr: Array<mixed>, val: mixed): number {
Expand Down
34 changes: 34 additions & 0 deletions test/unit/features/directives/model-checkbox.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,40 @@ describe('Directive v-model checkbox', () => {
expect(vm.check).toEqual(false)
})

it('should respect different primitive type value', (done) => {
const vm = new Vue({
data: {
test: [0]
},
template:
'<div>' +
'<input type="checkbox" value="" v-model="test">' +
'<input type="checkbox" value="0" v-model="test">' +
'<input type="checkbox" value="1" v-model="test">' +
'</div>'
}).$mount()
var checkboxInput = vm.$el.children
expect(checkboxInput[0].checked).toBe(false)
expect(checkboxInput[1].checked).toBe(true)
expect(checkboxInput[2].checked).toBe(false)
vm.test = [1]
waitForUpdate(() => {
expect(checkboxInput[0].checked).toBe(false)
expect(checkboxInput[1].checked).toBe(false)
expect(checkboxInput[2].checked).toBe(true)
vm.test = ['']
}).then(() => {
expect(checkboxInput[0].checked).toBe(true)
expect(checkboxInput[1].checked).toBe(false)
expect(checkboxInput[2].checked).toBe(false)
vm.test = ['', 0, 1]
}).then(() => {
expect(checkboxInput[0].checked).toBe(true)
expect(checkboxInput[1].checked).toBe(true)
expect(checkboxInput[2].checked).toBe(true)
}).then(done)
})

it('warn inline checked', () => {
const vm = new Vue({
template: `<input type="checkbox" v-model="test" checked>`,
Expand Down
29 changes: 29 additions & 0 deletions test/unit/features/directives/model-radio.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,35 @@ describe('Directive v-model radio', () => {
expect(vm.test).toBe(2)
})

it('should respect different primitive type value', (done) => {
const vm = new Vue({
data: {
test: 1
},
template:
'<div>' +
'<input type="radio" value="" v-model="test" name="test">' +
'<input type="radio" value="0" v-model="test" name="test">' +
'<input type="radio" value="1" v-model="test" name="test">' +
'</div>'
}).$mount()
var radioboxInput = vm.$el.children
expect(radioboxInput[0].checked).toBe(false)
expect(radioboxInput[1].checked).toBe(false)
expect(radioboxInput[2].checked).toBe(true)
vm.test = 0
waitForUpdate(() => {
expect(radioboxInput[0].checked).toBe(false)
expect(radioboxInput[1].checked).toBe(true)
expect(radioboxInput[2].checked).toBe(false)
vm.test = ''
}).then(() => {
expect(radioboxInput[0].checked).toBe(true)
expect(radioboxInput[1].checked).toBe(false)
expect(radioboxInput[2].checked).toBe(false)
}).then(done)
})

it('warn inline checked', () => {
const vm = new Vue({
template: `<input v-model="test" type="radio" value="1" checked>`,
Expand Down
31 changes: 30 additions & 1 deletion test/unit/features/directives/model-select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ describe('Directive v-model select', () => {
'<select v-model.number="test">' +
'<option value="1">a</option>' +
'<option :value="2">b</option>' +
' <option :value="3">c</option>' +
'<option :value="3">c</option>' +
'</select>'
}).$mount()
document.body.appendChild(vm.$el)
Expand All @@ -319,6 +319,35 @@ describe('Directive v-model select', () => {
expect(vm.test).toBe(1)
})

it('should respect different pritive type value', (done) => {
const vm = new Vue({
data: {
test: 0
},
template:
'<select v-model.number="test">' +
'<option value="">a</option>' +
'<option value="0">b</option>' +
'<option value="1">c</option>' +
'</select>'
}).$mount()
var opts = vm.$el.options
expect(opts[0].selected).toBe(false)
expect(opts[1].selected).toBe(true)
expect(opts[2].selected).toBe(false)
vm.test = 1
waitForUpdate(() => {
expect(opts[0].selected).toBe(false)
expect(opts[1].selected).toBe(false)
expect(opts[2].selected).toBe(true)
vm.test = ''
}).then(() => {
expect(opts[0].selected).toBe(true)
expect(opts[1].selected).toBe(false)
expect(opts[2].selected).toBe(false)
}).then(done)
})

it('should warn inline selected', () => {
const vm = new Vue({
data: {
Expand Down