diff --git a/package.json b/package.json index 4899803..32c40ca 100644 --- a/package.json +++ b/package.json @@ -56,10 +56,10 @@ }, "devDependencies": { "@babel/eslint-parser": "7.28.0", - "@types/node": "24.0.10", + "@types/node": "24.1.0", "@vitest/coverage-v8": "3.2.4", - "eslint": "9.30.1", - "eslint-plugin-jsdoc": "51.3.3", + "eslint": "9.32.0", + "eslint-plugin-jsdoc": "52.0.0", "typescript": "5.8.3", "vitest": "3.2.4" } diff --git a/src/unconvert.js b/src/unconvert.js index 5b2c90c..730e49a 100644 --- a/src/unconvert.js +++ b/src/unconvert.js @@ -30,7 +30,7 @@ export function unconvert(element, values) { if (ctype === 'JSON') { if (!Array.isArray(values)) throw new Error('JSON must be an array') const encoder = new TextEncoder() - return values.map(v => encoder.encode(JSON.stringify(v))) + return values.map(v => v === undefined ? undefined : encoder.encode(JSON.stringify(v))) } if (ctype === 'UTF8') { if (!Array.isArray(values)) throw new Error('strings must be an array') diff --git a/test/unconvert.test.js b/test/unconvert.test.js index 4f28595..57db79a 100644 --- a/test/unconvert.test.js +++ b/test/unconvert.test.js @@ -28,6 +28,20 @@ describe('unconvert', () => { expect(new TextDecoder().decode(result[1])).toEqual(JSON.stringify({ hello: 'world' })) }) + it('should handle undefined values in JSON arrays', () => { + /** @type {SchemaElement} */ + const schema = { name: 'test', converted_type: 'JSON' } + const input = [{ foo: 'bar' }, undefined, { hello: 'world' }] + const result = unconvert(schema, input) + + expect(result).toHaveLength(3) + expect(result[0]).toBeInstanceOf(Uint8Array) + expect(result[1]).toBeUndefined() + expect(result[2]).toBeInstanceOf(Uint8Array) + expect(new TextDecoder().decode(result[0])).toEqual(JSON.stringify({ foo: 'bar' })) + expect(new TextDecoder().decode(result[2])).toEqual(JSON.stringify({ hello: 'world' })) + }) + it('should convert string array to Uint8Array when converted_type = UTF8', () => { /** @type {SchemaElement} */ const schema = { name: 'test', converted_type: 'UTF8' }