Skip to content

Commit

Permalink
Merge pull request #181 from emmatown/fix-sanitizing-empty-uint8array
Browse files Browse the repository at this point in the history
Fix formatting empty Uint8Arrays
  • Loading branch information
ayrton authored Aug 12, 2024
2 parents a5c70a9 + f69cd76 commit 8e947f5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
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

0 comments on commit 8e947f5

Please sign in to comment.