Skip to content

Commit 52e5b1a

Browse files
committed
Allow omitting var name in nest parser
1 parent d7078bb commit 52e5b1a

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

lib/binary_parser.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,14 @@ Parser.prototype.choice = function(varName, options) {
170170
options = varName;
171171
varName = null;
172172
}
173+
173174
if (!options.tag) {
174175
throw new Error("Tag option of array is not defined.");
175176
}
176177
if (!options.choices) {
177178
throw new Error("Choices option of array is not defined.");
178179
}
180+
179181
Object.keys(options.choices).forEach(function(key) {
180182
if (isNaN(parseInt(key, 10))) {
181183
throw new Error("Key of choices must be a number.");
@@ -203,13 +205,22 @@ Parser.prototype.choice = function(varName, options) {
203205
};
204206

205207
Parser.prototype.nest = function(varName, options) {
208+
if (arguments.length == 1 && typeof varName === "object") {
209+
options = varName;
210+
varName = null;
211+
}
212+
206213
if (!options.type) {
207214
throw new Error("Type option of nest is not defined.");
208215
}
209-
210216
if (!(options.type instanceof Parser) && !aliasRegistry[options.type]) {
211217
throw new Error("Type option of nest must be a Parser object.");
212218
}
219+
if (!(options.type instanceof Parser) && !varName) {
220+
throw new Error(
221+
"options.type must be a object if variable name is omitted."
222+
);
223+
}
213224

214225
return this.setNextParser("nest", varName, options);
215226
};
@@ -701,8 +712,11 @@ Parser.prototype.generateChoice = function(ctx) {
701712

702713
Parser.prototype.generateNest = function(ctx) {
703714
var nestVar = ctx.generateVariable(this.varName);
715+
704716
if (this.options.type instanceof Parser) {
705-
ctx.pushCode("{0} = {};", nestVar);
717+
if (this.varName) {
718+
ctx.pushCode("{0} = {};", nestVar);
719+
}
706720
ctx.pushPath(this.varName);
707721
this.options.type.generate(ctx);
708722
ctx.popPath(this.varName);

test/composite_parser.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,28 @@ describe("Composite parser", function() {
882882
name: "John Doe"
883883
});
884884
});
885+
886+
it("should 'flatten' output when using null varName", function() {
887+
var parser = new Parser()
888+
.string("s1", { zeroTerminated: true })
889+
.nest(null, {
890+
type: new Parser().string("s2", { zeroTerminated: true })
891+
});
892+
893+
var buf = Buffer.from("foo\0bar\0");
894+
895+
assert.deepEqual(parser.parse(buf), { s1: "foo", s2: "bar" });
896+
});
897+
898+
it("should 'flatten' output when omitting varName", function() {
899+
var parser = new Parser().string("s1", { zeroTerminated: true }).nest({
900+
type: new Parser().string("s2", { zeroTerminated: true })
901+
});
902+
903+
var buf = Buffer.from("foo\0bar\0");
904+
905+
assert.deepEqual(parser.parse(buf), { s1: "foo", s2: "bar" });
906+
});
885907
});
886908

887909
describe("Buffer parser", function() {

0 commit comments

Comments
 (0)