Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions spec/arrayNodeName_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import XMLBuilder from '../src/fxb.js';

describe('Top-level array node names', function () {
// 2026-10-11: A valid one-character name must wrap each array item.
it('uses a single-character arrayNodeName', function () {
const builder = new XMLBuilder({ arrayNodeName: 'x' });
expect(builder.build(['a', 'b'])).toEqual('<x>a</x><x>b</x>');
expect(builder.build([{ value: 1 }])).toEqual('<x><value>1</value></x>');
});

it('keeps longer names and unnamed arrays unchanged', function () {
expect(new XMLBuilder({ arrayNodeName: 'item' }).build(['a']))
.toEqual('<item>a</item>');
expect(new XMLBuilder({ arrayNodeName: '' }).build(['a']))
.toEqual('<0>a</0>');
});
});
2 changes: 1 addition & 1 deletion src/fxb.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ Builder.prototype.build = function (jObj) {
if (this.options.preserveOrder) {
return buildFromOrderedJs(jObj, this.options);
} else {
if (Array.isArray(jObj) && this.options.arrayNodeName && this.options.arrayNodeName.length > 1) {
if (Array.isArray(jObj) && this.options.arrayNodeName) {
jObj = {
[this.options.arrayNodeName]: jObj
}
Expand Down
Loading