All files / utils filterSrcset.js

0% Statements 0/10
0% Branches 0/10
0% Functions 0/3
0% Lines 0/7

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                                       
// @flow
import parseSrcset from './parseSrcset';
import findClosestDpr from './findClosestDpr';
 
export default function filterImgSrc({ dataset: { src, srcset } }: any) {
  if (!srcset) return src;
 
  // $FlowIgnoreLine: DOM api
  const clientWidth = document.documentElement.clientWidth || window.innerWidth; // eslint-disable-line no-undef
  const devicePixelRatio = window.devicePixelRatio; // eslint-disable-line no-undef
  const parsedSrcset = parseSrcset(srcset);
  const srcInArray = parsedSrcset.map(s => ({
    ...s,
    ...(!s.dpr && s.width ? { dpr: s.width / clientWidth } : null),
  }));
  const foundSrc = srcInArray.find(({ dpr }) => devicePixelRatio === dpr);
 
  return foundSrc ? foundSrc.url : findClosestDpr(srcInArray, devicePixelRatio).url;
}