@@ -21,11 +21,27 @@ export class Serialize {
21
21
}
22
22
23
23
private isSpecialType ( prop : object ) {
24
- let propKeys = Object . keys ( prop ) . filter ( k => k != " type" && k != " digits" && k != " length" )
24
+ let propKeys = Object . keys ( prop ) . filter ( k => k != ' type' && k != ' digits' && k != ' length' )
25
25
26
26
return ! propKeys . length
27
27
}
28
28
29
+ private boolArrayToInt ( array : any ) {
30
+ // start with 1 to avoid errors such as 010 -> 10
31
+ // now it will be 1010 which will not simplify
32
+ let string = '1'
33
+ for ( var i = 0 ; i < array . length ; i ++ ) {
34
+ string += + ( ! ! array [ i ] )
35
+ }
36
+ return parseInt ( string , 2 )
37
+ }
38
+
39
+ private intToBoolArray ( int : number ) {
40
+ // convert string to array, map the numbers to bools,
41
+ // and remove the initial 1
42
+ return [ ...( int >>> 0 ) . toString ( 2 ) ] . map ( e => e == '0' ? false : true ) . slice ( 1 )
43
+ }
44
+
29
45
public flatten ( schema : any , data : any ) {
30
46
let flat : any [ ] = [ ]
31
47
@@ -43,12 +59,19 @@ export class Serialize {
43
59
// console.log('-------')
44
60
// console.log('data', typeof data, data)
45
61
46
- for ( let property in data ) {
62
+ let property : any
63
+ for ( property in data ) {
47
64
if ( data . hasOwnProperty ( property ) ) {
48
65
if ( typeof data [ property ] === 'object' ) {
49
66
// if data is array, but schemas is flat, use index 0 on the next iteration
50
- if ( Array . isArray ( data ) ) flatten ( schema , data [ parseInt ( property ) ] )
51
- else flatten ( schema [ property ] , data [ property ] )
67
+ if ( Array . isArray ( data ) ) {
68
+ flatten ( schema , data [ parseInt ( property ) ] )
69
+ } else if ( schema [ property ] ?. _type === "BitArray8" || schema [ property ] ?. _type === "BitArray16" ) {
70
+ flat . push ( {
71
+ d : this . boolArrayToInt ( data [ property ] ) ,
72
+ t : schema [ property ] . _type
73
+ } )
74
+ } else flatten ( schema [ property ] , data [ property ] )
52
75
}
53
76
//---
54
77
else {
@@ -64,11 +87,13 @@ export class Serialize {
64
87
}
65
88
flat . push ( { d : data [ property ] , t : schema [ property ] . type . _type } )
66
89
} else {
67
- // crop strings to default length of 12 characters if nothing else is specified
68
- if ( schema [ property ] . _type === 'String8' || schema [ property ] . _type === 'String16' ) {
69
- data [ property ] = this . cropString ( data [ property ] , 12 )
90
+ if ( schema [ property ] ?. _type ) {
91
+ // crop strings to default length of 12 characters if nothing else is specified
92
+ if ( schema [ property ] ?. _type === 'String8' || schema [ property ] ?. _type === 'String16' ) {
93
+ data [ property ] = this . cropString ( data [ property ] , 12 )
94
+ }
95
+ flat . push ( { d : data [ property ] , t : schema [ property ] . _type } )
70
96
}
71
- flat . push ( { d : data [ property ] , t : schema [ property ] . _type } )
72
97
}
73
98
}
74
99
} else {
@@ -133,6 +158,12 @@ export class Serialize {
133
158
} else if ( f . t === 'Float64Array' ) {
134
159
this . _dataView . setFloat64 ( this . _bytes , f . d )
135
160
this . _bytes += 8
161
+ } else if ( f . t === 'BitArray8' ) {
162
+ this . _dataView . setUint8 ( this . _bytes , f . d )
163
+ this . _bytes ++
164
+ } else if ( f . t === 'BitArray16' ) {
165
+ this . _dataView . setUint16 ( this . _bytes , f . d )
166
+ this . _bytes += 2
136
167
} else {
137
168
console . log ( 'ERROR: Something unexpected happened!' )
138
169
}
@@ -271,6 +302,14 @@ export class Serialize {
271
302
value = view . getFloat64 ( bytes )
272
303
bytes += _bytes
273
304
}
305
+ if ( _type === 'BitArray8' ) {
306
+ value = this . intToBoolArray ( view . getUint8 ( bytes ) )
307
+ bytes += _bytes
308
+ }
309
+ if ( _type === 'BitArray16' ) {
310
+ value = this . intToBoolArray ( view . getUint16 ( bytes ) )
311
+ bytes += _bytes
312
+ }
274
313
275
314
// apply special types options
276
315
if ( typeof value === 'number' && specialTypes ?. digits ) {
0 commit comments