From a0755830c3e54bb97c88b7bb283d9b9ee5f4ef82 Mon Sep 17 00:00:00 2001 From: ASurovenko Date: Tue, 25 Jun 2024 16:17:21 +0200 Subject: [PATCH] extract buildVideo into utils --- modules/zeta_global_sspBidAdapter.js | 71 +--------------------------- modules/zmaticooBidAdapter.js | 67 +------------------------- src/utils.js | 69 +++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 136 deletions(-) diff --git a/modules/zeta_global_sspBidAdapter.js b/modules/zeta_global_sspBidAdapter.js index 2ea6d745096..72aeb2b5b2a 100644 --- a/modules/zeta_global_sspBidAdapter.js +++ b/modules/zeta_global_sspBidAdapter.js @@ -1,4 +1,4 @@ -import {deepAccess, deepSetValue, isArray, isBoolean, isNumber, isStr, logWarn} from '../src/utils.js'; +import {buildVideo, deepSetValue, isArray, logWarn} from '../src/utils.js'; import {registerBidder} from '../src/adapters/bidderFactory.js'; import {BANNER, VIDEO} from '../src/mediaTypes.js'; import {config} from '../src/config.js'; @@ -20,32 +20,6 @@ const DEFAULT_CUR = 'USD'; const TTL = 300; const NET_REV = true; -const DATA_TYPES = { - 'NUMBER': 'number', - 'STRING': 'string', - 'BOOLEAN': 'boolean', - 'ARRAY': 'array', - 'OBJECT': 'object' -}; -const VIDEO_CUSTOM_PARAMS = { - 'mimes': DATA_TYPES.ARRAY, - 'minduration': DATA_TYPES.NUMBER, - 'maxduration': DATA_TYPES.NUMBER, - 'startdelay': DATA_TYPES.NUMBER, - 'playbackmethod': DATA_TYPES.ARRAY, - 'api': DATA_TYPES.ARRAY, - 'protocols': DATA_TYPES.ARRAY, - 'w': DATA_TYPES.NUMBER, - 'h': DATA_TYPES.NUMBER, - 'battr': DATA_TYPES.ARRAY, - 'linearity': DATA_TYPES.NUMBER, - 'placement': DATA_TYPES.NUMBER, - 'plcmt': DATA_TYPES.NUMBER, - 'minbitrate': DATA_TYPES.NUMBER, - 'maxbitrate': DATA_TYPES.NUMBER, - 'skip': DATA_TYPES.NUMBER -} - export const spec = { code: BIDDER_CODE, gvlid: 469, @@ -308,49 +282,6 @@ function buildBanner(request) { } } -function buildVideo(request) { - let video = {}; - const videoParams = deepAccess(request, 'mediaTypes.video', {}); - for (const key in VIDEO_CUSTOM_PARAMS) { - if (videoParams.hasOwnProperty(key)) { - video[key] = checkParamDataType(key, videoParams[key], VIDEO_CUSTOM_PARAMS[key]); - } - } - if (videoParams.playerSize) { - if (isArray(videoParams.playerSize[0])) { - video.w = parseInt(videoParams.playerSize[0][0], 10); - video.h = parseInt(videoParams.playerSize[0][1], 10); - } else if (isNumber(videoParams.playerSize[0])) { - video.w = parseInt(videoParams.playerSize[0], 10); - video.h = parseInt(videoParams.playerSize[1], 10); - } - } - return video; -} - -function checkParamDataType(key, value, datatype) { - let functionToExecute; - switch (datatype) { - case DATA_TYPES.BOOLEAN: - functionToExecute = isBoolean; - break; - case DATA_TYPES.NUMBER: - functionToExecute = isNumber; - break; - case DATA_TYPES.STRING: - functionToExecute = isStr; - break; - case DATA_TYPES.ARRAY: - functionToExecute = isArray; - break; - } - if (functionToExecute(value)) { - return value; - } - logWarn('Ignoring param key: ' + key + ', expects ' + datatype + ', found ' + typeof value); - return undefined; -} - function provideEids(request, payload) { if (Array.isArray(request.userIdAsEids) && request.userIdAsEids.length > 0) { deepSetValue(payload, 'user.ext.eids', request.userIdAsEids); diff --git a/modules/zmaticooBidAdapter.js b/modules/zmaticooBidAdapter.js index 905da191ab7..a26c34767b8 100644 --- a/modules/zmaticooBidAdapter.js +++ b/modules/zmaticooBidAdapter.js @@ -1,4 +1,4 @@ -import {deepAccess, isArray, isBoolean, isNumber, isStr, logWarn, triggerPixel} from '../src/utils.js'; +import {buildVideo, deepAccess, logWarn, triggerPixel} from '../src/utils.js'; import {registerBidder} from '../src/adapters/bidderFactory.js'; import {BANNER, VIDEO} from '../src/mediaTypes.js'; @@ -16,28 +16,6 @@ const DEFAULT_CUR = 'USD'; const TTL = 200; const NET_REV = true; -const DATA_TYPES = { - 'NUMBER': 'number', 'STRING': 'string', 'BOOLEAN': 'boolean', 'ARRAY': 'array', 'OBJECT': 'object' -}; -const VIDEO_CUSTOM_PARAMS = { - 'mimes': DATA_TYPES.ARRAY, - 'minduration': DATA_TYPES.NUMBER, - 'maxduration': DATA_TYPES.NUMBER, - 'startdelay': DATA_TYPES.NUMBER, - 'playbackmethod': DATA_TYPES.ARRAY, - 'api': DATA_TYPES.ARRAY, - 'protocols': DATA_TYPES.ARRAY, - 'w': DATA_TYPES.NUMBER, - 'h': DATA_TYPES.NUMBER, - 'battr': DATA_TYPES.ARRAY, - 'linearity': DATA_TYPES.NUMBER, - 'placement': DATA_TYPES.NUMBER, - 'plcmt': DATA_TYPES.NUMBER, - 'minbitrate': DATA_TYPES.NUMBER, - 'maxbitrate': DATA_TYPES.NUMBER, - 'skip': DATA_TYPES.NUMBER -} - export const spec = { code: BIDDER_CODE, supportedMediaTypes: [BANNER, VIDEO], @@ -239,49 +217,6 @@ function buildBanner(request) { }; } -function buildVideo(request) { - let video = {}; - const videoParams = deepAccess(request, 'mediaTypes.video', {}); - for (const key in VIDEO_CUSTOM_PARAMS) { - if (videoParams.hasOwnProperty(key)) { - video[key] = checkParamDataType(key, videoParams[key], VIDEO_CUSTOM_PARAMS[key]); - } - } - if (videoParams.playerSize) { - if (isArray(videoParams.playerSize[0])) { - video.w = parseInt(videoParams.playerSize[0][0], 10); - video.h = parseInt(videoParams.playerSize[0][1], 10); - } else if (isNumber(videoParams.playerSize[0])) { - video.w = parseInt(videoParams.playerSize[0], 10); - video.h = parseInt(videoParams.playerSize[1], 10); - } - } - return video; -} - -export function checkParamDataType(key, value, datatype) { - let functionToExecute; - switch (datatype) { - case DATA_TYPES.BOOLEAN: - functionToExecute = isBoolean; - break; - case DATA_TYPES.NUMBER: - functionToExecute = isNumber; - break; - case DATA_TYPES.STRING: - functionToExecute = isStr; - break; - case DATA_TYPES.ARRAY: - functionToExecute = isArray; - break; - } - if (functionToExecute(value)) { - return value; - } - logWarn('Ignoring param key: ' + key + ', expects ' + datatype + ', found ' + typeof value); - return undefined; -} - function hasBannerMediaType(bidRequest) { return !!deepAccess(bidRequest, 'mediaTypes.banner'); } diff --git a/src/utils.js b/src/utils.js index 1f05b7b37c1..7e595b185ef 100644 --- a/src/utils.js +++ b/src/utils.js @@ -4,10 +4,37 @@ import {includes} from './polyfill.js'; import { EVENTS, S2S } from './constants.js'; import {GreedyPromise} from './utils/promise.js'; import {getGlobal} from './prebidGlobal.js'; +import { default as deepAccess } from 'dlv/index.js'; export { default as deepAccess } from 'dlv/index.js'; export { dset as deepSetValue } from 'dset'; +const DATA_TYPES = { + 'NUMBER': 'number', + 'STRING': 'string', + 'BOOLEAN': 'boolean', + 'ARRAY': 'array', + 'OBJECT': 'object' +}; +const VIDEO_CUSTOM_PARAMS = { + 'mimes': DATA_TYPES.ARRAY, + 'minduration': DATA_TYPES.NUMBER, + 'maxduration': DATA_TYPES.NUMBER, + 'startdelay': DATA_TYPES.NUMBER, + 'playbackmethod': DATA_TYPES.ARRAY, + 'api': DATA_TYPES.ARRAY, + 'protocols': DATA_TYPES.ARRAY, + 'w': DATA_TYPES.NUMBER, + 'h': DATA_TYPES.NUMBER, + 'battr': DATA_TYPES.ARRAY, + 'linearity': DATA_TYPES.NUMBER, + 'placement': DATA_TYPES.NUMBER, + 'plcmt': DATA_TYPES.NUMBER, + 'minbitrate': DATA_TYPES.NUMBER, + 'maxbitrate': DATA_TYPES.NUMBER, + 'skip': DATA_TYPES.NUMBER +} + var tStr = 'String'; var tFn = 'Function'; var tNumb = 'Number'; @@ -1189,3 +1216,45 @@ export function hasNonSerializableProperty(obj, checkedObjects = new Set()) { } return false; } + +export function buildVideo(request) { + let video = {}; + const videoParams = deepAccess(request, 'mediaTypes.video', {}); + for (const key in VIDEO_CUSTOM_PARAMS) { + if (videoParams.hasOwnProperty(key)) { + video[key] = checkParamDataType(key, videoParams[key], VIDEO_CUSTOM_PARAMS[key]); + } + } + if (videoParams.playerSize) { + if (isArray(videoParams.playerSize[0])) { + video.w = parseInt(videoParams.playerSize[0][0], 10); + video.h = parseInt(videoParams.playerSize[0][1], 10); + } else if (isNumber(videoParams.playerSize[0])) { + video.w = parseInt(videoParams.playerSize[0], 10); + video.h = parseInt(videoParams.playerSize[1], 10); + } + } + return video; +} + +function checkParamDataType(key, value, datatype) { + let functionToExecute; + switch (datatype) { + case DATA_TYPES.BOOLEAN: + functionToExecute = isBoolean; + break; + case DATA_TYPES.NUMBER: + functionToExecute = isNumber; + break; + case DATA_TYPES.STRING: + functionToExecute = isStr; + break; + case DATA_TYPES.ARRAY: + functionToExecute = isArray; + break; + } + if (functionToExecute(value)) { + return value; + } + return undefined; +}