All files / src/helpers computeOptions.js

100% Statements 22/22
100% Branches 22/22
100% Functions 5/5
100% Lines 21/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67              23x 103x                 23x 96x   96x 63x 61x 60x     96x                       23x 36x 36x   36x               23x 103x 103x   103x   103x         103x 8x     103x  
import { progressive, PLACEHOLDER_TRANSFORMATIONS, predominantColorTransformPxl } from '../constants';
 
/**
 * 
 * @param {Boolean} enable 
 * @returns {Object} transformation object for progressive effect
 */
const addProgressive = (enable = false) => {
  return enable ? progressive : {}
}
 
/**
 * 
 * @param {Object} baseOptions 
 * @param {Object} extra 
 * @returns {Object} new options Object with extra inside transformation
 */
export const extendOptions = (baseOptions = {}, extra) => {
 let transformation = baseOptions.transformation ? [...baseOptions.transformation] : []
 
 if (Array.isArray(extra)) {
   extra.forEach(effect => transformation.push(effect))
 } else if (extra) {
   transformation.push(extra)
 }
 
 return {
   ...baseOptions,
   transformation
 }
}
 
/**
 * 
 * @param {'lqip'|'color'|'pixelate'|'predominant-color'|'vectorize'|'blur'} type 
 * @param {Option} imgOptions
 * @returns {Object} options for generating delivery URL of a placeholder image
 */
export const computePlaceholder = (type, imgOptions = {}) => {
 const isPredominantWithSize = type === 'predominant-color' && imgOptions.width && imgOptions.height
 const placeholder = isPredominantWithSize ? predominantColorTransformPxl : (PLACEHOLDER_TRANSFORMATIONS[type] || {})
 
 return extendOptions(imgOptions, placeholder)
}
 
/**
 * 
 * @param {Object} object contains accessibility, withProgressive, cldTransforms and baseOptions
 * @returns {Object} options for generating delivery URL of a media component 
 */
export const computeOptions = ({ accessibility, withProgressive, baseOptions = {}, extra = [] }) => {
  const transformation = [...extra]
  const progressive = addProgressive(withProgressive)
 
  transformation.push(progressive)
 
  const res = {
    ...baseOptions,
    transformation
  }
 
  if (accessibility) {
    res.accessibility = accessibility
  }
 
  return res
}