Skip to content

Commit

Permalink
[fixed] false positive on selecting item
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Jul 20, 2016
1 parent 61ee9c3 commit 8a4c455
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
20 changes: 7 additions & 13 deletions src/util/dataHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ export function dataText(item, textField){

export function dataIndexOf(data, item, valueField){
var idx = -1, len = data.length
, finder = datum => valueMatcher(item, datum, valueField);
, isValueEqual = datum => valueMatcher(item, datum, valueField);

while (++idx < len)
if (finder(data[idx])) return idx
while (++idx < len) {
var datum = data[idx];
if (datum === item || isValueEqual(datum))
return idx
}

return -1
}
Expand All @@ -44,16 +47,7 @@ export function valueMatcher(a, b, valueField){
}

export function dataItem(data, item, valueField) {
var first = data[0]
, idx;

// make an attempt to see if we were passed in dataItem vs just a valueField value
// either an object with the right prop, or a primitive
// { valueField: 5 } || "hello" [ "hello" ]
if (has(item, valueField) || typeof first === typeof item)
return item

idx = dataIndexOf(data, dataValue(item, valueField), valueField)
var idx = dataIndexOf(data, dataValue(item, valueField), valueField)

if (idx !== -1)
return data[idx]
Expand Down
12 changes: 12 additions & 0 deletions test/DataHelperMixin.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ describe('when using DATA HELPERS', function(){
})
})

it('should work with dataItem', function(){
var val = { value: 3 }

expect(helpers.dataItem([ 2, 3, 1], 1)).to.equal(1)

expect(helpers.dataItem([{}, val, {}], val)).to.equal(val)

expect(helpers.dataItem([{}, val, {}], { value: 3 })).to.equal(val)

expect(helpers.dataItem([{}, val, {}], 3, 'value')).to.equal(val)
})


it('should work with indexOf', function(){
var val = { value: 3 }
Expand Down

0 comments on commit 8a4c455

Please sign in to comment.