@@ -62,6 +62,74 @@ const suite = (Buffer) =>
6262 ] ,
6363 } ) ;
6464 } ) ;
65+ it ( 'should parse array of user defined types and have access to parent context' , function ( ) {
66+ var elementParser = new Parser ( ) . uint8 ( 'key' ) . array ( 'value' , {
67+ type : "uint8" ,
68+ length : function ( ) {
69+ return this . $parent . valueLength ;
70+ }
71+ } ) ;
72+
73+ var parser = Parser . start ( ) . uint16le ( 'length' ) . uint16le ( 'valueLength' ) . array ( 'message' , {
74+ length : 'length' ,
75+ type : elementParser ,
76+ } ) ;
77+
78+ var buffer = Buffer . from ( [
79+ 0x02 ,
80+ 0x00 ,
81+ 0x02 ,
82+ 0x00 ,
83+ 0xca ,
84+ 0xd2 ,
85+ 0x04 ,
86+ 0xbe ,
87+ 0xd3 ,
88+ 0x04 ,
89+ ] ) ;
90+ assert . deepStrictEqual ( parser . parse ( buffer ) , {
91+ length : 0x02 ,
92+ valueLength : 0x02 ,
93+ message : [
94+ { key : 0xca , value : [ 0xd2 , 0x04 ] } ,
95+ { key : 0xbe , value : [ 0xd3 , 0x04 ] } ,
96+ ] ,
97+ } ) ;
98+ } ) ;
99+ it ( 'should parse array of user defined types and have access to root context' , function ( ) {
100+ var elementParser = new Parser ( ) . uint8 ( 'key' ) . nest ( "data" , {
101+ type : new Parser ( ) . array ( 'value' , {
102+ type : "uint8" ,
103+ length : "$root.valueLength"
104+ } )
105+ } ) ;
106+
107+ var parser = Parser . start ( ) . uint16le ( 'length' ) . uint16le ( 'valueLength' ) . array ( 'message' , {
108+ length : 'length' ,
109+ type : elementParser ,
110+ } ) ;
111+
112+ var buffer = Buffer . from ( [
113+ 0x02 ,
114+ 0x00 ,
115+ 0x02 ,
116+ 0x00 ,
117+ 0xca ,
118+ 0xd2 ,
119+ 0x04 ,
120+ 0xbe ,
121+ 0xd3 ,
122+ 0x04 ,
123+ ] ) ;
124+ assert . deepStrictEqual ( parser . parse ( buffer ) , {
125+ length : 0x02 ,
126+ valueLength : 0x02 ,
127+ message : [
128+ { key : 0xca , data : { value : [ 0xd2 , 0x04 ] } } ,
129+ { key : 0xbe , data : { value : [ 0xd3 , 0x04 ] } } ,
130+ ] ,
131+ } ) ;
132+ } ) ;
65133 it ( 'should parse array of user defined types with lengthInBytes' , function ( ) {
66134 var elementParser = new Parser ( ) . uint8 ( 'key' ) . int16le ( 'value' ) ;
67135
@@ -493,7 +561,7 @@ const suite = (Buffer) =>
493561 test : 314159 ,
494562 } ) ;
495563 } ) ;
496- it ( 'should parse choices of user defied types' , function ( ) {
564+ it ( 'should parse choices of user defined types' , function ( ) {
497565 var parser = Parser . start ( )
498566 . uint8 ( 'tag' )
499567 . choice ( 'data' , {
@@ -761,7 +829,7 @@ const suite = (Buffer) =>
761829 number : 12345678 ,
762830 } ) ;
763831 } ) ;
764- it ( "should be able to 'flatten' choices when omitting varName paramater " , function ( ) {
832+ it ( "should be able to 'flatten' choices when omitting varName parameter " , function ( ) {
765833 var parser = Parser . start ( )
766834 . uint8 ( 'tag' )
767835 . choice ( {
@@ -846,6 +914,60 @@ const suite = (Buffer) =>
846914 number : 12345678 ,
847915 } ) ;
848916 } ) ;
917+ it ( 'should be able to use parsing context' , function ( ) {
918+ var parser = Parser . start ( )
919+ . uint8 ( 'tag' )
920+ . uint8 ( 'items' )
921+ . choice ( 'data' , {
922+ tag : 'tag' ,
923+ choices : {
924+ 1 : Parser . start ( )
925+ . uint8 ( 'length' )
926+ . string ( 'message' , { length : 'length' } )
927+ . array ( 'value' , { type : "uint8" , length : "$parent.items" } ) ,
928+ 3 : Parser . start ( ) . int32le ( 'number' ) ,
929+ } ,
930+ } ) ;
931+
932+ var buffer = Buffer . from ( [
933+ 0x1 ,
934+ 0x2 ,
935+ 0xc ,
936+ 0x68 ,
937+ 0x65 ,
938+ 0x6c ,
939+ 0x6c ,
940+ 0x6f ,
941+ 0x2c ,
942+ 0x20 ,
943+ 0x77 ,
944+ 0x6f ,
945+ 0x72 ,
946+ 0x6c ,
947+ 0x64 ,
948+ 0x01 ,
949+ 0x02 ,
950+ 0x02 ,
951+ 0x02 ,
952+ ] ) ;
953+ assert . deepStrictEqual ( parser . parse ( buffer ) , {
954+ tag : 1 ,
955+ items : 2 ,
956+ data : {
957+ length : 12 ,
958+ message : 'hello, world' ,
959+ value : [ 0x01 , 0x02 ] ,
960+ } ,
961+ } ) ;
962+ buffer = Buffer . from ( [ 0x03 , 0x0 , 0x4e , 0x61 , 0xbc , 0x00 ] ) ;
963+ assert . deepStrictEqual ( parser . parse ( buffer ) , {
964+ tag : 3 ,
965+ items : 0 ,
966+ data : {
967+ number : 12345678 ,
968+ } ,
969+ } ) ;
970+ } ) ;
849971 } ) ;
850972
851973 describe ( 'Nest parser' , function ( ) {
@@ -923,6 +1045,46 @@ const suite = (Buffer) =>
9231045
9241046 assert . deepStrictEqual ( parser . parse ( buf ) , { s1 : 'foo' , s2 : 'bar' } ) ;
9251047 } ) ;
1048+
1049+ it ( 'should be able to use parsing context' , function ( ) {
1050+ var parser = Parser . start ( )
1051+ . uint8 ( 'items' )
1052+ . nest ( 'data' , {
1053+ type : Parser . start ( )
1054+ . uint8 ( 'length' )
1055+ . string ( 'message' , { length : 'length' } )
1056+ . array ( 'value' , { type : "uint8" , length : "$parent.items" } ) ,
1057+ } ) ;
1058+
1059+ var buffer = Buffer . from ( [
1060+ 0x2 ,
1061+ 0xc ,
1062+ 0x68 ,
1063+ 0x65 ,
1064+ 0x6c ,
1065+ 0x6c ,
1066+ 0x6f ,
1067+ 0x2c ,
1068+ 0x20 ,
1069+ 0x77 ,
1070+ 0x6f ,
1071+ 0x72 ,
1072+ 0x6c ,
1073+ 0x64 ,
1074+ 0x01 ,
1075+ 0x02 ,
1076+ 0x02 ,
1077+ 0x02 ,
1078+ ] ) ;
1079+ assert . deepStrictEqual ( parser . parse ( buffer ) , {
1080+ items : 2 ,
1081+ data : {
1082+ length : 12 ,
1083+ message : 'hello, world' ,
1084+ value : [ 0x01 , 0x02 ] ,
1085+ } ,
1086+ } ) ;
1087+ } ) ;
9261088 } ) ;
9271089
9281090 describe ( 'Constructors' , function ( ) {
0 commit comments