# All files / src/logic filterSrcset.js

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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20

    4x

    3x
    3x
    3x
    5x

    3x

    3x

     // @flow
    import parseSrcset from '../utils/parseSrcset';
    import findClosestDpr from '../utils/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;
    }