Last active
November 29, 2017 06:10
-
-
Save kevincharm/bdcab137e28007d784c6dc831c97f4a9 to your computer and use it in GitHub Desktop.
bitarray.service.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* bitarray.js | |
* | |
* Utility for working with JS TypedArrays at bit level. (Angular Service) | |
* | |
* @author kevincharm | |
* @license MIT | |
*/ | |
angular | |
.module('ShockWiz') | |
.service('BitArray', [ | |
BitArrayService | |
]); | |
function BitArray(buffer, endian) { | |
if (buffer) { | |
this.dataView = new DataView(buffer); | |
this.bitArray = _getBitArray(this.dataView, this.endian); | |
} else { | |
this.bitArray = []; | |
} | |
if (typeof endian === 'string') { | |
this.endian = !!(endian.toUpperCase() === 'LITTLE'); | |
} else if (typeof endian === 'boolean') { | |
this.endian = endian; | |
} else { | |
// defaults to little endian | |
this.endian = true; | |
} | |
} | |
function _getBitArray(data, endian) { | |
var bitString = ''; | |
for (var i = 0; i < data.byteLength; i++) { | |
var currentByteBitString = data.getUint8(i, endian).toString(2); | |
if (currentByteBitString.length < 8) { | |
var offset = 8 - currentByteBitString.length; | |
var pad = ''; | |
while (offset--) pad += '0'; | |
currentByteBitString += pad; | |
} | |
bitString += currentByteBitString; | |
} | |
return bitString.split('').map(s => parseInt(s)); | |
} | |
function _packBitArray(srcBitArray) { | |
var byteSize = Math.ceil(srcBitArray.length / 8); | |
var destinationBuffer = new Uint8Array(byteSize); | |
var destinationBufferBitCounter = 0; | |
for (var i = 0; i < srcBitArray.length; i++) { | |
var destinationBufferIndex = Math.floor(destinationBufferBitCounter / 8); | |
var destinationBufferBitOffset = destinationBufferBitCounter % 8; | |
destinationBuffer[destinationBufferIndex] |= srcBitArray[i] << destinationBufferBitOffset; | |
destinationBufferBitCounter++; | |
} | |
return destinationBuffer.buffer; | |
} | |
BitArray.prototype.toArray = function () { | |
return this.bitArray; | |
}; | |
BitArray.prototype.toString = function () { | |
return this.bitArray.join(''); | |
}; | |
BitArray.prototype.toBuffer = function () { | |
return _packBitArray(this.bitArray); | |
}; | |
BitArray.prototype.getBit = function (index) { | |
return this.bitArray[index]; | |
}; | |
BitArray.prototype.setBit = function (index, value) { | |
value = +(!!(+value)); /* coerce to number -> bool -> binary int */ | |
this.bitArray[index] = value; | |
}; | |
BitArray.prototype.getBitChunk = function (index, size) { | |
return this.bitArray.slice(index, index + size); | |
}; | |
BitArray.prototype.setBitChunk = function (index, chunkBitArray) { | |
if (typeof chunkBitArray === 'string') chunkBitArray = chunkBitArray.split(''); | |
for (var i = 0; i < chunkBitArray.length; i++) { | |
this.bitArray[index + i] = chunkBitArray[i]; | |
} | |
}; | |
function BitArrayService() { | |
return BitArray; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment