Skip to content

Commit fba307b

Browse files
committed
feat: Handle root-level arrays
1 parent 2e72189 commit fba307b

3 files changed

Lines changed: 47 additions & 4 deletions

File tree

src/addon_decode.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,6 @@ decode(napi_env env, napi_callback_info info) {
204204
return NULL;
205205
}
206206

207-
enum lite3_type root_type = (enum lite3_type)*(ctx-> buf);
208-
209207
// Decode the buffer into a napi_value:
210208
napi_value result;
211209
NAPI_CALL(
@@ -215,7 +213,7 @@ decode(napi_env env, napi_callback_info info) {
215213
env, // napi_env
216214
ctx, // lite3_ctx*
217215
0, // offset (0 == root)
218-
LITE3_TYPE_OBJECT, // type: TODO: determine dynamically
216+
lite3_ctx_get_root_type(ctx), // type
219217
NULL, // key (NULL == root, or not an object)
220218
-1, // index (-1 == not an array index)
221219
&result // [out] result

test/encode-decode.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,49 @@ describe('encode/decode roundtrip', () => {
105105
};
106106
expect(decode(encode(obj))).toEqual(obj);
107107
});
108+
});
109+
110+
describe('root-level arrays', () => {
111+
it('handles simple arrays', () => {
112+
const arr = [1, 2, 3];
113+
expect(decode(encode(arr))).toEqual(arr);
114+
});
115+
116+
it('handles empty arrays', () => {
117+
const arr: unknown[] = [];
118+
expect(decode(encode(arr))).toEqual(arr);
119+
});
120+
121+
it('handles arrays with strings', () => {
122+
const arr = ['hello', 'world'];
123+
expect(decode(encode(arr))).toEqual(arr);
124+
});
125+
126+
it('handles arrays with mixed types', () => {
127+
const arr = [1, 'two', true, null, 3.14];
128+
expect(decode(encode(arr))).toEqual(arr);
129+
});
130+
131+
it('handles nested arrays', () => {
132+
const arr = [[1, 2], [3, 4], [5, 6]];
133+
expect(decode(encode(arr))).toEqual(arr);
134+
});
135+
136+
it('handles arrays with objects', () => {
137+
const arr = [{ a: 1 }, { b: 2 }, { c: 3 }];
138+
expect(decode(encode(arr))).toEqual(arr);
139+
});
140+
141+
it('handles deeply nested structures in arrays', () => {
142+
const arr = [
143+
{ user: { name: 'Alice', tags: ['admin', 'active'] } },
144+
{ user: { name: 'Bob', tags: ['guest'] } }
145+
];
146+
expect(decode(encode(arr))).toEqual(arr);
147+
});
148+
149+
it('handles arrays with unicode', () => {
150+
const arr = ['🎉', '中文', 'hello 世界'];
151+
expect(decode(encode(arr))).toEqual(arr);
152+
});
108153
});

0 commit comments

Comments
 (0)