Skip to content

Commit 9f46615

Browse files
committed
Add tests
1 parent 1d8f5ab commit 9f46615

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

__tests__/index.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,11 @@ describe('cast', () => {
626626
expect(cast({ name: 'test', type: 'FLOAT64' }, '2.32')).toEqual(2.32)
627627
})
628628

629+
test('casts binary data to array of 8-bit unsigned integers', () => {
630+
expect(cast({ name: 'test', type: 'BLOB' }, '')).toEqual(new Uint8Array([]))
631+
expect(cast({ name: 'test', type: 'BLOB' }, 'Å')).toEqual(new Uint8Array([197]))
632+
})
633+
629634
test('casts JSON string to JSON object', () => {
630635
expect(cast({ name: 'test', type: 'JSON' }, '{ "foo": "bar" }')).toStrictEqual({ foo: 'bar' })
631636
})

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ function decodeRow(row: QueryResultRow): Array<string | null> {
379379
}
380380

381381
export function cast(field: Field, value: string | null): any {
382-
if (value === '' || value == null) {
382+
if (value == null) {
383383
return value
384384
}
385385

@@ -393,7 +393,7 @@ export function cast(field: Field, value: string | null): any {
393393
case 'UINT24':
394394
case 'UINT32':
395395
case 'YEAR':
396-
return parseInt(value, 10)
396+
return value ? parseInt(value, 10) : value
397397
case 'FLOAT32':
398398
case 'FLOAT64':
399399
return parseFloat(value)
@@ -412,7 +412,7 @@ export function cast(field: Field, value: string | null): any {
412412
case 'BINARY':
413413
return uint8Array(value)
414414
case 'JSON':
415-
return JSON.parse(decode(value))
415+
return value ? JSON.parse(decode(value)) : value
416416
default:
417417
return decode(value)
418418
}

0 commit comments

Comments
 (0)