arraybuffer-slice
Advanced tools
Comparing version 0.0.2 to 0.1.0
57
index.js
// https://github.com/ttaubert/node-arraybuffer-slice | ||
// (c) 2013 Tim Taubert <tim@timtaubert.de> | ||
// (c) 2014 Tim Taubert <tim@timtaubert.de> | ||
// arraybuffer-slice may be freely distributed under the MIT license. | ||
"use strict"; | ||
(function (undefined) { | ||
"use strict"; | ||
if (!ArrayBuffer.prototype.slice) { | ||
ArrayBuffer.prototype.slice = function (begin, end) { | ||
begin = (begin|0) || 0; | ||
var num = this.byteLength; | ||
end = end === (void 0) ? num : (end|0); | ||
function clamp(val, length) { | ||
val = (val|0) || 0; | ||
// Handle negative values. | ||
if (begin < 0) begin += num; | ||
if (end < 0) end += num; | ||
if (val < 0) { | ||
return Math.max(val + length, 0); | ||
} | ||
if (num === 0 || begin >= num || begin >= end) { | ||
return new ArrayBuffer(0); | ||
if (val > length) { | ||
return length; | ||
} | ||
var length = Math.min(num - begin, end - begin); | ||
var target = new ArrayBuffer(length); | ||
var targetArray = new Uint8Array(target); | ||
targetArray.set(new Uint8Array(this, begin, length)); | ||
return target; | ||
}; | ||
} | ||
return val; | ||
} | ||
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 length = end - begin; | ||
var target = new ArrayBuffer(length); | ||
var targetArray = new Uint8Array(target); | ||
var sourceArray = new Uint8Array(this, begin, length); | ||
targetArray.set(sourceArray); | ||
return target; | ||
}; | ||
} | ||
})(); |
{ | ||
"name": "arraybuffer-slice", | ||
"version": "0.0.2", | ||
"version": "0.1.0", | ||
"description": "Polyfill for ArrayBuffer.prototype.slice.", | ||
"main": "index.js", | ||
"devDependencies": { | ||
"tape" : "~1.1.0" | ||
"tape": "~2.13.1" | ||
}, | ||
"scripts" : { | ||
"test" : "tape test/*.js" | ||
"scripts": { | ||
"test": "tape test/*.js" | ||
}, | ||
@@ -12,0 +12,0 @@ "keywords": [ |
// https://github.com/ttaubert/node-arraybuffer-slice | ||
// (c) 2013 Tim Taubert <tim@timtaubert.de> | ||
// (c) 2014 Tim Taubert <tim@timtaubert.de> | ||
// arraybuffer-slice may be freely distributed under the MIT license. | ||
@@ -47,2 +47,3 @@ | ||
t.equal(buf.slice(-4, -2).byteLength, 2); | ||
t.equal(buf.slice(-1000, 5).byteLength, 5); | ||
@@ -49,0 +50,0 @@ deepEqual(t, buf.slice(5), [5,6,7,8,9]); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4428
77