blob: 31050f8b59145a6177939fc248103394b673d9ba [file] [log] [blame]
// https://github.com/ttaubert/node-arraybuffer-slice
// (c) 2014 Tim Taubert <tim@timtaubert.de>
// arraybuffer-slice may be freely distributed under the MIT license.
(function (undefined) {
"use strict";
function clamp(val, length) {
val = (val|0) || 0;
if (val < 0) {
return Math.max(val + length, 0);
}
return Math.min(val, length);
}
if (!ArrayBuffer.prototype.slice) {
ArrayBuffer.prototype.slice = function (from, to) {
var length = this.byteLength;
var begin = clamp(from, length);
var end = length;
if (to !== undefined) {
end = clamp(to, length);
}
if (begin > end) {
return new ArrayBuffer(0);
}
var num = end - begin;
var target = new ArrayBuffer(num);
var targetArray = new Uint8Array(target);
var sourceArray = new Uint8Array(this, begin, num);
targetArray.set(sourceArray);
return target;
};
}
})();