diff --git a/src/util/dataHelpers.js b/src/util/dataHelpers.js index 419aaf9a4..e3cd59778 100644 --- a/src/util/dataHelpers.js +++ b/src/util/dataHelpers.js @@ -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 } @@ -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] diff --git a/test/DataHelperMixin.browser.js b/test/DataHelperMixin.browser.js index dbdc2eec2..c70fbbf77 100644 --- a/test/DataHelperMixin.browser.js +++ b/test/DataHelperMixin.browser.js @@ -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 }