diff --git a/README.md b/README.md index 50009e4..5e639ea 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Install the module with: `npm install base64-arraybuffer` ## API The library encodes and decodes base64 to and from ArrayBuffers - - __encode(buffer)__ - Encodes `ArrayBuffer` into base64 string + - __encode(buffer [, start, [length]])__ - Encodes `ArrayBuffer` into base64 string - __decode(str)__ - Decodes base64 string to `ArrayBuffer` ## License diff --git a/lib/base64-arraybuffer.js b/lib/base64-arraybuffer.js index 362fbfa..ff5bd03 100644 --- a/lib/base64-arraybuffer.js +++ b/lib/base64-arraybuffer.js @@ -8,8 +8,13 @@ (function(chars){ "use strict"; - exports.encode = function(arraybuffer) { - var bytes = new Uint8Array(arraybuffer), + exports.encode = function(arraybuffer, start, length) { + + // use != undefined to include both 'undefined' and 'null' + start = (start != undefined) ? start : 0; + length = (length != undefined) ? length : arraybuffer.byteLength - start; + + var bytes = new Uint8Array(arraybuffer, start, length), i, len = bytes.length, base64 = ""; for (i = 0; i < len; i+=3) {