buffer-indexof-polyfill
Advanced tools
Comparing version 1.0.0 to 1.0.1
39
index.js
@@ -34,1 +34,40 @@ "use strict"; | ||
} | ||
function bufferLastIndexOf (value, offset) { | ||
// Always wrap the input as a Buffer so that this method will support any | ||
// data type such as array octet, string or buffer. | ||
if (typeof value === "string" || value instanceof String) { | ||
value = new Buffer(value); | ||
} else if (typeof value === "number" || value instanceof Number) { | ||
value = new Buffer([ value ]); | ||
} | ||
var len = value.length; | ||
offset = offset || this.length - len; | ||
for (var i = offset; i >= 0; i--) { | ||
var mismatch = false; | ||
for (var j = 0; j < len; j++) { | ||
if (this[i + j] != value[j]) { | ||
mismatch = true; | ||
break; | ||
} | ||
} | ||
if (!mismatch) { | ||
return i; | ||
} | ||
} | ||
return -1; | ||
} | ||
if (Buffer.prototype.lastIndexOf) { | ||
// check Buffer#lastIndexOf is usable: https://github.com/nodejs/node/issues/4604 | ||
if (new Buffer ("ABC").lastIndexOf ("ABC") === -1) | ||
Buffer.prototype.lastIndexOf = bufferLastIndexOf; | ||
} else { | ||
Buffer.prototype.lastIndexOf = bufferLastIndexOf; | ||
} |
{ | ||
"name": "buffer-indexof-polyfill", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "This is a polyfill for Buffer#indexOf introduced in NodeJS 4.0.", | ||
@@ -12,2 +12,5 @@ "main": "index.js", | ||
"license": "MIT", | ||
"engines": { | ||
"node": ">=0.10" | ||
}, | ||
"repository": { | ||
@@ -19,3 +22,3 @@ "type": "git", | ||
"chai": "^3.3.0", | ||
"eslint": "^1.6.0", | ||
"eslint": "^1.10.3", | ||
"mocha": "^2.3.3" | ||
@@ -22,0 +25,0 @@ }, |
# buffer-indexof-polyfill | ||
[![Build Status][travis-image]][travis-url] | ||
[![NPM Version][npm-image]][npm-url] | ||
[![NPM Downloads][downloads-image]][downloads-url] | ||
This is a polyfill for [`Buffer#indexOf`](https://nodejs.org/api/buffer.html#buffer_buf_indexof_value_byteoffset) introduced in NodeJS 4.0. | ||
This is a polyfill for [`Buffer#indexOf`](https://nodejs.org/api/buffer.html#buffer_buf_indexof_value_byteoffset) and Buffer#lastIndexOf introduced in NodeJS 4.0. | ||
@@ -31,1 +32,3 @@ ## Example | ||
[downloads-url]: https://npmjs.org/package/buffer-indexof-polyfill | ||
[travis-image]: https://travis-ci.org/sarosia/buffer-indexof-polyfill.svg?branch=master | ||
[travis-url]: https://travis-ci.org/sarosia/buffer-indexof-polyfill |
@@ -43,3 +43,3 @@ "use strict"; | ||
}); | ||
it("Number as value", function () { | ||
@@ -59,1 +59,72 @@ var buffer = new Buffer([ 1, 2, 3 ]); | ||
}); | ||
describe("Buffer#lastIndexOf", function () { | ||
it("Buffer as value", function () { | ||
var buffer = new Buffer("ABCABC"); | ||
expect(buffer.lastIndexOf(new Buffer("ABC"))).to.be.equal(3); | ||
expect(buffer.lastIndexOf(new Buffer("AB"))).to.be.equal(3); | ||
expect(buffer.lastIndexOf(new Buffer("BC"))).to.be.equal(4); | ||
expect(buffer.lastIndexOf(new Buffer("C"))).to.be.equal(5); | ||
expect(buffer.lastIndexOf(new Buffer("CC"))).to.be.equal(-1); | ||
expect(buffer.lastIndexOf(new Buffer("CA"))).to.be.equal(2); | ||
expect(buffer.lastIndexOf(new Buffer("ABC"), 1)).to.be.equal(0); | ||
expect(buffer.lastIndexOf(new Buffer("AB"), 1)).to.be.equal(0); | ||
expect(buffer.lastIndexOf(new Buffer("BC"), 1)).to.be.equal(1); | ||
expect(buffer.lastIndexOf(new Buffer("C"), 1)).to.be.equal(-1); | ||
expect(buffer.lastIndexOf(new Buffer("CC"), 1)).to.be.equal(-1); | ||
expect(buffer.lastIndexOf(new Buffer("CA"), 1)).to.be.equal(-1); | ||
}); | ||
it("String as value", function () { | ||
var buffer = new Buffer("ABCABC"); | ||
expect(buffer.lastIndexOf("ABC")).to.be.equal(3); | ||
expect(buffer.lastIndexOf("AB")).to.be.equal(3); | ||
expect(buffer.lastIndexOf("BC")).to.be.equal(4); | ||
expect(buffer.lastIndexOf("C")).to.be.equal(5); | ||
expect(buffer.lastIndexOf("CC")).to.be.equal(-1); | ||
expect(buffer.lastIndexOf("CA")).to.be.equal(2); | ||
expect(buffer.lastIndexOf("ABC", 1)).to.be.equal(0); | ||
expect(buffer.lastIndexOf("AB", 1)).to.be.equal(0); | ||
expect(buffer.lastIndexOf("BC", 1)).to.be.equal(1); | ||
expect(buffer.lastIndexOf("C", 1)).to.be.equal(-1); | ||
expect(buffer.lastIndexOf("CC", 1)).to.be.equal(-1); | ||
expect(buffer.lastIndexOf("CA", 1)).to.be.equal(-1); | ||
// make sure it works predictable | ||
buffer = buffer.toString(); | ||
expect(buffer.lastIndexOf("ABC")).to.be.equal(3); | ||
expect(buffer.lastIndexOf("AB")).to.be.equal(3); | ||
expect(buffer.lastIndexOf("BC")).to.be.equal(4); | ||
expect(buffer.lastIndexOf("C")).to.be.equal(5); | ||
expect(buffer.lastIndexOf("CC")).to.be.equal(-1); | ||
expect(buffer.lastIndexOf("CA")).to.be.equal(2); | ||
expect(buffer.lastIndexOf("ABC", 1)).to.be.equal(0); | ||
expect(buffer.lastIndexOf("AB", 1)).to.be.equal(0); | ||
expect(buffer.lastIndexOf("BC", 1)).to.be.equal(1); | ||
expect(buffer.lastIndexOf("C", 1)).to.be.equal(-1); | ||
expect(buffer.lastIndexOf("CC", 1)).to.be.equal(-1); | ||
expect(buffer.lastIndexOf("CA", 1)).to.be.equal(-1); | ||
}); | ||
it("Number as value", function () { | ||
var buffer = new Buffer([ 1, 2, 3, 1, 2, 3]); | ||
expect(buffer.lastIndexOf(1)).to.be.equal(3); | ||
expect(buffer.lastIndexOf(2)).to.be.equal(4); | ||
expect(buffer.lastIndexOf(3)).to.be.equal(5); | ||
expect(buffer.lastIndexOf(4)).to.be.equal(-1); | ||
expect(buffer.lastIndexOf(1, 1)).to.be.equal(0); | ||
expect(buffer.lastIndexOf(2, 1)).to.be.equal(1); | ||
expect(buffer.lastIndexOf(3, 1)).to.be.equal(-1); | ||
expect(buffer.lastIndexOf(4, 1)).to.be.equal(-1); | ||
}); | ||
}); |
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
11068
8
161
34