The tea shop keeps each product photo in several widths and lists them in srcset with w descriptors, so each screen gets one that's sharp enough and no bigger. Write pickSrcset(srcset, slotWidth, devicePixelRatio). It takes a srcset value, the width the image will take up in CSS pixels (what sizes works out to, always more than 0), and the screen's device pixel ratio, and returns the candidate a browser requests, as { url, density }, or null when there's no valid candidate.
Split the value the way the HTML Standard's "parse a srcset attribute" does. Skip whitespace and commas between candidates. A candidate's URL runs up to the next whitespace, commas included; if it ends with commas, they're removed, and the candidate has no descriptors. Otherwise its descriptors run up to the next comma that isn't inside parentheses, separated by whitespace.
A descriptor is a width, Nw, where N is digits only and more than 0; a density, Nx, where N is a number like 2, 1.5, or .5 (with an exponent allowed, and never below 0); or a height, Nh, which is only allowed next to a width and changes nothing here. A candidate is invalid, and skipped, if it has any other descriptor, two widths or two densities, both a width and a density, or a height without a width.
Give each candidate a density: its width divided by slotWidth for a width, its number for a density, and 1 when it has neither. Keep only the first candidate of each density, sort them from the lowest density to the highest, and pick the first whose density is at least devicePixelRatio, or the densest one when none is.
pickSrcset("/teas/green-400.jpg 400w, /teas/green-800.jpg 800w, /teas/green-1600.jpg 1600w", 400, 1.5);
// In a 400 px slot the densities are 1, 2, and 4, and the first one of at least 1.5 is
// { url: "/teas/green-800.jpg", density: 2 }
That last step is how the HTML Standard selects an image source, and it's what Chrome does for a fresh page. The standard leaves a browser one choice of its own: it may take a denser candidate whose image it already has without a network request, such as one in its memory cache, and Chrome does.
const srcset = "/teas/green-400.jpg 400w, /teas/green-800.jpg 800w, /teas/green-1600.jpg 1600w";
expect(pickSrcset(srcset, 400, 1)).toEqual({ url: "/teas/green-400.jpg", density: 1 });
expect(pickSrcset(srcset, 400, 1.5)).toEqual({ url: "/teas/green-800.jpg", density: 2 });
expect(pickSrcset(srcset, 400, 2)).toEqual({ url: "/teas/green-800.jpg", density: 2 });
expect(pickSrcset(srcset, 400, 3)).toEqual({ url: "/teas/green-1600.jpg", density: 4 });
expect(pickSrcset(srcset, 200, 1)).toEqual({ url: "/teas/green-400.jpg", density: 2 });
const srcset = "/teas/green-1600.jpg 1600w, /teas/green-400.jpg 400w, /teas/green-800.jpg 800w";
expect(pickSrcset(srcset, 1000, 2)).toEqual({ url: "/teas/green-1600.jpg", density: 1.6 });
expect(pickSrcset(srcset, 400, 1.5)).toEqual({ url: "/teas/green-800.jpg", density: 2 });
expect(pickSrcset("/teas/mint.jpg, /teas/mint-2x.jpg 2x", 300, 1)).toEqual({ url: "/teas/mint.jpg", density: 1 });
expect(pickSrcset("/teas/mint.jpg, /teas/mint-2x.jpg 2x", 300, 1.25)).toEqual({ url: "/teas/mint-2x.jpg", density: 2 });
expect(pickSrcset("/teas/mint-small.jpg .5x, /teas/mint.jpg 1e0x", 300, 0.5)).toEqual({ url: "/teas/mint-small.jpg", density: 0.5 });
expect(pickSrcset("/teas/black-a.jpg 800w, /teas/black-b.jpg 800w", 400, 2)).toEqual({ url: "/teas/black-a.jpg", density: 2 });
expect(pickSrcset("/teas/black-2x.jpg 2x, /teas/black-800.jpg 800w", 400, 2)).toEqual({ url: "/teas/black-2x.jpg", density: 2 });
expect(pickSrcset("/teas/black-800.jpg 800w, /teas/black-2x.jpg 2x", 400, 2)).toEqual({ url: "/teas/black-800.jpg", density: 2 });
const messy = " \n /teas/mint-400.jpg 400w ,, /teas/mint,800.jpg 800w ";
expect(pickSrcset(messy, 400, 2)).toEqual({ url: "/teas/mint,800.jpg", density: 2 });
expect(pickSrcset(messy, 400, 1)).toEqual({ url: "/teas/mint-400.jpg", density: 1 });
expect(pickSrcset("/teas/mint-small.jpg, /teas/mint-big.jpg 2x", 400, 1)).toEqual({ url: "/teas/mint-small.jpg", density: 1 });
const srcset = "/teas/black-400.jpg 400w (warm, dark), /teas/black-800.jpg 800w";
// The first candidate has a descriptor it doesn't know, "(warm, dark)", so it's skipped.
expect(pickSrcset(srcset, 400, 1)).toEqual({ url: "/teas/black-800.jpg", density: 2 });
const srcset = [
"/teas/a.jpg 0w",
"/teas/b.jpg 400.5w",
"/teas/c.jpg 400w 2x",
"/teas/d.jpg -1x",
"/teas/e.jpg 600h",
"/teas/f.jpg 800w 800w",
"/teas/g.jpg 800",
"/teas/h.jpg 1.x",
"/teas/i.jpg 1600w 1200h",
].join(", ");
expect(pickSrcset(srcset, 400, 1)).toEqual({ url: "/teas/i.jpg", density: 4 });
expect(pickSrcset("", 400, 1)).toBe(null);
expect(pickSrcset(" , ,", 400, 1)).toBe(null);
expect(pickSrcset("/teas/a.jpg 0w, /teas/b.jpg 2x 3x", 400, 1)).toBe(null);
Run tests runs every test on your code, here in your browser. The problem is solved when they all pass.
Ctrl + Enter runs the tests (⌘ + Enter on a Mac).
I'm here to help with Pick a srcset candidate. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.