UNPKG

binconv

Version:

Binary converters for Blob, Uint8Array, ReadableStream, ArrayBuffer, string in JavaScript/TypeScript

260 lines 14 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); var assert = require("power-assert"); var mergeUint8Arrays_1 = require("../src/mergeUint8Arrays"); var base64ToUint8Array_1 = require("../src/base64ToUint8Array"); var readableStreamToUint8Array_1 = require("../src/readableStreamToUint8Array"); var blobToReadableStream_1 = require("../src/blobToReadableStream"); var blobToArrayBuffer_1 = require("../src/blobToArrayBuffer"); var blobToUint8Array_1 = require("../src/blobToUint8Array"); var stringToUint8Array_1 = require("../src/stringToUint8Array"); var stringToArrayBuffer_1 = require("../src/stringToArrayBuffer"); var readableStreamToBlob_1 = require("../src/readableStreamToBlob"); var uint8ArrayToBase64_1 = require("../src/uint8ArrayToBase64"); var uint8ArrayToHexString_1 = require("../src/uint8ArrayToHexString"); describe('mergeUint8Arrays', function () { it('should merge empty array of Uint8Array', function () { return __awaiter(void 0, void 0, void 0, function () { var arrays, actual, expect; return __generator(this, function (_a) { arrays = []; actual = mergeUint8Arrays_1.mergeUint8Arrays(arrays); expect = new Uint8Array([]); assert.deepStrictEqual(actual, expect); return [2]; }); }); }); it('should merge array of Uint8Array', function () { return __awaiter(void 0, void 0, void 0, function () { var arrays, actual, expect; return __generator(this, function (_a) { arrays = [ new Uint8Array([1, 2, 3]), new Uint8Array([4, 5, 6, 7]), new Uint8Array([8, 9]), ]; actual = mergeUint8Arrays_1.mergeUint8Arrays(arrays); expect = new Uint8Array([ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]); assert.deepStrictEqual(actual, expect); return [2]; }); }); }); }); describe('base64ToUint8Array', function () { it('should convert Base64 string to Uint8Array', function () { return __awaiter(void 0, void 0, void 0, function () { var base64, actual, expect; return __generator(this, function (_a) { base64 = "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGFsdGVyYSBxdWlkYW0gaW4gcHJvLg=="; actual = base64ToUint8Array_1.base64ToUint8Array(base64); expect = new Uint8Array([ 76, 111, 114, 101, 109, 32, 105, 112, 115, 117, 109, 32, 100, 111, 108, 111, 114, 32, 115, 105, 116, 32, 97, 109, 101, 116, 44, 32, 97, 108, 116, 101, 114, 97, 32, 113, 117, 105, 100, 97, 109, 32, 105, 110, 32, 112, 114, 111, 46 ]); ; assert.deepStrictEqual(actual, expect); return [2]; }); }); }); }); describe('readableStreamToUint8Array', function () { it('should convert ReadableStream to Uint8Array', function () { return __awaiter(void 0, void 0, void 0, function () { var readableStream, actual, expect; return __generator(this, function (_a) { switch (_a.label) { case 0: readableStream = new ReadableStream({ start: function (controller) { controller.enqueue(new Uint8Array([1, 2, 3])); controller.enqueue(new Uint8Array([4, 5, 6, 7])); controller.enqueue(new Uint8Array([8, 9])); controller.close(); } }); return [4, readableStreamToUint8Array_1.readableStreamToUint8Array(readableStream)]; case 1: actual = _a.sent(); expect = new Uint8Array([ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]); assert.deepStrictEqual(actual, expect); return [2]; } }); }); }); }); describe('blobToReadableStream', function () { it('should convert blob to ReadableStream', function () { return __awaiter(void 0, void 0, void 0, function () { var blob, readableStream, array, expect; return __generator(this, function (_a) { switch (_a.label) { case 0: blob = new Blob(["Lorem ipsum dolor sit amet, altera quidam in pro."]); readableStream = blobToReadableStream_1.blobToReadableStream(blob); return [4, readableStreamToUint8Array_1.readableStreamToUint8Array(readableStream)]; case 1: array = _a.sent(); expect = new Uint8Array([ 76, 111, 114, 101, 109, 32, 105, 112, 115, 117, 109, 32, 100, 111, 108, 111, 114, 32, 115, 105, 116, 32, 97, 109, 101, 116, 44, 32, 97, 108, 116, 101, 114, 97, 32, 113, 117, 105, 100, 97, 109, 32, 105, 110, 32, 112, 114, 111, 46 ]); assert.deepStrictEqual(array, expect); return [2]; } }); }); }); }); describe('blobToArrayBuffer', function () { it('should convert blob to ArrayBuffer', function () { return __awaiter(void 0, void 0, void 0, function () { var blob, actual, expect; return __generator(this, function (_a) { switch (_a.label) { case 0: blob = new Blob(["Lorem ipsum dolor sit amet, altera quidam in pro."]); return [4, blobToArrayBuffer_1.blobToArrayBuffer(blob)]; case 1: actual = _a.sent(); expect = new Uint8Array([ 76, 111, 114, 101, 109, 32, 105, 112, 115, 117, 109, 32, 100, 111, 108, 111, 114, 32, 115, 105, 116, 32, 97, 109, 101, 116, 44, 32, 97, 108, 116, 101, 114, 97, 32, 113, 117, 105, 100, 97, 109, 32, 105, 110, 32, 112, 114, 111, 46 ]).buffer; assert.deepStrictEqual(actual, expect); return [2]; } }); }); }); }); describe('blobToUint8Array', function () { it('should convert blob to Uint8Array', function () { return __awaiter(void 0, void 0, void 0, function () { var blob, actual, expect; return __generator(this, function (_a) { switch (_a.label) { case 0: blob = new Blob(["Lorem ipsum dolor sit amet, altera quidam in pro."]); return [4, blobToUint8Array_1.blobToUint8Array(blob)]; case 1: actual = _a.sent(); expect = new Uint8Array([ 76, 111, 114, 101, 109, 32, 105, 112, 115, 117, 109, 32, 100, 111, 108, 111, 114, 32, 115, 105, 116, 32, 97, 109, 101, 116, 44, 32, 97, 108, 116, 101, 114, 97, 32, 113, 117, 105, 100, 97, 109, 32, 105, 110, 32, 112, 114, 111, 46 ]); assert.deepStrictEqual(actual, expect); return [2]; } }); }); }); }); describe('stringToUint8Array', function () { it('should convert string to Uint8Array', function () { return __awaiter(void 0, void 0, void 0, function () { var string, actual, expect; return __generator(this, function (_a) { string = "Lorem ipsum dolor sit amet, altera quidam in pro."; actual = stringToUint8Array_1.stringToUint8Array(string); expect = new Uint8Array([ 76, 111, 114, 101, 109, 32, 105, 112, 115, 117, 109, 32, 100, 111, 108, 111, 114, 32, 115, 105, 116, 32, 97, 109, 101, 116, 44, 32, 97, 108, 116, 101, 114, 97, 32, 113, 117, 105, 100, 97, 109, 32, 105, 110, 32, 112, 114, 111, 46 ]); assert.deepStrictEqual(actual, expect); return [2]; }); }); }); }); describe('stringToArrayBuffer', function () { it('should convert string to ArrayBuffer', function () { return __awaiter(void 0, void 0, void 0, function () { var string, actual, expect; return __generator(this, function (_a) { string = "Lorem ipsum dolor sit amet, altera quidam in pro."; actual = stringToArrayBuffer_1.stringToArrayBuffer(string); expect = new Uint8Array([ 76, 111, 114, 101, 109, 32, 105, 112, 115, 117, 109, 32, 100, 111, 108, 111, 114, 32, 115, 105, 116, 32, 97, 109, 101, 116, 44, 32, 97, 108, 116, 101, 114, 97, 32, 113, 117, 105, 100, 97, 109, 32, 105, 110, 32, 112, 114, 111, 46 ]).buffer; assert.deepStrictEqual(actual, expect); return [2]; }); }); }); }); describe('readableStreamToBlob', function () { it('should convert ReadableStream to Blob', function () { return __awaiter(void 0, void 0, void 0, function () { var readableStream, actual, expect; return __generator(this, function (_a) { switch (_a.label) { case 0: readableStream = new ReadableStream({ start: function (controller) { controller.enqueue(new Uint8Array([1, 2, 3])); controller.enqueue(new Uint8Array([4, 5, 6, 7])); controller.enqueue(new Uint8Array([8, 9])); controller.close(); } }); return [4, readableStreamToBlob_1.readableStreamToBlob(readableStream)]; case 1: actual = _a.sent(); expect = new Blob([new Uint8Array([ 1, 2, 3, 4, 5, 6, 7, 8, 9 ])]); assert.deepStrictEqual(actual, expect); return [2]; } }); }); }); }); describe('uint8ArrayToBase64', function () { it('should convert Uint8Array to Base64', function () { return __awaiter(void 0, void 0, void 0, function () { var array, actual, expect; return __generator(this, function (_a) { array = new Uint8Array([ 76, 111, 114, 101, 109, 32, 105, 112, 115, 117, 109, 32, 100, 111, 108, 111, 114, 32, 115, 105, 116, 32, 97, 109, 101, 116, 44, 32, 97, 108, 116, 101, 114, 97, 32, 113, 117, 105, 100, 97, 109, 32, 105, 110, 32, 112, 114, 111, 46 ]); actual = uint8ArrayToBase64_1.uint8ArrayToBase64(array); expect = "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGFsdGVyYSBxdWlkYW0gaW4gcHJvLg=="; assert.deepStrictEqual(actual, expect); return [2]; }); }); }); }); describe('uint8ArrayToHexString', function () { it('should convert Uint8Array to hex string', function () { return __awaiter(void 0, void 0, void 0, function () { var array, actual, expect; return __generator(this, function (_a) { array = new Uint8Array([ 76, 111, 114, 101, 109, 32, 105, 112, 115, 117, 109, 32, 100, 111, 108, 111, 114, 32, 115, 105, 116, 32, 97, 109, 101, 116, 44, 32, 97, 108, 116, 101, 114, 97, 32, 113, 117, 105, 100, 97, 109, 32, 105, 110, 32, 112, 114, 111, 46 ]); actual = uint8ArrayToHexString_1.uint8ArrayToHexString(array); expect = "4c6f72656d20697073756d20646f6c6f722073697420616d65742c20616c746572612071756964616d20696e2070726f2e"; assert.deepStrictEqual(actual, expect); return [2]; }); }); }); }); //# sourceMappingURL=index.test.js.map