Skip to content

Commit

Permalink
farm out arraybuffers.concat to Buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
trevj committed Apr 14, 2016
1 parent 03feb6f commit 5bb0cb9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 53 deletions.
38 changes: 1 addition & 37 deletions src/arraybuffers/arraybuffers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,49 +68,13 @@ describe('ArrayBuffers <-> Hex Strings', function() {
});
});

describe('ArrayBuffers concat & chunk', function() {
describe('ArrayBuffers chunk', function() {
it('chunk(array12, 6).length == 3', function() {
expect(arraybuffers.chunk(array12,6).length).toBe(3);
});
it('chunk(array12, 1).length == 15', function() {
expect(arraybuffers.chunk(array12,1).length).toBe(15);
});
it('concat(array1,array2) == array12', function() {
expect(arraybuffers.byteEquality(
arraybuffers.concat([array1, array2]),
array12))
.toBe(true);
});
it('concat(chunk(array12, 1)) == array12', function() {
expect(arraybuffers.byteEquality(
arraybuffers.concat(arraybuffers.chunk(array12,1)),
array12))
.toBe(true);
});
it('concat(chunk(array12, 4)) == array12', function() {
expect(arraybuffers.byteEquality(
arraybuffers.concat(arraybuffers.chunk(array12,4)),
array12))
.toBe(true);
});
it('concat(chunk(array12, 5)) == array12', function() {
expect(arraybuffers.byteEquality(
arraybuffers.concat(arraybuffers.chunk(array12,5)),
array12))
.toBe(true);
});
it('concat(chunk(array12, array12.byteLength)) == array12', function() {
expect(arraybuffers.byteEquality(
arraybuffers.concat(arraybuffers.chunk(array12,array12.byteLength)),
array12))
.toBe(true);
});
it('concat(chunk(array12, 20)) == array12', function() {
expect(arraybuffers.byteEquality(
arraybuffers.concat(arraybuffers.chunk(array12,20)),
array12))
.toBe(true);
});
});

describe('ArrayBuffers <-> strings', function() {
Expand Down
22 changes: 6 additions & 16 deletions src/arraybuffers/arraybuffers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,12 @@ export function byteEquality(b1: ArrayBuffer, b2: ArrayBuffer): boolean {
return new Buffer(b1).equals(new Buffer(b2));
}

// Concat |ArrayBuffer|s into a single ArrayBuffer. If size is given, then
// the destination array buffer is of the given size. If size is not given or
// zero, the size of all buffers is summed to make the new array buffer.
export function concat(buffers:ArrayBuffer[], size?:number)
:ArrayBuffer {
if(!size) {
size = 0;
buffers.forEach(a => { size += a.byteLength });
}
var accumulatorBuffer = new Uint8Array(size);
var location = 0;
buffers.forEach(a => {
accumulatorBuffer.set(new Uint8Array(a), location);
location += a.byteLength;
});
return accumulatorBuffer.buffer;
// Returns a new ArrayBuffer which is the result of concatenating all the
// supplied ArrayBuffers together. If size is supplied, the resulting
// ArrayBuffer will be of the given size.
export function concat(buffers: ArrayBuffer[], size?: number): ArrayBuffer {
// The Buffer instances share memory with their source ArrayBuffers.
return Buffer.concat(buffers.map(buffer => new Buffer(buffer)), size).buffer;
}

// Break an array buffer into multiple array buffers that are at most |size|
Expand Down

0 comments on commit 5bb0cb9

Please sign in to comment.