Skip to content

Commit

Permalink
modify: error when buf size is zero
Browse files Browse the repository at this point in the history
  • Loading branch information
ykisii committed Apr 4, 2023
1 parent 4984a8c commit 71a05ed
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
5 changes: 4 additions & 1 deletion binary_to_json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ export class BinaryToJSON {

private bufferToJSON(br: BinaryReader, buf: Uint8Array, formats:any[]): {} {
// needs array. when read binary sequentially.
if (Array.isArray(formats) === false) return {};
if (Array.isArray(formats) === false ||
buf.length === 0) {
return {};
}

const output: Obj = {};
const array: object[] = [];
Expand Down
13 changes: 13 additions & 0 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { assertEquals, assertNotEquals } from "https://deno.land/std/testing/ass
//import { BinaryToJSON } from "https://deno.land/x/binary_to_json@v0.1.0/mod.ts";
import { BinaryToJSON } from "./binary_to_json.ts";


Deno.test(
"constructor",
function(): void {
Expand Down Expand Up @@ -79,4 +80,16 @@ Deno.test(
console.log(data);
assertEquals(0x00010203, data['dat']);
},
);

Deno.test(
"convert array null",
function(): void {
const arry = new Uint8Array();
const format = [{"dat":4}];
const b2j = new BinaryToJSON();
const data: any = b2j.convert(arry, format);
console.log(data);
assertEquals({}, data);
},
);

0 comments on commit 71a05ed

Please sign in to comment.