diff --git a/src/prebid.js b/src/prebid.js index 76681364112..a919f77d7d6 100644 --- a/src/prebid.js +++ b/src/prebid.js @@ -154,33 +154,24 @@ function getPresetTargeting() { } } -function getWinningBids(code) { - const getWinningBidForAdUnit = (adUnitCode) => { - const adUnits = $$PREBID_GLOBAL$$._bidsReceived - .filter(bid => bid.adUnitCode === adUnitCode ? bid : null); - - if (adUnits.length) { - return adUnits.reduce(getHighestCpm, { - adUnitCode: adUnitCode, - cpm: 0, - adserverTargeting: {}, - timeToRespond: 0 - }); - } - }; - - if (code) { - return getWinningBidForAdUnit(code); - } else { - return $$PREBID_GLOBAL$$._bidsReceived - .map(bid => bid.adUnitCode) - .filter(uniques) - .map(getWinningBidForAdUnit); - } +function getWinningBids(adUnitCodes) { + return $$PREBID_GLOBAL$$._bidsReceived + .filter(adUnitsFilter.bind(this, adUnitCodes)) + .map(bid => bid.adUnitCode) + .filter(uniques) + .map(adUnitCode => $$PREBID_GLOBAL$$._bidsReceived + .filter(bid => bid.adUnitCode === adUnitCode ? bid : null) + .reduce(getHighestCpm, + { + adUnitCode: adUnitCode, + cpm: 0, + adserverTargeting: {}, + timeToRespond: 0 + })); } -function getWinningBidTargeting() { - let winners = getWinningBids(); +function getWinningBidTargeting(adUnitCodes) { + let winners = getWinningBids(adUnitCodes); // winning bids with deals need an hb_deal targeting key winners @@ -846,38 +837,16 @@ $$PREBID_GLOBAL$$.buildMasterVideoTagFromAdserverTag = function (adserverTag, op }; /** - * Get winning bids for all ad units on page - * @return {array} array containing an object for each winning bid - */ -$$PREBID_GLOBAL$$.getHighestCpmBids = function () { - return getWinningBids(); -}; - -/** - * Get winning bid for ad unit code - * @param {string} adUnitCode ad unit code - * @return {object} winning bid for ad unit code - */ -$$PREBID_GLOBAL$$.getHighestCpmBidForAdUnit = function (adUnitCode) { - if (adUnitCode) { - return getWinningBids(adUnitCode); - } else { - utils.logMessage('getWinningBidForAdUnit requires an adUnitCode'); - } -}; - -/** - * Get array of highest cpm bids for all adUnits, or highest cpm bid - * object for the given adUnit + * Get array of highest cpm bids for all adUnits or the given adUnit * @param {string} optional adUnitCode ad unit code - * @return {array|object} array or object containing highest cpm bid(s) + * @return {array} array containing highest cpm bid object(s) */ -$$PREBID_GLOBAL$$.getHighestCpmBid = function (adUnitCode) { - if (adUnitCode) { - return getWinningBids(adUnitCode); - } else { - return getWinningBids(); - } +$$PREBID_GLOBAL$$.getHighestCpmBids = function (adUnitCode) { + const adUnitCodes = adUnitCode && adUnitCode.length ? + [adUnitCode] : + $$PREBID_GLOBAL$$._adUnitCodes; + + return getWinningBids(adUnitCodes); }; processQue(); diff --git a/test/spec/unit/pbjs_api_spec.js b/test/spec/unit/pbjs_api_spec.js index 1a1d40f46a3..00993da9d34 100644 --- a/test/spec/unit/pbjs_api_spec.js +++ b/test/spec/unit/pbjs_api_spec.js @@ -1247,25 +1247,22 @@ describe('Unit: Prebid Module', function () { }); describe('getHighestCpm', () => { - it('returns an array of winning bids for each adUnit', () => { - const highestCpmBids = $$PREBID_GLOBAL$$.getHighestCpmBid(); - + it('returns an array of winning bid objects for each adUnit', () => { + const highestCpmBids = $$PREBID_GLOBAL$$.getHighestCpmBids(); expect(highestCpmBids.length).to.equal(2); expect(highestCpmBids[0]).to.deep.equal($$PREBID_GLOBAL$$._bidsReceived[1]); expect(highestCpmBids[1]).to.deep.equal($$PREBID_GLOBAL$$._bidsReceived[2]); }); - it('returns the highest bid for the given adUnitCode', () => { - const highestCpmBid = $$PREBID_GLOBAL$$.getHighestCpmBid('/19968336/header-bid-tag-0'); - - expect(highestCpmBid).to.be.an('object'); - expect(highestCpmBid).to.deep.equal($$PREBID_GLOBAL$$._bidsReceived[1]); + it('returns an array containing the highest bid object for the given adUnitCode', () => { + const highestCpmBids = $$PREBID_GLOBAL$$.getHighestCpmBids('/19968336/header-bid-tag-0'); + expect(highestCpmBids.length).to.equal(1); + expect(highestCpmBids[0]).to.deep.equal($$PREBID_GLOBAL$$._bidsReceived[1]); }); - it('returns nothing when the given adUnit is invalid', () => { - const shouldBeUndefined = $$PREBID_GLOBAL$$.getHighestCpmBid('Stallone'); - - expect(shouldBeUndefined).to.be.undefined; + it('returns an empty array when the given adUnit is not found', () => { + const highestCpmBids = $$PREBID_GLOBAL$$.getHighestCpmBids('/stallone'); + expect(highestCpmBids.length).to.equal(0); }); });