@chubbyts/chubbyts-negotiation
Version:
A simple negotiation library.
18 lines (17 loc) • 759 B
JavaScript
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'])));
};