aboutsummaryrefslogtreecommitdiffhomepage
path: root/node_modules/arraybuffer.slice/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/arraybuffer.slice/index.js')
-rw-r--r--node_modules/arraybuffer.slice/index.js29
1 files changed, 29 insertions, 0 deletions
diff --git a/node_modules/arraybuffer.slice/index.js b/node_modules/arraybuffer.slice/index.js
new file mode 100644
index 0000000..11ac556
--- /dev/null
+++ b/node_modules/arraybuffer.slice/index.js
@@ -0,0 +1,29 @@
+/**
+ * An abstraction for slicing an arraybuffer even when
+ * ArrayBuffer.prototype.slice is not supported
+ *
+ * @api public
+ */
+
+module.exports = function(arraybuffer, start, end) {
+ var bytes = arraybuffer.byteLength;
+ start = start || 0;
+ end = end || bytes;
+
+ if (arraybuffer.slice) { return arraybuffer.slice(start, end); }
+
+ if (start < 0) { start += bytes; }
+ if (end < 0) { end += bytes; }
+ if (end > bytes) { end = bytes; }
+
+ if (start >= bytes || start >= end || bytes === 0) {
+ return new ArrayBuffer(0);
+ }
+
+ var abv = new Uint8Array(arraybuffer);
+ var result = new Uint8Array(end - start);
+ for (var i = start, ii = 0; i < end; i++, ii++) {
+ result[ii] = abv[i];
+ }
+ return result.buffer;
+};