An entity tag (ETag) names one version of a file, like "menu-v7". The tea shop's server compares the tags a browser sends with the current one to decide between a full 200 and an empty 304 Not Modified. Write three functions, following RFC 9110.
An entity tag is an opaque tag in double quotes, like "menu-v7", optionally after the prefix W/ (a capital W, as the RFC's grammar requires), which marks a weak tag: one that changes when the file's meaning changes, not necessarily with every byte. Inside the quotes, any visible ASCII character except a double quote is allowed, commas included, and there's no escaping. Anything else, such as menu-v7 without quotes, w/"menu-v7", or W/ "menu-v7", isn't an entity tag, and never matches anything.
strongMatch(a, b) is the RFC's strong comparison: two tags match when neither is weak and their opaque tags are the same, character for character. weakMatch(a, b) is its weak comparison: the opaque tags are the same, whether either tag is weak or not. Both take tags exactly as written, without spaces around them.
ifNoneMatch(field, current) evaluates an If-None-Match header as RFC 9110 section 13.1.2 says. field is the header's value: *, or a list of entity tags separated by commas, with optional spaces and empty items. current is the current version's entity tag, or null when the file doesn't exist. Return false when the condition fails, which is when the server answers a GET with 304: field is * (with optional spaces around it) and the file exists, or a listed tag matches current by weak comparison, which the RFC requires here because a weak tag is good enough to reuse a cached copy. Otherwise return true. A * in a list with other items is invalid, and simply never matches.
strongMatch('"menu-v7"', '"menu-v7"'); // true
strongMatch('W/"menu-v7"', '"menu-v7"'); // false: one is weak
weakMatch('W/"menu-v7"', '"menu-v7"'); // true
ifNoneMatch('"menu-v6", W/"menu-v7"', '"menu-v7"'); // false: the browser has this version, so 304
ifNoneMatch("*", null); // true: there's no menu yet
expect(strongMatch('W/"1"', 'W/"1"')).toBe(false);
expect(strongMatch('W/"1"', 'W/"2"')).toBe(false);
expect(strongMatch('W/"1"', '"1"')).toBe(false);
expect(strongMatch('"1"', 'W/"1"')).toBe(false);
expect(strongMatch('"1"', '"1"')).toBe(true);
expect(strongMatch('"1"', '"2"')).toBe(false);
expect(weakMatch('W/"1"', 'W/"1"')).toBe(true);
expect(weakMatch('W/"1"', 'W/"2"')).toBe(false);
expect(weakMatch('W/"1"', '"1"')).toBe(true);
expect(weakMatch('"1"', 'W/"1"')).toBe(true);
expect(weakMatch('"1"', '"1"')).toBe(true);
expect(strongMatch('"Menu-v7"', '"menu-v7"')).toBe(false);
expect(weakMatch('"menu-v7"', '"menu-v7-"')).toBe(false);
expect(strongMatch('""', '""')).toBe(true);
expect(weakMatch('W/""', '""')).toBe(true);
expect(strongMatch("menu-v7", "menu-v7")).toBe(false);
expect(weakMatch('w/"menu-v7"', 'w/"menu-v7"')).toBe(false);
expect(weakMatch('W/ "menu-v7"', '"menu-v7"')).toBe(false);
expect(weakMatch('"menu"v7"', '"menu"v7"')).toBe(false);
expect(weakMatch('"menu v7"', '"menu v7"')).toBe(false);
expect(ifNoneMatch('"menu-v7"', '"menu-v7"')).toBe(false);
expect(ifNoneMatch('W/"menu-v7"', '"menu-v7"')).toBe(false);
expect(ifNoneMatch('"menu-v7"', 'W/"menu-v7"')).toBe(false);
expect(ifNoneMatch('"menu-v6"', '"menu-v7"')).toBe(true);
expect(ifNoneMatch('"menu-v5", "menu-v6" ,W/"menu-v7"', '"menu-v7"')).toBe(false);
expect(ifNoneMatch('"green", , "mint",', '"mint"')).toBe(false);
expect(ifNoneMatch('"green,mint", "oolong"', '"green,mint"')).toBe(false);
expect(ifNoneMatch('"green,mint"', '"mint"')).toBe(true);
expect(ifNoneMatch("*", '"menu-v7"')).toBe(false);
expect(ifNoneMatch(" * ", 'W/"menu-v7"')).toBe(false);
expect(ifNoneMatch("*", null)).toBe(true);
expect(ifNoneMatch('"menu-v7"', null)).toBe(true);
expect(ifNoneMatch('*, "menu-v6"', '"menu-v7"')).toBe(true);
expect(ifNoneMatch('"menu-v6", *', '"menu-v6"')).toBe(false);
expect(ifNoneMatch("", '"menu-v7"')).toBe(true);
expect(ifNoneMatch("menu-v7", '"menu-v7"')).toBe(true);
expect(ifNoneMatch('w/"menu-v7"', '"menu-v7"')).toBe(true);
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 Compare ETags. 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.