Prebid.js icon indicating copy to clipboard operation
Prebid.js copied to clipboard

utils.extend() destroys arrays

Open BartVB opened this issue 10 years ago • 6 comments

I'm currently implementing Criteo RTA. A part of the implementation is:

    pbjs.bidderSettings = {
        criteo: {
            alwaysUseBid : true,
            adserverTargeting: [{
                key: "criteo",
                val: function (bidResponse) {
                    return bidResponse.keys;
                }
            }
            ]
        },

This should result in setTargeting(key, value) being called where 'value' is an array of zones which are present in the Criteo cookie.

Alas this doesn't work because: https://github.com/prebid/Prebid.js/blob/master/src/utils.js#L99

replaces the array with an Object. I fixed this by replacing this code with simply:

exports.extend = function(target, source){
    target = target || {};

    this._each(source,function(value,prop){
            target[prop] = source[prop];
    });
    return target;
};

This also brings it more into accordance with Object.assign() where only one level of properties is copied.

BartVB avatar Dec 23 '15 15:12 BartVB

@mkendall07 @jdelhommeau mind take a look?

ghost avatar Jan 04 '16 23:01 ghost

maybe to avoid any breaking changes/still fix the array issue we could just change line 103 from:

        if (typeof source[prop] === objectType_object) {

to

        if (this.isA(source[prop], 'Object')) {

@BartVB would that work for criteo?

nickjacob avatar Jan 15 '16 03:01 nickjacob

@nickjacob @BartVB @mkendall07 does this work? Is it preferable to copy arrays as arrays or is the object Object cast a requirement of the current implementation?

And does this solve this issue?

protonate avatar Feb 05 '16 22:02 protonate

@protonate You'll need to search the source for callers of the function to determine how it's used today. I'm not sure if it will work right if it is changed.

mkendall07 avatar Feb 08 '16 21:02 mkendall07

I believe this is no longer needed, if any updates please reopen. Closing.

protonate avatar Mar 24 '16 22:03 protonate

Apologies for the late response! Got sidetracked.

I've tested this with current master and that code still destroys arrays: https://github.com/prebid/Prebid.js/blob/c14bd2e99081ce121bb638219911764f9edf42bc/src/utils.js#L108

The solution which @nickjacob suggests solves the problem for me.

BartVB avatar Apr 21 '16 13:04 BartVB