@@ -11,6 +11,7 @@ const suite = (Buffer) =>
1111 hex . match ( / .{ 1 , 2 } / g) . map ( ( byte ) => parseInt ( byte , 16 ) )
1212 ) ;
1313 }
14+
1415 describe ( 'Array parser' , function ( ) {
1516 it ( 'should parse array of primitive types' , function ( ) {
1617 var parser = Parser . start ( ) . uint8 ( 'length' ) . array ( 'message' , {
@@ -64,16 +65,19 @@ const suite = (Buffer) =>
6465 } ) ;
6566 it ( 'should parse array of user defined types and have access to parent context' , function ( ) {
6667 var elementParser = new Parser ( ) . uint8 ( 'key' ) . array ( 'value' , {
67- type : " uint8" ,
68+ type : ' uint8' ,
6869 length : function ( ) {
6970 return this . $parent . valueLength ;
70- }
71+ } ,
7172 } ) ;
7273
73- var parser = Parser . start ( ) . uint16le ( 'length' ) . uint16le ( 'valueLength' ) . array ( 'message' , {
74- length : 'length' ,
75- type : elementParser ,
76- } ) ;
74+ var parser = Parser . start ( )
75+ . uint16le ( 'length' )
76+ . uint16le ( 'valueLength' )
77+ . array ( 'message' , {
78+ length : 'length' ,
79+ type : elementParser ,
80+ } ) ;
7781
7882 var buffer = Buffer . from ( [
7983 0x02 ,
@@ -97,17 +101,20 @@ const suite = (Buffer) =>
97101 } ) ;
98102 } ) ;
99103 it ( 'should parse array of user defined types and have access to root context' , function ( ) {
100- var elementParser = new Parser ( ) . uint8 ( 'key' ) . nest ( " data" , {
104+ var elementParser = new Parser ( ) . uint8 ( 'key' ) . nest ( ' data' , {
101105 type : new Parser ( ) . array ( 'value' , {
102- type : " uint8" ,
103- length : " $root.valueLength"
104- } )
106+ type : ' uint8' ,
107+ length : ' $root.valueLength' ,
108+ } ) ,
105109 } ) ;
106110
107- var parser = Parser . start ( ) . uint16le ( 'length' ) . uint16le ( 'valueLength' ) . array ( 'message' , {
108- length : 'length' ,
109- type : elementParser ,
110- } ) ;
111+ var parser = Parser . start ( )
112+ . uint16le ( 'length' )
113+ . uint16le ( 'valueLength' )
114+ . array ( 'message' , {
115+ length : 'length' ,
116+ type : elementParser ,
117+ } ) ;
111118
112119 var buffer = Buffer . from ( [
113120 0x02 ,
@@ -125,8 +132,8 @@ const suite = (Buffer) =>
125132 length : 0x02 ,
126133 valueLength : 0x02 ,
127134 message : [
128- { key : 0xca , data : { value : [ 0xd2 , 0x04 ] } } ,
129- { key : 0xbe , data : { value : [ 0xd3 , 0x04 ] } } ,
135+ { key : 0xca , data : { value : [ 0xd2 , 0x04 ] } } ,
136+ { key : 0xbe , data : { value : [ 0xd3 , 0x04 ] } } ,
130137 ] ,
131138 } ) ;
132139 } ) ;
@@ -474,7 +481,6 @@ const suite = (Buffer) =>
474481 ] ,
475482 } ) ;
476483 } ) ;
477-
478484 it ( 'should allow parent parser attributes as choice key' , function ( ) {
479485 var ChildParser = Parser . start ( ) . choice ( 'data' , {
480486 tag : function ( vars ) {
@@ -502,6 +508,93 @@ const suite = (Buffer) =>
502508 child : { data : { v2 : 0x0304 } } ,
503509 } ) ;
504510 } ) ;
511+ it ( 'should be able to access to index context variable when using length' , function ( ) {
512+ var elementParser = new Parser ( )
513+ . uint8 ( 'key' , {
514+ formatter : function ( item ) {
515+ return this . $index % 2 === 0 ? item : String . fromCharCode ( item ) ;
516+ } ,
517+ } )
518+ . nest ( 'data' , {
519+ type : new Parser ( ) . array ( 'value' , {
520+ type : 'uint8' ,
521+ length : '$root.valueLength' ,
522+ } ) ,
523+ } ) ;
524+
525+ var parser = Parser . start ( )
526+ . uint16le ( 'length' )
527+ . uint16le ( 'valueLength' )
528+ . array ( 'message' , {
529+ length : 'length' ,
530+ type : elementParser ,
531+ } ) ;
532+
533+ var buffer = Buffer . from ( [
534+ 0x02 ,
535+ 0x00 ,
536+ 0x02 ,
537+ 0x00 ,
538+ 0x50 ,
539+ 0xd2 ,
540+ 0x04 ,
541+ 0x51 ,
542+ 0xd3 ,
543+ 0x04 ,
544+ ] ) ;
545+ assert . deepStrictEqual ( parser . parse ( buffer ) , {
546+ length : 0x02 ,
547+ valueLength : 0x02 ,
548+ message : [
549+ { key : 0x50 , data : { value : [ 0xd2 , 0x04 ] } } ,
550+ { key : 'Q' , data : { value : [ 0xd3 , 0x04 ] } } ,
551+ ] ,
552+ } ) ;
553+ } ) ;
554+ it ( 'should be able to access to index context variable when using length on named parser' , function ( ) {
555+ var elementParser = new Parser ( )
556+ . uint8 ( 'key' , {
557+ formatter : function ( item ) {
558+ return this . $index % 2 === 0 ? item : String . fromCharCode ( item ) ;
559+ } ,
560+ } )
561+ . nest ( 'data' , {
562+ type : new Parser ( ) . array ( 'value' , {
563+ type : 'uint8' ,
564+ length : '$root.valueLength' ,
565+ } ) ,
566+ } )
567+ . namely ( 'ArrayLengthIndexTest' ) ;
568+
569+ var parser = Parser . start ( )
570+ . uint16le ( 'length' )
571+ . uint16le ( 'valueLength' )
572+ . array ( 'message' , {
573+ length : 'length' ,
574+ type : 'ArrayLengthIndexTest' ,
575+ } ) ;
576+
577+ var buffer = Buffer . from ( [
578+ 0x02 ,
579+ 0x00 ,
580+ 0x02 ,
581+ 0x00 ,
582+ 0x50 ,
583+ 0xd2 ,
584+ 0x04 ,
585+ 0x51 ,
586+ 0xd3 ,
587+ 0x04 ,
588+ ] ) ;
589+ assert . deepStrictEqual ( parser . parse ( buffer ) , {
590+ length : 0x02 ,
591+ valueLength : 0x02 ,
592+ message : [
593+ { key : 0x50 , data : { value : [ 0xd2 , 0x04 ] } } ,
594+ { key : 'Q' , data : { value : [ 0xd3 , 0x04 ] } } ,
595+ ] ,
596+ } ) ;
597+ } ) ;
505598 } ) ;
506599
507600 describe ( 'Choice parser' , function ( ) {
@@ -924,7 +1017,7 @@ const suite = (Buffer) =>
9241017 1 : Parser . start ( )
9251018 . uint8 ( 'length' )
9261019 . string ( 'message' , { length : 'length' } )
927- . array ( 'value' , { type : " uint8" , length : " $parent.items" } ) ,
1020+ . array ( 'value' , { type : ' uint8' , length : ' $parent.items' } ) ,
9281021 3 : Parser . start ( ) . int32le ( 'number' ) ,
9291022 } ,
9301023 } ) ;
@@ -1050,11 +1143,11 @@ const suite = (Buffer) =>
10501143 var parser = Parser . start ( )
10511144 . uint8 ( 'items' )
10521145 . nest ( 'data' , {
1053- type : Parser . start ( )
1054- . uint8 ( 'length' )
1055- . string ( 'message' , { length : 'length' } )
1056- . array ( 'value' , { type : " uint8" , length : " $parent.items" } ) ,
1057- } ) ;
1146+ type : Parser . start ( )
1147+ . uint8 ( 'length' )
1148+ . string ( 'message' , { length : 'length' } )
1149+ . array ( 'value' , { type : ' uint8' , length : ' $parent.items' } ) ,
1150+ } ) ;
10581151
10591152 var buffer = Buffer . from ( [
10601153 0x2 ,
@@ -1092,6 +1185,7 @@ const suite = (Buffer) =>
10921185 function Person ( ) {
10931186 this . name = '' ;
10941187 }
1188+
10951189 Person . prototype . toString = function ( ) {
10961190 return '[object Person]' ;
10971191 } ;
0 commit comments