Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix formatting empty Uint8Arrays #181

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions __tests__/sanitization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ describe('sanitization', () => {
expect(format(query, [state])).toEqual(expected)
})

test('formats empty Uint8Array', () => {
const query = 'select 1 from user where state = ?'
const expected = "select 1 from user where state = x''"
expect(format(query, [new Uint8Array([])])).toEqual(expected)
})

test('escapes double quotes', () => {
const query = 'select 1 from user where state = ?'
const expected = 'select 1 from user where state = \'\\"a\\"\''
Expand Down
3 changes: 2 additions & 1 deletion __tests__/text.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ describe('text', () => {

describe('uint8ArrayToHex', () => {
test('converts an array of 8-bit unsigned integers to hex', () => {
expect(uint8ArrayToHex(new Uint8Array([197]))).toEqual('0xc5')
expect(uint8ArrayToHex(new Uint8Array([]))).toEqual("x''")
expect(uint8ArrayToHex(new Uint8Array([197]))).toEqual("x'c5'")
})
})
})
2 changes: 1 addition & 1 deletion src/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function uint8Array(text: string): Uint8Array {

export function uint8ArrayToHex(uint8: Uint8Array): string {
const digits = Array.from(uint8).map((i) => i.toString(16).padStart(2, '0'))
return `0x${digits.join('')}`
return `x'${digits.join('')}'`
}

function bytes(text: string): number[] {
Expand Down
Loading