"use strict" ; export param { Buffer } bitArray returns { number [ ] } export function bitArrayToArray ( bitArray ) { let resultArray = [ ] ; for ( let index = 0 ; index < bitArray . byteLength ; index ++ ) { let arrayByte = bitArray . readUInt8 ( index ) ; if ( arrayByte !== 0 ) { for ( let bitIndex = 0 ; bitIndex < 8 ; bitIndex ++ ) { if ( ( arrayByte & 0x01 ) === 0x01 ) { resultArray . push ( index * 8 + bitIndex ) ; } arrayByte = arrayByte >>> 1 ; } } } return resultArray ; } export param { number [ ] } numberArray param { number } bufferLength param { Buffer } destinationBuffer returns { Buffer } export function arrayToBitArray ( numberArray , bufferLength , destinationBuffer ? ) { const returnBuffer = destinationBuffer ? destinationBuffer : Buffer . alloc ( bufferLength ) ; if ( destinationBuffer ) { bufferLength = destinationBuffer . byteLength ; } const maxAllowedNumber = bufferLength * 8 - 1 ; returnBuffer . fill ( 0 ) ; for ( let index = 0 ; index < numberArray . length ; index ++ ) { const numberToWrite = numberArray [ index ] ; if ( numberToWrite < 0 || numberToWrite > maxAllowedNumber ) throw new Error ( "Number out of range." ) ; returnBuffer [ Math . floor ( numberToWrite / 8 ) ] |= 1 << ( numberToWrite % 8 ) ; } return returnBuffer ; }