Skip to content

Commit

Permalink
chore: [capricorn86#1570] Changed logic to improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
capricorn86 committed Nov 6, 2024
1 parent b545943 commit 511e21e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
16 changes: 10 additions & 6 deletions packages/happy-dom/src/cookie/urilities/CookieStringUtility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ export default class CookieStringUtility {
*/
public static stringToCookie(originURL: URL, cookieString: string): ICookie | null {
const parts = cookieString.split(';');
const [key, ...value] = parts.shift().split('=');
const part = parts.shift();
const index = part.indexOf('=');
const key = index !== -1 ? part.slice(0, index).trim() : part.trim();
const value = index !== -1 ? part.slice(index + 1).trim() : null;

const cookie: ICookie = {
// Required
key: key.trim(),
value: value.length ? value.join('=') : null,
key,
value,
originURL,

// Optional
Expand All @@ -38,9 +41,10 @@ export default class CookieStringUtility {
}

for (const part of parts) {
const [_key, ..._value] = part.split('=');
const key = _key.trim().toLowerCase();
const value = _value.join('=').trim();
const index = part.indexOf('=');
const key =
index !== -1 ? part.slice(0, index).trim().toLowerCase() : part.trim().toLowerCase();
const value = index !== -1 ? part.slice(index + 1).trim() : '';

switch (key) {
case 'expires':
Expand Down
4 changes: 3 additions & 1 deletion packages/happy-dom/test/cookie/CookieContainer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ describe('CookieContainer', () => {
`key1=value1; Expires=${new Date(expires).toString()};`
)
),
<ICookie>CookieStringUtility.stringToCookie(originURL, `key2=value2; Max-Age=${maxAge};`),
<ICookie>(
CookieStringUtility.stringToCookie(originURL, `key2 = value2 ; Max-Age=${maxAge};`)
),
<ICookie>CookieStringUtility.stringToCookie(originURL, `key3=value3; Domain=example.com;`),
<ICookie>CookieStringUtility.stringToCookie(originURL, `key4=value4; Domain=other.com;`),
<ICookie>(
Expand Down

0 comments on commit 511e21e

Please sign in to comment.