UNPKG

@chubbyts/chubbyts-negotiation

Version:
18 lines (17 loc) 759 B
export const resolveHeaderToMap = (header) => { return new Map(header .split(',') .map((headerValue) => { const [notTrimmedName, ...notSplittedAttributes] = headerValue.split(';'); const name = notTrimmedName.trim(); const attributes = Object.fromEntries(notSplittedAttributes .filter((attribute) => -1 !== attribute.search(/=/)) .map((attribute) => { const [attributeKey, attributeValue] = attribute.split('='); return [attributeKey.trim(), attributeValue.trim()]; })); return [name, { ...attributes, q: attributes['q'] ?? '1.0' }]; }) .filter(([locale]) => locale !== '') .sort((a, b) => b[1]['q'].localeCompare(a[1]['q']))); };