Skip to content

Commit

Permalink
feature: update toTitleCase function to work better with etag (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
levivilet authored Dec 27, 2024
1 parent dc34238 commit 84ed36d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ export const ContentType = 'Content-Type'
export const CrossOriginEmbedderPolicy = 'Cross-Origin-Embedder-Policy'
export const CrossOriginOpenerPolicy = 'Cross-Origin-Opener-Policy'
export const CrossOriginResourcePolicy = 'Cross-Origin-Resource-Policy'
export const Etag = 'Etag'
export const Etag = 'ETag'
export const IfNotMatch = 'if-none-match'
8 changes: 8 additions & 0 deletions packages/preview-process/src/parts/ToTitleCase/ToTitleCase.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
const SPECIAL_CASES: Record<string, string> = {
etag: 'ETag',
}

export const toTitleCase = (key: string): string => {
const lowerKey = key.toLowerCase()
if (SPECIAL_CASES[lowerKey]) {
return SPECIAL_CASES[lowerKey]
}
return key
.split('-')
.map((part) => part.charAt(0).toUpperCase() + part.slice(1).toLowerCase())
Expand Down
2 changes: 1 addition & 1 deletion packages/preview-process/test/SerializeHeaders.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test('serializeHeaders - multiple headers', () => {
headers.set('Cross-Origin-Resource-Policy', 'same-origin')
expect(SerializeHeaders.serializeHeaders(headers)).toEqual({
'Content-Type': 'text/plain',
Etag: '"123"',
ETag: '"123"',
'Cross-Origin-Resource-Policy': 'same-origin',
})
})
Expand Down
2 changes: 1 addition & 1 deletion packages/preview-process/test/SerializeResponse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ test('serializeResponse - with no content', async () => {
init: {
status: 304,
headers: {
Etag: '"123"',
ETag: '"123"',
},
},
})
Expand Down
10 changes: 10 additions & 0 deletions packages/preview-process/test/ToTitleCase.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,13 @@ test('toTitleCase - mixed case', () => {
test('toTitleCase - multiple hyphens', () => {
expect(ToTitleCase.toTitleCase('cross-origin-resource-policy')).toBe('Cross-Origin-Resource-Policy')
})

test('toTitleCase - etag special case', () => {
expect(ToTitleCase.toTitleCase('etag')).toBe('ETag')
})

test('toTitleCase - etag special case with different casing', () => {
expect(ToTitleCase.toTitleCase('ETAG')).toBe('ETag')
expect(ToTitleCase.toTitleCase('ETag')).toBe('ETag')
expect(ToTitleCase.toTitleCase('etAG')).toBe('ETag')
})

0 comments on commit 84ed36d

Please sign in to comment.