2424 */
2525
2626
27- var cppMsg = module . exports ;
28- var iconv = require ( 'iconv-lite' ) ;
27+ let cppMsg = module . exports ;
28+ const iconv = require ( 'iconv-lite' ) ;
2929
3030
3131///////////// cppString defined
@@ -40,7 +40,7 @@ function CppString(str, len) {
4040 this . str += "\0" ;
4141
4242 this . byteLen = len ;
43- this . buffer = new Buffer ( this . byteLen ) ;
43+ this . buffer = Buffer . alloc ( this . byteLen ) ;
4444 this . length = this . buffer . length ;
4545
4646 this . process ( ) ;
@@ -58,7 +58,7 @@ CppString.prototype.getLength = function () {
5858CppString . prototype . process = function ( ) {
5959 this . buffer . fill ( 0 ) ;
6060 this . buffer . write ( this . str ) ;
61- /*for(var i=this.str.length;i < this.buffer.length;i++){
61+ /*for(let i=this.str.length;i < this.buffer.length;i++){
6262 this.buffer[i] = 0x00;
6363 }*/
6464} ;
@@ -109,8 +109,7 @@ function CppNum(num, intType) {
109109CppNum . prototype . process = function ( ) {
110110
111111 this . value = this . numArray [ 0 ] ;
112- this . buffer = new Buffer ( this . byteLen ) ;
113- this . buffer . fill ( 0 ) ; //clear all the buffer
112+ this . buffer = Buffer . alloc ( this . byteLen ) ;
114113
115114 switch ( this . intType ) {
116115 case "uint8" ://uint8
@@ -137,17 +136,13 @@ CppNum.prototype.process = function () {
137136} ;
138137
139138
140- function isArray ( obj ) {
141- return ( Object . prototype . toString . call ( obj ) === '[object Array]' ) ;
142- }
143-
144139function isObject ( obj ) {
145140 return ( Object . prototype . toString . call ( obj ) === '[object Object]' ) ;
146141}
147142
148143
149144// 基本数据类型定义
150- var DataType = {
145+ const DataType = {
151146 int8 : 0 ,
152147 uint8 : 1 ,
153148 int16 : 2 ,
@@ -162,7 +157,7 @@ var DataType = {
162157 object : 11
163158} ;
164159
165- var DataTypeLen = [ 1 , 1 , 2 , 2 , 4 , 4 , 8 , 4 , 8 , 1 , 0 ] ;
160+ const DataTypeLen = [ 1 , 1 , 2 , 2 , 4 , 4 , 8 , 4 , 8 , 1 , 0 ] ;
166161
167162/**
168163 ds = [{<name>:[<type>,[len],[arraylen]]}]
@@ -181,7 +176,7 @@ cppMsg.msg = function (ds, data) {
181176 this . dsDecode = [ ] ; // 解码使用的结构 [<offset>,<datalen>,<dataType>,<name>]
182177 this . dsLen = 0 ;
183178
184- var ret = this . phraseDS ( ds ) ;
179+ const ret = this . phraseDS ( ds ) ;
185180 if ( ret !== false ) {
186181 this . dsLen = ret [ 0 ] ;
187182 this . dsEncode = ret [ 1 ] ;
@@ -200,23 +195,22 @@ cppMsg.msg = function (ds, data) {
200195} ;
201196
202197cppMsg . msg . prototype . phraseDS = function ( ds ) {
203- if ( isArray ( ds ) ) {
204- var len = ds . length ;
205- var offset = 0 ;
206- var i = 0 ;
207- var it = null ;
208- var dataType = DataType . int8 ;
209- var dataLen = 1 ;
210- var arrayLen = 1 ;
211-
212- var dsLen = 0 ;
213- var dsEncode = { } ; // 编码使用结构 { name:[<dataType>,<offset>,[len]] }
214- var dsDecode = [ ] ; // 解码使用的结构 [<offset>,<datalen>,<dataType>,<name>]
215-
216- for ( i = 0 ; i < len ; i ++ ) {
217- var it = ds [ i ] ;
218-
219- if ( isArray ( it ) && it . length >= 2 ) {
198+ if ( Array . isArray ( ds ) ) {
199+ let len = ds . length ;
200+ let offset = 0 ;
201+ let it = null ;
202+ let dataType = DataType . int8 ;
203+ let dataLen = 1 ;
204+ let arrayLen = 1 ;
205+
206+ let dsLen = 0 ;
207+ let dsEncode = { } ; // 编码使用结构 { name:[<dataType>,<offset>,[len]] }
208+ let dsDecode = [ ] ; // 解码使用的结构 [<offset>,<datalen>,<dataType>,<name>]
209+
210+ for ( let i = 0 ; i < len ; i ++ ) {
211+ let it = ds [ i ] ;
212+
213+ if ( Array . isArray ( it ) && it . length >= 2 ) {
220214 switch ( it [ 1 ] ) {
221215 case 'int8' :
222216 dataType = DataType . int8 ;
@@ -259,8 +253,8 @@ cppMsg.msg.prototype.phraseDS = function (ds) {
259253 break ;
260254 }
261255
262- var enAddin = null ;
263- var deAddin = null ;
256+ let enAddin = null ;
257+ let deAddin = null ;
264258 if ( dataType === - 1 ) {
265259 throw Error ( ' cppType.msg ds phrase error ' ) ;
266260 } else {
@@ -280,7 +274,7 @@ cppMsg.msg.prototype.phraseDS = function (ds) {
280274 }
281275 else if ( dataType === DataType . object ) { // 对象
282276 dataLen = - 1 ;
283- var ret = this . phraseDS ( it [ 2 ] ) ;
277+ let ret = this . phraseDS ( it [ 2 ] ) ;
284278 if ( ret !== false ) {
285279 //console.log('ret-------- testObj', ret );
286280 dataLen = ret [ 0 ] ;
@@ -318,57 +312,57 @@ cppMsg.msg.prototype.phraseDS = function (ds) {
318312
319313
320314cppMsg . msg . prototype . push_uint8 = function ( value ) {
321- var uint8Value = new CppNum ( value , "uint8" ) ;
315+ let uint8Value = new CppNum ( value , "uint8" ) ;
322316 this . listBuffer . push ( uint8Value . buffer ) ;
323317 this . length += uint8Value . byteLen ;
324318} ;
325319
326320cppMsg . msg . prototype . push_int8 = function ( value ) {
327- var int8Value = new CppNum ( value , "int8" ) ;
321+ let int8Value = new CppNum ( value , "int8" ) ;
328322 this . listBuffer . push ( int8Value . buffer ) ;
329323 this . length += int8Value . byteLen ;
330324} ;
331325
332326cppMsg . msg . prototype . push_uint16 = function ( value ) {
333- var uint16Value = new CppNum ( value , "uint16" ) ;
327+ let uint16Value = new CppNum ( value , "uint16" ) ;
334328 this . listBuffer . push ( uint16Value . buffer ) ;
335329 this . length += uint16Value . byteLen ;
336330} ;
337331
338332cppMsg . msg . prototype . push_int16 = function ( value ) {
339- var int16Value = new CppNum ( value , "int16" ) ;
333+ let int16Value = new CppNum ( value , "int16" ) ;
340334 this . listBuffer . push ( int16Value . buffer ) ;
341335 this . length += int16Value . byteLen ;
342336} ;
343337
344338cppMsg . msg . prototype . push_uint32 = function ( value ) {
345- var uint32Value = new CppNum ( value , "uint32" ) ;
339+ let uint32Value = new CppNum ( value , "uint32" ) ;
346340 this . listBuffer . push ( uint32Value . buffer ) ;
347341 this . length += uint32Value . byteLen ;
348342} ;
349343
350344cppMsg . msg . prototype . push_int32 = function ( value ) {
351- var int32Value = new CppNum ( value , "int32" ) ;
345+ let int32Value = new CppNum ( value , "int32" ) ;
352346 this . listBuffer . push ( int32Value . buffer ) ;
353347 this . length += int32Value . byteLen ;
354348} ;
355349
356350cppMsg . msg . prototype . push_string = function ( strValue , len ) {
357- var strValue1 = new CppString ( strValue , len ) ;
351+ let strValue1 = new CppString ( strValue , len ) ;
358352 this . listBuffer . push ( strValue1 . buffer ) ;
359353 this . length += strValue1 . byteLen ;
360354} ;
361355
362356cppMsg . msg . prototype . push_char = function ( strChar ) {
363- var strValue = new CppString ( strValue , 2 ) ;
357+ let strValue = new CppString ( strValue , 2 ) ;
364358 this . listBuffer . push ( strValue . buffer ) ;
365359 this . length += strValue . byteLen ;
366360} ;
367361
368362cppMsg . msg . prototype . encode = function ( data ) {
369363
370364 if ( isObject ( data ) ) {
371- var msgBuf = this . encodeMsg ( data ) ;
365+ let msgBuf = this . encodeMsg ( data ) ;
372366 this . listBuffer . push ( msgBuf ) ;
373367 this . length += this . listBuffer . length ;
374368 }
@@ -394,30 +388,28 @@ cppMsg.msg.prototype.encodeMsg = function (data) {
394388}
395389
396390function readFunc ( f , arrayLen , off , datalen ) {
397- var res = [ ] ;
391+ let res = [ ] ;
398392 if ( arrayLen <= 1 )
399393 res = f ( off ) ;
400394 else
401- for ( var i = 0 ; i < arrayLen ; i ++ ) {
395+ for ( let i = 0 ; i < arrayLen ; i ++ ) {
402396 res . push ( f ( off ) ) ;
403397 off += datalen ;
404398 }
405399 return res ;
406400}
407401
408402function decodeObject ( buf , offset , dsDecode ) {
409- var data = { } ;
403+ let data = { } ;
410404 // [<offset>,<datalen>,<dataType>,<name>]
411- var len = dsDecode . length ;
412- var i = 0 , off = 0 ;
413- var info = null ;
414- for ( i = 0 ; i < len ; i ++ ) {
415- info = dsDecode [ i ] ;
416- off = info [ 0 ] + offset ;
417- var key = info [ 3 ] ;
418- var arrayLen = info [ 5 ] ;
419- var values = [ ] ;
420- for ( var arri = 0 ; arri < arrayLen ; arri ++ ) {
405+ let len = dsDecode . length ;
406+ for ( let i = 0 ; i < dsDecode . length ; i ++ ) {
407+ let info = dsDecode [ i ] ;
408+ let off = info [ 0 ] + offset ;
409+ let key = info [ 3 ] ;
410+ let arrayLen = info [ 5 ] ;
411+ let values = [ ] ;
412+ for ( let arri = 0 ; arri < arrayLen ; arri ++ ) {
421413 if ( off >= buf . length ) continue ;
422414 switch ( info [ 2 ] ) {
423415 case DataType . int8 :
@@ -430,8 +422,8 @@ function decodeObject(buf, offset, dsDecode) {
430422 values . push ( buf . readInt32LE ( off ) ) ;
431423 break ;
432424 case DataType . int64 :
433- var high = buf . readUInt32LE ( off ) ;
434- var low = buf . readUInt32LE ( off + 4 ) ;
425+ let high = buf . readUInt32LE ( off ) ;
426+ let low = buf . readUInt32LE ( off + 4 ) ;
435427 values . push ( low * 0x100000000 + high ) ;
436428 break ;
437429 case DataType . uint8 :
@@ -455,7 +447,7 @@ function decodeObject(buf, offset, dsDecode) {
455447 break ;
456448 case DataType . string : {
457449 //values = buf.toString(undefined, off, off+info[1]-1 );
458- var val = iconv . decode ( buf . slice ( off , off + info [ 1 ] - 1 ) , info [ 4 ] ) ;
450+ let val = iconv . decode ( buf . slice ( off , off + info [ 1 ] - 1 ) , info [ 4 ] ) ;
459451 values . push ( val . replace ( / \0 [ \s \S ] * / g, '' ) ) ;
460452 }
461453 break ;
@@ -473,21 +465,20 @@ function decodeObject(buf, offset, dsDecode) {
473465}
474466
475467function encodeObject ( data , dsLen , dsEncode ) {
476- var key = '' ;
477- var keyInfo = null ;
478-
479- var msgBuf = new Buffer ( dsLen ) ;
480- msgBuf . fill ( 0 ) ;
468+ let keyInfo = null ;
469+ let msgBuf = Buffer . alloc ( dsLen ) ;
481470
482- for ( var p in data ) {
483- key = p ;
484- keyInfo = dsEncode [ key ] ; // { name:[<dataType>,<offset>,[len],[arraylen]] }
471+ for ( let p in data ) {
472+ keyInfo = dsEncode [ p ] ; // { name:[<dataType>,<offset>,[len],[arraylen]] }
485473 if ( keyInfo === undefined ) {
486474 continue ;
487475 }
488- var out = Array . isArray ( data [ p ] ) ? data [ p ] : [ data [ p ] ] ;
489- var off = keyInfo [ 1 ] ;
490- out . map ( function ( x ) {
476+ let out = Array . isArray ( data [ p ] ) ? data [ p ] : [ data [ p ] ] ;
477+ let off = keyInfo [ 1 ] ;
478+
479+ for ( let idx = 0 ; idx < out . length ; idx ++ )
480+ {
481+ let x = out [ idx ] ;
491482 switch ( keyInfo [ 0 ] ) {
492483 case DataType . int8 :
493484 msgBuf . writeInt8 ( x , off ) ;
@@ -524,8 +515,8 @@ function encodeObject(data, dsLen, dsEncode) {
524515 msgBuf . writeUInt8 ( x ? 1 : 0 , off ) ;
525516 break ;
526517 case DataType . string :
527- var strLen = keyInfo [ 2 ] ;
528- var str = '' ;
518+ let strLen = keyInfo [ 2 ] ;
519+ let str = '' ;
529520 if ( x . length > strLen - 1 ) {
530521 str = iconv . encode ( x . slice ( 0 , strLen - 1 ) , keyInfo [ 3 ] ) ;
531522 } else {
@@ -536,12 +527,12 @@ function encodeObject(data, dsLen, dsEncode) {
536527 break ;
537528 case
538529 DataType . object :
539- var tmpBuf = encodeObject ( x , keyInfo [ 2 ] , keyInfo [ 3 ] ) ;
530+ let tmpBuf = encodeObject ( x , keyInfo [ 2 ] , keyInfo [ 3 ] ) ;
540531 tmpBuf . copy ( msgBuf , off , 0 , keyInfo [ 2 ] ) ;
541532 break ;
542533 }
543534 off += keyInfo [ 2 ] ;
544- } )
535+ }
545536 }
546537
547538 return msgBuf ;
0 commit comments