Skip to content

Commit

Permalink
fix for zero-length data
Browse files Browse the repository at this point in the history
  • Loading branch information
itanka9 committed Dec 18, 2024
1 parent bfb1635 commit 4f11bf8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 26 deletions.
21 changes: 0 additions & 21 deletions src/json2pbf.d.ts

This file was deleted.

6 changes: 3 additions & 3 deletions src/json2pbf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export function packJson(val: any, options?: PackOptions): ArrayBuffer {
}
const fcol = Object.keys(val)[0];
const clen = val?.[fcol]?.length;
if (!clen) {
if (clen === undefined) {
throw new Error('Cannot determine columnar data length');
}
pbf.writeFixed32(clen);
Expand All @@ -219,8 +219,8 @@ export function packJson(val: any, options?: PackOptions): ArrayBuffer {
throw new Error('No columns');
}
const rlen = val?.length;
if (!rlen) {
throw new Error('Cannot determine columnar data length');
if (rlen === undefined) {
throw new Error('Cannot determine row data length');
}
pbf.writeFixed32(rlen);
writeColumns(columns, pbf);
Expand Down
39 changes: 37 additions & 2 deletions tests/json2pbf.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { test, expect } from '@playwright/test';
import { codecs } from '../demo/config';
import { packJson, unpackJson } from 'src/json2pbf';
import { existsSync, readFileSync, writeFileSync } from 'fs';
import { JsonType, packJson, PackMethod, PackOptions, unpackJson } from 'src/json2pbf';
import { readFileSync } from 'fs';

type Configuration = { arraySize: number, iterations: number };

Expand Down Expand Up @@ -49,6 +49,41 @@ test.describe('basic cases', () => {
});
}

const rowCases = [
[],
[{ id: 'foo', hidden: 1 }],
[
{ id: 'foo', hidden: 1 },
{ id: 'bar', hidden: 0 }
],
];

for (const content of rowCases) {
const packOptions: PackOptions = {
method: PackMethod.Row,
columns: { id: JsonType.String, hidden: JsonType.Number }
};
test(`row pack(unpack(${JSON.stringify(content)})) === ${JSON.stringify(content)}`, () => {
expect(unpackJson(packJson(content, packOptions))).toStrictEqual(content)
});
}

const columnCases = [
{ id: [], hidden: [] },
{ id: ['foo'], hidden: [1] },
{ id: ['foo', 'bar'], hidden: [1, 0] }
];

for (const content of columnCases) {
const packOptions: PackOptions = {
method: PackMethod.Columnar,
columns: { id: JsonType.String, hidden: JsonType.Number }
};
test(`col pack(unpack(${JSON.stringify(content)})) === ${JSON.stringify(content)}`, () => {
expect(unpackJson(packJson(content, packOptions))).toStrictEqual(content)
});
}

const versions = [1];

const serializeCases = [
Expand Down

0 comments on commit 4f11bf8

Please sign in to comment.