javascript-binary-converter
Version:
A utility package to quickly handle and convert various Javascript binary objects
145 lines • 8.35 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const expect_1 = require("expect");
const converter_1 = __importDefault(require("../../converter"));
const test_utils_1 = require("./test-utils");
describe('Node DecimalBytesConverter tests', () => {
it('Should return Uint8Array, and an Int8Array, from byte decimals', async function () {
var bytes = [255, 1];
var uint8 = (0, converter_1.default)(bytes).toUint8Array();
(0, expect_1.expect)(uint8 instanceof Uint8Array).toBe(true);
(0, expect_1.expect)(uint8[0]).toBe(255);
(0, expect_1.expect)(uint8[1]).toBe(1);
var bytes = [128, 119, 247];
var int8 = (0, converter_1.default)(bytes).toInt8Array();
(0, expect_1.expect)(int8 instanceof Int8Array).toBe(true);
(0, expect_1.expect)(int8[0]).toBe(-128);
(0, expect_1.expect)(int8[1]).toBe(119);
(0, expect_1.expect)(int8[2]).toBe(-9);
var bytes = [-127, 119, -1];
var int8 = (0, converter_1.default)(bytes).toInt8Array(); //
(0, expect_1.expect)(int8 instanceof Int8Array).toBe(true);
(0, expect_1.expect)(int8[0]).toBe(-127);
(0, expect_1.expect)(int8[1]).toBe(119);
(0, expect_1.expect)(int8[2]).toBe(-1);
});
it('Should return Uint16Array, and an Int16Array, from byte decimals', async function () {
var bytes = [255, 255, 0, 0];
var uint16 = (0, converter_1.default)(bytes).toUint16Array();
(0, expect_1.expect)(uint16 instanceof Uint16Array).toBe(true);
(0, expect_1.expect)(uint16[0]).toBe(65535);
(0, expect_1.expect)(uint16[1]).toBe(0);
// var bytes = [255,255, 247, 247, 128, 0]
var bytes = [-127, -127, 120, 120, 120, 0];
var int16 = (0, converter_1.default)(bytes).toInt16Array();
(0, expect_1.expect)(int16 instanceof Int16Array).toBe(true);
(0, expect_1.expect)(int16[0]).toBe(-32383);
(0, expect_1.expect)(int16[1]).toBe(30840);
(0, expect_1.expect)(int16[2]).toBe(120);
var bytes = [-127, -127, -1, 2];
//11111111111111111111111110000001,11111111111111111111111110000001,11111111111111111111111111111111,10
//10000001,10000001,11111111,00000010
var int16 = (0, converter_1.default)(bytes).toInt16Array();
// console.log(int16)
(0, expect_1.expect)(int16 instanceof Int16Array).toBe(true);
(0, expect_1.expect)(int16[0]).toBe(-32383);
(0, expect_1.expect)(int16[1]).toBe(767);
var bytes = [-45, -40, -20, -21];
var int16 = (0, converter_1.default)(bytes).toInt16Array();
(0, expect_1.expect)(int16 instanceof Int16Array).toBe(true);
(0, expect_1.expect)(int16[0]).toBe(-10029);
(0, expect_1.expect)(int16[1]).toBe(-5140);
});
it('Should return an Int32Array, from byte decimals', async function () {
// var bytes = ['10000000', '00000000', '00000000', '00000000', '11110111', '11110111', '11110111', '11110111', '01110111', '01110111', '01110111', '01110111']
// var bytes = [128, 0,0,0, 247, 247, 247, 247, 119, 119, 119, 119]
let bytes = [120, -2, 0, 0];
var int32 = (0, converter_1.default)(bytes).toInt32Array();
(0, expect_1.expect)(int32 instanceof Int32Array).toBe(true);
(0, expect_1.expect)(int32[0]).toBe(65144);
// expect(int32[1]).toBe(-134744073)
// expect(int32[2]).toBe(2004318071)
bytes = [-127, -127, -127, 0];
var int32 = (0, converter_1.default)(bytes).toInt32Array();
(0, expect_1.expect)(int32 instanceof Int32Array).toBe(true);
(0, expect_1.expect)(int32[0]).toBe(8487297);
});
it('Should return a Uint32Array, from byte decimals', async function () {
var bytes = [119, 119, 119, 1];
var uInt32 = (0, converter_1.default)(bytes).toUint32Array();
(0, expect_1.expect)(uInt32 instanceof Uint32Array).toBe(true);
(0, expect_1.expect)(uInt32.length).toBe(1);
(0, expect_1.expect)(uInt32[0]).toBe(24606583);
});
it('Should return a "ΆΆ" string, from byte decimals', async function () {
var bytes = [206, 134, 206, 134];
var text = (0, converter_1.default)(bytes).toText();
(0, expect_1.expect)(text).toBe('ΆΆ');
});
it('Should return a Blob, from byte decimals', async function () {
const bytes = (0, test_utils_1.getDecimalBytes)();
var blob = await (0, converter_1.default)(bytes).toBlob();
(0, expect_1.expect)(blob instanceof Blob).toBe(true);
(0, expect_1.expect)(blob.size === bytes.length).toBe(true);
});
it('Should return an array of hex, from an array of byte decimals', async function () {
var bytes = [212, 252, 136]; //-44,-4,-120 decimals in twos complement
var hexes = (0, converter_1.default)(bytes).toHexString();
(0, expect_1.expect)(hexes[0]).toBe('D4');
(0, expect_1.expect)(hexes[1]).toBe('FC');
(0, expect_1.expect)(hexes[2]).toBe('88');
// console.log('hexes', hexes)
var hexes = (0, converter_1.default)(bytes).toHexString();
(0, expect_1.expect)(hexes[0][hexes[0].length - 1]).toBe('4');
(0, expect_1.expect)(hexes[0][hexes[0].length - 2]).toBe('D');
(0, expect_1.expect)(hexes[1][hexes[1].length - 1]).toBe('C');
(0, expect_1.expect)(hexes[1][hexes[1].length - 2]).toBe('F');
(0, expect_1.expect)(hexes[2]).toBe('88'); //Inquire this !
// console.log('hexes', hexes)
});
it('Should return an array of hex, from an array of bytes', async function () {
var bytes = [212, 252, 136]; //-44,-4,-120 decimals in twos complement
var hexes = (0, converter_1.default)(bytes).toHexString();
(0, expect_1.expect)(hexes[0]).toBe('D4');
(0, expect_1.expect)(hexes[1]).toBe('FC');
(0, expect_1.expect)(hexes[2]).toBe('88');
// console.log('hexes', hexes)
var hexes = (0, converter_1.default)(bytes).toHexString();
(0, expect_1.expect)(hexes[0][hexes[0].length - 1]).toBe('4');
(0, expect_1.expect)(hexes[0][hexes[0].length - 2]).toBe('D');
(0, expect_1.expect)(hexes[1][hexes[1].length - 1]).toBe('C');
(0, expect_1.expect)(hexes[1][hexes[1].length - 2]).toBe('F');
(0, expect_1.expect)(hexes[2]).toBe('88'); //Inquire this !
// console.log('hexes', hexes)
});
// it('Should return a Float32Array, from bytes', async function () {
// var bytes = [66, 10, 68, 156, 66, 26, 68, 156]
// // '01000010000010100100010010011100','01000010000110100100010010011100'
// var float32 = converter(bytes).toFloat32Array()
// expect(float32[0].toString().includes('34.5670013')).toBe(true)
// expect(float32[1].toString().includes('38.5670013')).toBe(true)
// var bytes = [194, 10, 68, 156]
// // '11000010000010100100010010011100'
// var float32 = converter(bytes).toFloat32Array()
// expect(float32[0].toString().includes('-34.5670013')).toBe(true)
// var bytes = [63, 17, 39, 0]
// // 00111111000100010010011100000000
// var float32 = converter(bytes).toFloat32Array()
// expect(float32[0].toString().includes('0.5670013')).toBe(true)
// var bytes = [191, 17, 39, 0]
// // 10111111000100010010011100000000
// var float32 = converter(bytes).toFloat32Array()
// expect(float32[0].toString().includes('-0.5670013')).toBe(true)
// var bytes = [-127, -127, -127, 1]
// // 10111111000100010010011100000001
// var float32 = converter(bytes).toFloat32Array()
// expect(float32[0].toString().includes('-4.757222')).toBe(true)
// });
// const bytes = ['11111111', '11111111', '11110111']
// const decimals = converter(bytes).toIntegers({isSigned:true})//Can be signed or unsigned. You can also assert the integer size(Default is 8)
// console.log('yoyo',decimals)
});
//# sourceMappingURL=node-decimal-bytes-converter.spec.js.map