/**
* @function buildURI
* @summary
* Use this function to add (and encode) query params to a specified URL.
*
* Options Object:
* * base:
* * Type: String
* * Requirements: Required
* * Description: The base url you would like to append query params on to.
* * Note: This is the URL you would like to click out to.
*
* * [key]:
* * Type: Number or String
* * Description: Any additional opts params will be turned into a query (key+value) pair with the opts key becoming the query key (name) and the opts key's contents becoming the query value.
* * Note: Query value will be encoded (to UTF-8) using encodeURIComponent() to account for special characters.
* * Example:
* * * provided opts: opts.color = '#ffffff'
* * * query param created: &color=%23ffffff
*
* Example:
* ```
* window.$b.snippets.buildURI({
* base: 'https://s.tremorvideodsp.com/ws/video-360/v2.2.17/index.html',
* src: 'https://s.tremorvideodsp.com/v/2018/02/BJHE2L8If/720p.mp4',
* cameraPosition: '15,-10,120',
* sphereGeometry: '500,60,41',
* startingCoords: '0.00,0.00,0.00',
* fov: 75,
* vr: true,
* debug: false
* })
* ```
* @param {Object} opts Object containing all of the params to pass into the function
* @param {string} opts.base The base url to append query params onto. This is the URL you would like to click out to.
* @param {number|string|boolean|null|undefined} opts.* Any additional opts params will be turned into a query (key+value) pair with the opts key becoming the query key (name) and the opts key's contents becoming the query value.
* @returns {string} Returns the opts.base (url) with all query strings appended. This can then be passed into a $b.emitClick event to click out to.
*/
function buildURI (opts) {
if (!opts || typeof opts !== 'object') {
console.error('You must provide an object with keys and values to convert to query string.')
return
}
if (!opts.base || typeof opts.base !== 'string') {
console.error('You must provide a base URL (string) to click out to (opts.base)')
return
}
if (Object.keys(opts).length < 2) {
console.error('Your opts object must contain keys and values to convert to query string.')
return
}
var arr = []
for (var key in opts) {
if (key !== 'base') {
arr.push(String(key))
}
}
var query = '?'
arr.forEach(function (prop) {
if (typeof opts[prop] === 'string' || typeof opts[prop] === 'number' || typeof opts[prop] === 'boolean' || opts[prop] === undefined || opts[prop] === null) {
query += '&' + prop + '=' + encodeURIComponent([opts[prop]])
} else {
console.error('opts.' + prop + ' is invalid. Opts parameters must be either a string or a number.')
}
})
var fullpath = opts.base + query
console.log('Query Params: ', query)
console.log('Full Path: ', fullpath)
return fullpath
}
window.$b = window.$b || {}
window.$b.snippets = window.$b.snippets || {}
var snippets = window.$b.snippets
snippets.buildURI = buildURI
if (window.module) {
window.module.exports = buildURI
}