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 | 4x 3x 3x 3x 5x 3x 3x | // @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;
}
|