Skip to content

Commit c3c87af

Browse files
committed
Fix formatting empty Uint8Array
1 parent 68ed111 commit c3c87af

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

__tests__/sanitization.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ describe('sanitization', () => {
8080
expect(format(query, [state])).toEqual(expected)
8181
})
8282

83+
test('formats empty Uint8Array', () => {
84+
const query = 'select 1 from user where state = ?'
85+
const expected = "select 1 from user where state = x''"
86+
expect(format(query, [new Uint8Array([])])).toEqual(expected)
87+
})
88+
8389
test('escapes double quotes', () => {
8490
const query = 'select 1 from user where state = ?'
8591
const expected = 'select 1 from user where state = \'\\"a\\"\''

__tests__/text.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ describe('text', () => {
4242

4343
describe('uint8ArrayToHex', () => {
4444
test('converts an array of 8-bit unsigned integers to hex', () => {
45-
expect(uint8ArrayToHex(new Uint8Array([197]))).toEqual('0xc5')
45+
expect(uint8ArrayToHex(new Uint8Array([]))).toEqual("x''")
46+
expect(uint8ArrayToHex(new Uint8Array([197]))).toEqual("x'c5'")
4647
})
4748
})
4849
})

src/text.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function uint8Array(text: string): Uint8Array {
1515

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

2121
function bytes(text: string): number[] {

0 commit comments

Comments
 (0)