
A release archiver accepts filenames that start with an ISO-shaped date and a dash. Named groups make the extracted fields easier to pass into the next step. The third input demonstrates shape validation accepting a calendar impossibility. Group names improve readability but do not verify date semantics.
Isolate the operation in one function
The example keeps the input to RegExp.exec outside the function, so it is possible to run the same transformation in a test. The comparison uses concrete data rather than the rendered component. If the application accepts a value from JSON, run the fixture after parsing that JSON to preserve the relevant types.
function evaluate(value) {
return (/^(?<year>[0-9]{4})-(?<month>[0-9]{2})-(?<day>[0-9]{2})-/.exec(value)?.groups) ?? null;
}
const samples = ["2026-12-03-cover.jpg", "draft-12.jpg", "2026-22-35-x"];
for (const sample of samples) console.log(evaluate(sample));Trace the result
| Input | Return value |
|---|---|
| '2026-12-03-cover.jpg' | {"year": "2026", "month": "12", "day": "03"} |
| 'draft-12.jpg' | None |
| '2026-22-35-x' | {"year": "2026", "month": "22", "day": "35"} |
For '2026-12-03-cover.jpg', the observed output is {"year": "2026", "month": "12", "day": "03"}. With 'draft-12.jpg', it becomes None. The useful comparison is at the application boundary. Run the pair against the input as it reaches the program after decoding or parsing, then inspect what the next component receives.
The input '2026-22-35-x' produces 3 named fields. Check the key as well as the associated value. A record containing a key with a false or empty value can carry a different instruction from an absent key.
What the calling feature must decide
Use a calendar-aware validation step before creating the actual publication date.
Run one case twice through the real feature and look at both the displayed value and its stored representation. A component may coerce a result to text even when the implementation returns a mapping from names to values. Keep a failing sample small enough that it can travel with a bug report, including the browser or Node version that produced it.
A second check against a changed input
A dated filename can pass the shape test while naming February 31. Keep filename recognition and calendar validation as independently reported checks so the editor knows whether to repair the spelling or the date itself.
The same JavaScript function was also run with '' / '2026-12-03-cover.jpg ' / 'gpj.revoc-30-21-6202'. These additional inputs change the boundary without changing the implementation. An exception name in the table records where the operation rejects an input; a returned empty or zero value records a successful call. Choose deliberately which of those outcomes the surrounding feature should expose.
| Additional fixture | Result or exception |
|---|---|
| '' | None |
| '2026-12-03-cover.jpg ' | {"year": "2026", "month": "12", "day": "03"} |
| 'gpj.revoc-30-21-6202' | None |
JavaScript standard library reference · MDN JavaScript language guide
Sources and further reading
Cover photograph: Wikimedia Morocco Mini-Hackathon 2026 day 2 - Person seated at desk with laptop and stickers - 0159 by DERDAKI ILIAS. CC BY-SA 4.0. Cropped and typeset for this cover. The photograph illustrates the topic. This image adaptation uses the same share-alike license.
