UNPKG

typedarray-to-buffer

Version:

Convert a typed array to a Buffer without a copy

27 lines (22 loc) 754 B
var test = require('tape') var convertToBuffer = require('../') test('convert to buffer from uint8array', function (t) { if (typeof Uint8Array === 'function') { var arr = new Uint8Array([1, 2, 3]) arr = convertToBuffer(arr) t.deepEqual(arr, new Buffer([1, 2, 3]), 'contents equal') t.ok(Buffer.isBuffer(arr), 'is buffer') t.equal(arr.readUInt16BE(0), 258) } t.end() }) test('convert to buffer from another array type (uint32array)', function (t) { if (typeof Uint32Array === 'function') { var arr = new Uint32Array([1, 2, 3]) arr = convertToBuffer(arr) t.deepEqual(arr, new Buffer([1, 2, 3]), 'contents equal') t.ok(Buffer.isBuffer(arr), 'is buffer') t.equal(arr.readUInt16BE(0), 258) } t.end() })