From 5ae7f57e09676e921ebb495f1ddf2d06a34b09cc Mon Sep 17 00:00:00 2001 From: Antony David Date: Thu, 20 Jun 2024 11:23:13 +0200 Subject: [PATCH 1/3] perf: parseAccept without spread operator --- src/helper/accepts/accepts.ts | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/src/helper/accepts/accepts.ts b/src/helper/accepts/accepts.ts index b984e6f36..70e528213 100644 --- a/src/helper/accepts/accepts.ts +++ b/src/helper/accepts/accepts.ts @@ -27,19 +27,27 @@ export interface acceptsOptions extends acceptsConfig { export const parseAccept = (acceptHeader: string): Accept[] => { // Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 - const accepts = acceptHeader.split(',') // ['text/html', 'application/xhtml+xml', 'application/xml;q=0.9', 'image/webp', '*/*;q=0.8'] - return accepts.map((accept) => { - const [type, ...params] = accept.trim().split(';') // ['application/xml', 'q=0.9'] - const q = params.find((param) => param.startsWith('q=')) // 'q=0.9' - return { - type, - params: params.reduce((acc, param) => { - const [key, value] = param.split('=') - return { ...acc, [key.trim()]: value.trim() } - }, {}), - q: q ? parseFloat(q.split('=')[1]) : 1, - } - }) + const accepts = acceptHeader.split(','); + return accepts.map((accept) => { + const parts = accept.trim().split(';'); // ['text/html', 'q=0.9', 'image/webp'] + const type = parts[0]; // text/html + const params = parts.slice(1); // ['q=0.9', 'image/webp'] + const q = params.find((param) => param.startsWith('q=')); + + const paramsObject = params.reduce((acc, param) => { + const keyValue = param.split('='); + const key = keyValue[0].trim(); + const value = keyValue[1].trim(); + acc[key] = value; + return acc; + }, {} as { [key: string]: string }); + + return { + type: type, + params: paramsObject, + q: q ? parseFloat(q.split('=')[1]) : 1, + }; + }); } export const defaultMatch = (accepts: Accept[], config: acceptsConfig): string => { From 25fb9503a0bcdd4b5aee2b2147e8e495d841b2f4 Mon Sep 17 00:00:00 2001 From: Antony David Date: Thu, 20 Jun 2024 11:28:39 +0200 Subject: [PATCH 2/3] fix: format --- src/helper/accepts/accepts.ts | 42 +++++++++++++++++------------------ 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/helper/accepts/accepts.ts b/src/helper/accepts/accepts.ts index 70e528213..c63452098 100644 --- a/src/helper/accepts/accepts.ts +++ b/src/helper/accepts/accepts.ts @@ -27,27 +27,27 @@ export interface acceptsOptions extends acceptsConfig { export const parseAccept = (acceptHeader: string): Accept[] => { // Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 - const accepts = acceptHeader.split(','); - return accepts.map((accept) => { - const parts = accept.trim().split(';'); // ['text/html', 'q=0.9', 'image/webp'] - const type = parts[0]; // text/html - const params = parts.slice(1); // ['q=0.9', 'image/webp'] - const q = params.find((param) => param.startsWith('q=')); - - const paramsObject = params.reduce((acc, param) => { - const keyValue = param.split('='); - const key = keyValue[0].trim(); - const value = keyValue[1].trim(); - acc[key] = value; - return acc; - }, {} as { [key: string]: string }); - - return { - type: type, - params: paramsObject, - q: q ? parseFloat(q.split('=')[1]) : 1, - }; - }); + const accepts = acceptHeader.split(',') + return accepts.map((accept) => { + const parts = accept.trim().split(';') // ['text/html', 'q=0.9', 'image/webp'] + const type = parts[0] // text/html + const params = parts.slice(1) // ['q=0.9', 'image/webp'] + const q = params.find((param) => param.startsWith('q=')) + + const paramsObject = params.reduce((acc, param) => { + const keyValue = param.split('=') + const key = keyValue[0].trim() + const value = keyValue[1].trim() + acc[key] = value + return acc + }, {} as { [key: string]: string }) + + return { + type: type, + params: paramsObject, + q: q ? parseFloat(q.split('=')[1]) : 1, + } + }) } export const defaultMatch = (accepts: Accept[], config: acceptsConfig): string => { From 90d20cbd80427edfb3882ce12f5afb01ab9a9227 Mon Sep 17 00:00:00 2001 From: Antony David Date: Sun, 23 Jun 2024 09:15:44 +0200 Subject: [PATCH 3/3] fix: review --- src/helper/accepts/accepts.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helper/accepts/accepts.ts b/src/helper/accepts/accepts.ts index c63452098..f767da82c 100644 --- a/src/helper/accepts/accepts.ts +++ b/src/helper/accepts/accepts.ts @@ -27,7 +27,7 @@ export interface acceptsOptions extends acceptsConfig { export const parseAccept = (acceptHeader: string): Accept[] => { // Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 - const accepts = acceptHeader.split(',') + const accepts = acceptHeader.split(',') // ['text/html', 'application/xhtml+xml', 'application/xml;q=0.9', 'image/webp', '*/*;q=0.8'] return accepts.map((accept) => { const parts = accept.trim().split(';') // ['text/html', 'q=0.9', 'image/webp'] const type = parts[0] // text/html