Skip to content

Commit d7078bb

Browse files
authored
Merge pull request keichi#67 from Ericbla/PR_flatten_choice
Flattened data stucture for "choice" types
2 parents b698f9e + 00b1ad6 commit d7078bb

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

lib/binary_parser.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ Parser.prototype.array = function(varName, options) {
166166
};
167167

168168
Parser.prototype.choice = function(varName, options) {
169+
if (arguments.length == 1 && typeof varName === "object") {
170+
options = varName;
171+
varName = null;
172+
}
169173
if (!options.tag) {
170174
throw new Error("Tag option of array is not defined.");
171175
}

test/composite_parser.js

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,131 @@ describe("Composite parser", function() {
702702
}
703703
});
704704
});
705+
it("should be able to 'flatten' choices when using null varName", function() {
706+
var parser = Parser.start()
707+
.uint8("tag")
708+
.choice(null, {
709+
tag: "tag",
710+
choices: {
711+
1: Parser.start()
712+
.uint8("length")
713+
.string("message", { length: "length" }),
714+
3: Parser.start().int32le("number")
715+
}
716+
});
717+
718+
var buffer = Buffer.from([
719+
0x1,
720+
0xc,
721+
0x68,
722+
0x65,
723+
0x6c,
724+
0x6c,
725+
0x6f,
726+
0x2c,
727+
0x20,
728+
0x77,
729+
0x6f,
730+
0x72,
731+
0x6c,
732+
0x64
733+
]);
734+
assert.deepEqual(parser.parse(buffer), {
735+
tag: 1,
736+
length: 12,
737+
message: "hello, world"
738+
});
739+
buffer = Buffer.from([0x03, 0x4e, 0x61, 0xbc, 0x00]);
740+
assert.deepEqual(parser.parse(buffer), {
741+
tag: 3,
742+
number: 12345678
743+
});
744+
});
745+
it("should be able to 'flatten' choices when omitting varName paramater", function() {
746+
var parser = Parser.start()
747+
.uint8("tag")
748+
.choice({
749+
tag: "tag",
750+
choices: {
751+
1: Parser.start()
752+
.uint8("length")
753+
.string("message", { length: "length" }),
754+
3: Parser.start().int32le("number")
755+
}
756+
});
757+
758+
var buffer = Buffer.from([
759+
0x1,
760+
0xc,
761+
0x68,
762+
0x65,
763+
0x6c,
764+
0x6c,
765+
0x6f,
766+
0x2c,
767+
0x20,
768+
0x77,
769+
0x6f,
770+
0x72,
771+
0x6c,
772+
0x64
773+
]);
774+
assert.deepEqual(parser.parse(buffer), {
775+
tag: 1,
776+
length: 12,
777+
message: "hello, world"
778+
});
779+
buffer = Buffer.from([0x03, 0x4e, 0x61, 0xbc, 0x00]);
780+
assert.deepEqual(parser.parse(buffer), {
781+
tag: 3,
782+
number: 12345678
783+
});
784+
});
785+
it("should be able to use function as the choice selector", function() {
786+
var parser = Parser.start()
787+
.string("selector", { length: 4 })
788+
.choice(null, {
789+
tag: function() {
790+
return parseInt(this.selector, 2); // string base 2 to integer decimal
791+
},
792+
choices: {
793+
2: Parser.start()
794+
.uint8("length")
795+
.string("message", { length: "length" }),
796+
7: Parser.start().int32le("number")
797+
}
798+
});
799+
800+
var buffer = Buffer.from([
801+
48,
802+
48,
803+
49,
804+
48,
805+
0xc,
806+
0x68,
807+
0x65,
808+
0x6c,
809+
0x6c,
810+
0x6f,
811+
0x2c,
812+
0x20,
813+
0x77,
814+
0x6f,
815+
0x72,
816+
0x6c,
817+
0x64
818+
]);
819+
assert.deepEqual(parser.parse(buffer), {
820+
selector: "0010", // -> choice 2
821+
length: 12,
822+
message: "hello, world"
823+
});
824+
buffer = Buffer.from([48, 49, 49, 49, 0x4e, 0x61, 0xbc, 0x00]);
825+
assert.deepEqual(parser.parse(buffer), {
826+
selector: "0111", // -> choice 7
827+
number: 12345678
828+
});
829+
});
705830
});
706831

707832
describe("Nest parser", function() {

0 commit comments

Comments
 (0)