[Pkg-javascript-commits] [node-arraybuffer.slice] 01/02: Imported Upstream version 0.0.5
Sebastiaan Couwenberg
sebastic at moszumanska.debian.org
Sun Mar 29 17:23:53 UTC 2015
This is an automated email from the git hooks/post-receive script.
sebastic pushed a commit to branch master
in repository node-arraybuffer.slice.
commit 87b908074c84737daf59d93f8188cfb505adc6bd
Author: Bas Couwenberg <sebastic at xs4all.nl>
Date: Sun Mar 29 18:59:27 2015 +0200
Imported Upstream version 0.0.5
---
.gitignore | 17 ++++
Makefile | 8 ++
README.md | 17 ++++
index.js | 29 +++++++
package.json | 15 ++++
test/slice-buffer.js | 227 +++++++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 313 insertions(+)
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..cfbee8d
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,17 @@
+lib-cov
+lcov.info
+*.seed
+*.log
+*.csv
+*.dat
+*.out
+*.pid
+*.gz
+
+pids
+logs
+results
+build
+.grunt
+
+node_modules
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..849887f
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,8 @@
+
+REPORTER = dot
+
+test:
+ @./node_modules/.bin/mocha \
+ --reporter $(REPORTER)
+
+.PHONY: test
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..15e465e
--- /dev/null
+++ b/README.md
@@ -0,0 +1,17 @@
+# How to
+```javascript
+var sliceBuffer = require('arraybuffer.slice');
+var ab = (new Int8Array(5)).buffer;
+var sliced = sliceBuffer(ab, 1, 3);
+sliced = sliceBuffer(ab, 1);
+```
+
+# Licence (MIT)
+Copyright (C) 2013 Rase-
+
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..636fef9
--- /dev/null
+++ b/index.js
@@ -0,0 +1,29 @@
+/**
+ * An abstraction for slicing an arraybuffer even when
+ * ArrayBuffer.prototype.slice is not supported
+ *
+ * @api private
+ */
+
+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;
+};
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..85d31a2
--- /dev/null
+++ b/package.json
@@ -0,0 +1,15 @@
+{
+ "name": "arraybuffer.slice",
+ "description": "Exports a function for slicing ArrayBuffers (no polyfilling)",
+ "version": "0.0.5",
+ "homepage": "https://github.com/rase-/arraybuffer.slice",
+ "dependencies": {},
+ "devDependencies": {
+ "mocha": "1.17.1",
+ "expect.js": "0.2.0"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git at github.com:rase-/arraybuffer.slice.git"
+ }
+}
diff --git a/test/slice-buffer.js b/test/slice-buffer.js
new file mode 100644
index 0000000..4778da6
--- /dev/null
+++ b/test/slice-buffer.js
@@ -0,0 +1,227 @@
+/*
+ * Test dependencies
+ */
+
+var sliceBuffer = require('../index.js');
+var expect = require('expect.js');
+
+/**
+ * Tests
+ */
+
+describe('sliceBuffer', function() {
+ describe('using standard slice', function() {
+ it('should slice correctly with only start provided', function() {
+ var abv = new Uint8Array(10);
+ for (var i = 0; i < abv.length; i++) {
+ abv[i] = i;
+ }
+
+ var sliced = sliceBuffer(abv.buffer, 3);
+ var sabv = new Uint8Array(sliced);
+ for (var i = 3, ii = 0; i < abv.length; i++, ii++) {
+ expect(abv[i]).to.equal(sabv[ii]);
+ }
+ });
+
+ it('should slice correctly with start and end provided', function() {
+ var abv = new Uint8Array(10);
+ for (var i = 0; i < abv.length; i++) {
+ abv[i] = i;
+ }
+
+ var sliced = sliceBuffer(abv.buffer, 3, 8);
+ var sabv = new Uint8Array(sliced);
+ for (var i = 3, ii = 0; i < 8; i++, ii++) {
+ expect(abv[i]).to.equal(sabv[ii]);
+ }
+ });
+
+ it('should slice correctly with negative start', function() {
+ var abv = new Uint8Array(10);
+ for (var i = 0; i < abv.length; i++) {
+ abv[i] = i;
+ }
+
+ var sliced = sliceBuffer(abv.buffer, -3);
+ var sabv = new Uint8Array(sliced);
+ for (var i = abv.length - 3, ii = 0; i < abv.length; i++, ii++) {
+ expect(abv[i]).to.equal(sabv[ii]);
+ }
+ });
+
+ it('should slice correctly with negative end', function() {
+ var abv = new Uint8Array(10);
+ for (var i = 0; i < abv.length; i++) {
+ abv[i] = i;
+ }
+
+ var sliced = sliceBuffer(abv.buffer, 0, -3);
+ var sabv = new Uint8Array(sliced);
+ for (var i = 0, ii = 0; i < abv.length - 3; i++, ii++) {
+ expect(abv[i]).to.equal(sabv[ii]);
+ }
+ });
+
+ it('should slice correctly with negative start and end', function() {
+ var abv = new Uint8Array(10);
+ for (var i = 0; i < abv.length; i++) {
+ abv[i] = i;
+ }
+
+ var sliced = sliceBuffer(abv.buffer, -6, -3);
+ var sabv = new Uint8Array(sliced);
+ for (var i = abv.length - 6, ii = 0; i < abv.length - 3; i++, ii++) {
+ expect(abv[i]).to.equal(sabv[ii]);
+ }
+ });
+
+ it('should slice correctly with equal start and end', function() {
+ var abv = new Uint8Array(10);
+ for (var i = 0; i < abv.length; i++) {
+ abv[i] = i;
+ }
+
+ var sliced = sliceBuffer(abv.buffer, 1, 1);
+ expect(sliced.byteLength).to.equal(0);
+ });
+
+ it('should slice correctly when end larger than buffer', function() {
+ var abv = new Uint8Array(10);
+ for (var i = 0; i < abv.length; i++) {
+ abv[i] = i;
+ }
+
+ var sliced = sliceBuffer(abv.buffer, 0, 100);
+ expect(new Uint8Array(sliced)).to.eql(abv);
+ });
+
+ it('shoud slice correctly when start larger than end', function() {
+ var abv = new Uint8Array(10);
+ for (var i = 0; i < abv.length; i++) {
+ abv[i] = i;
+ }
+
+ var sliced = sliceBuffer(abv.buffer, 6, 5);
+ expect(sliced.byteLength).to.equal(0);
+ });
+ });
+
+ describe('using fallback', function() {
+ it('should slice correctly with only start provided', function() {
+ var abv = new Uint8Array(10);
+ for (var i = 0; i < abv.length; i++) {
+ abv[i] = i;
+ }
+ var ab = abv.buffer;
+ ab.slice = undefined;
+
+ var sliced = sliceBuffer(ab, 3);
+ var sabv = new Uint8Array(sliced);
+ for (var i = 3, ii = 0; i < abv.length; i++, ii++) {
+ expect(abv[i]).to.equal(sabv[ii]);
+ }
+ });
+
+ it('should slice correctly with start and end provided', function() {
+ var abv = new Uint8Array(10);
+ for (var i = 0; i < abv.length; i++) {
+ abv[i] = i;
+ }
+ var ab = abv.buffer;
+ ab.slice = undefined;
+
+
+ var sliced = sliceBuffer(ab, 3, 8);
+ var sabv = new Uint8Array(sliced);
+ for (var i = 3, ii = 0; i < 8; i++, ii++) {
+ expect(abv[i]).to.equal(sabv[ii]);
+ }
+ });
+
+ it('should slice correctly with negative start', function() {
+ var abv = new Uint8Array(10);
+ for (var i = 0; i < abv.length; i++) {
+ abv[i] = i;
+ }
+ var ab = abv.buffer;
+ ab.slice = undefined;
+
+
+ var sliced = sliceBuffer(ab, -3);
+ var sabv = new Uint8Array(sliced);
+ for (var i = abv.length - 3, ii = 0; i < abv.length; i++, ii++) {
+ expect(abv[i]).to.equal(sabv[ii]);
+ }
+ });
+
+ it('should slice correctly with negative end', function() {
+ var abv = new Uint8Array(10);
+ for (var i = 0; i < abv.length; i++) {
+ abv[i] = i;
+ }
+ var ab = abv.buffer;
+ ab.slice = undefined;
+
+ var sliced = sliceBuffer(ab, 0, -3);
+ var sabv = new Uint8Array(sliced);
+ for (var i = 0, ii = 0; i < abv.length - 3; i++, ii++) {
+ expect(abv[i]).to.equal(sabv[ii]);
+ }
+ });
+
+ it('should slice correctly with negative start and end', function() {
+ var abv = new Uint8Array(10);
+ for (var i = 0; i < abv.length; i++) {
+ abv[i] = i;
+ }
+ var ab = abv.buffer;
+ ab.slice = undefined;
+
+ var sliced = sliceBuffer(ab, -6, -3);
+ var sabv = new Uint8Array(sliced);
+ for (var i = abv.length - 6, ii = 0; i < abv.length - 3; i++, ii++) {
+ expect(abv[i]).to.equal(sabv[ii]);
+ }
+ });
+
+ it('should slice correctly with equal start and end', function() {
+ var abv = new Uint8Array(10);
+ for (var i = 0; i < abv.length; i++) {
+ abv[i] = i;
+ }
+ var ab = abv.buffer;
+ ab.slice = undefined;
+
+ var sliced = sliceBuffer(ab, 1, 1);
+ expect(sliced.byteLength).to.equal(0);
+ });
+
+ it('should slice correctly when end larger than buffer', function() {
+ var abv = new Uint8Array(10);
+ for (var i = 0; i < abv.length; i++) {
+ abv[i] = i;
+ }
+ var ab = abv.buffer;
+ ab.slice = undefined;
+
+ var sliced = sliceBuffer(ab, 0, 100);
+ var sabv = new Uint8Array(sliced);
+ for (var i = 0; i < abv.length; i++) {
+ expect(abv[i]).to.equal(sabv[i]);
+ }
+ });
+
+ it('shoud slice correctly when start larger than end', function() {
+ var abv = new Uint8Array(10);
+ for (var i = 0; i < abv.length; i++) {
+ abv[i] = i;
+ }
+ var ab = abv.buffer;
+ ab.slice = undefined;
+
+ var sliced = sliceBuffer(ab, 6, 5);
+ expect(sliced.byteLength).to.equal(0);
+ });
+ });
+});
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-arraybuffer.slice.git
More information about the Pkg-javascript-commits
mailing list