Test case details

Preparation Code

const testHeaders = ` Content-Type: application/json Authorization: Bearer token123 Accept-Language: en-US User-Agent: Mozilla/5.0 Cache-Control: no-cache Connection: keep-alive Host: example.com Accept: */* Content-Length: 123 `

Test cases

Test #1

const headersWithFlatMap = Object.fromEntries( testHeaders .split(/\r?\n/) .flatMap((line) => { const match = /^([\w-]+): (.+)$/.exec(line); return match ? [[match[1], match[2]]] : []; }) );

Test #2

const headersWithMapFilter = Object.fromEntries( testHeaders .split(/\r?\n/) .map((line) => { const match = /^([\w-]+): (.+)$/.exec(line); return match ? [match[1], match[2]] : null; }) .filter(Boolean) );