nigel
Advanced tools
Comparing version 1.0.1 to 2.0.0
@@ -0,7 +1,8 @@ | ||
'use strict'; | ||
// Load modules | ||
var Events = require('events'); | ||
var Stream = require('stream'); | ||
var Hoek = require('hoek'); | ||
var Vise = require('vise'); | ||
const Stream = require('stream'); | ||
const Hoek = require('hoek'); | ||
const Vise = require('vise'); | ||
@@ -11,3 +12,3 @@ | ||
var internals = {}; | ||
const internals = {}; | ||
@@ -20,3 +21,3 @@ | ||
var profile = { | ||
const profile = { | ||
value: needle, | ||
@@ -29,8 +30,8 @@ lastPos: needle.length - 1, | ||
for (var i = 0; i < 256; ++i) { | ||
for (let i = 0; i < 256; ++i) { | ||
profile.badCharShift[i] = profile.length; // Defaults to the full length of the needle | ||
} | ||
var last = profile.length - 1; | ||
for (i = 0; i < last; ++i) { // For each character in the needle (skip last since its position is already the default) | ||
const last = profile.length - 1; | ||
for (let i = 0; i < last; ++i) { // For each character in the needle (skip last since its position is already the default) | ||
profile.badCharShift[profile.value[i]] = last - i; | ||
@@ -50,4 +51,4 @@ } | ||
for (var i = start, il = haystack.length - needle.length; i <= il;) { // Has enough room to fit the entire needle | ||
var lastChar = haystack.readUInt8(i + needle.lastPos, true); | ||
for (let i = start; i <= haystack.length - needle.length;) { // Has enough room to fit the entire needle | ||
const lastChar = haystack.readUInt8(i + needle.lastPos, true); | ||
if (lastChar === needle.last && | ||
@@ -72,3 +73,3 @@ internals.startsWith(haystack, needle, i)) { | ||
for (var i = 0, il = needle.lastPos; i < il; ++i) { | ||
for (let i = 0; i < needle.lastPos; ++i) { | ||
if (needle.value[i] !== haystack.readUInt8(pos + i, true)) { | ||
@@ -88,6 +89,6 @@ return false; | ||
var matches = []; | ||
for (var last = start, hlen = haystack.length; last !== -1 && last < hlen;) { | ||
const matches = []; | ||
for (let last = start; last !== -1 && last < haystack.length;) { | ||
last = exports.horspool(haystack, needle, last) | ||
last = exports.horspool(haystack, needle, last); | ||
if (last !== -1) { | ||
@@ -107,3 +108,3 @@ matches.push(last); | ||
for (var i = 0, il = haystack.length - needle.length; i <= il; ++i) { // Has enough room to fit the entire needle | ||
for (let i = 0; i <= haystack.length - needle.length; ++i) { // Has enough room to fit the entire needle | ||
if (haystack.startsWith(needle.value, i)) { | ||
@@ -120,3 +121,3 @@ return i; | ||
var self = this; | ||
const self = this; | ||
@@ -129,12 +130,12 @@ Stream.Writable.call(this); | ||
this.on('finish', function () { | ||
this.on('finish', () => { | ||
// Flush out the remainder | ||
var chunks = self._haystack.chunks(); | ||
for (var i = 0, il = chunks.length; i < il; ++i) { | ||
const chunks = self._haystack.chunks(); | ||
for (let i = 0; i < chunks.length; ++i) { | ||
self.emit('haystack', chunks[i]); | ||
} | ||
setImmediate(function () { // Give pending events a chance to fire | ||
setImmediate(() => { // Give pending events a chance to fire | ||
@@ -159,3 +160,3 @@ self.emit('close'); | ||
var match = this._indexOf(this._haystack, this._needle); | ||
let match = this._indexOf(this._haystack, this._needle); | ||
if (match === -1 && | ||
@@ -176,4 +177,5 @@ chunk.length >= this._needle.length) { | ||
if (this._haystack.length) { | ||
var notChecked = this._haystack.length - this._needle.length + 1; // Not enough space for Horspool | ||
for (var i = notChecked; i < this._haystack.length; ++i) { | ||
const notChecked = this._haystack.length - this._needle.length + 1; // Not enough space for Horspool | ||
let i = notChecked; | ||
for (; i < this._haystack.length; ++i) { | ||
if (this._haystack.startsWith(this._needle.value, i, this._haystack.length - i)) { | ||
@@ -193,4 +195,4 @@ break; | ||
var chunks = this._haystack.shift(pos); | ||
for (var i = 0, il = chunks.length; i < il; ++i) { | ||
const chunks = this._haystack.shift(pos); | ||
for (let i = 0; i < chunks.length; ++i) { | ||
this.emit('haystack', chunks[i]); | ||
@@ -203,6 +205,6 @@ } | ||
var chunks = this._haystack.shift(this._haystack.length); | ||
for (var i = 0, il = chunks.length; i < il; ++i) { | ||
const chunks = this._haystack.shift(this._haystack.length); | ||
for (let i = 0; i < chunks.length; ++i) { | ||
this.emit('haystack', chunks[i]); | ||
} | ||
}; |
{ | ||
"name": "nigel", | ||
"description": "BoyerMooreHorspool algorithms", | ||
"version": "1.0.1", | ||
"version": "2.0.0", | ||
"repository": "git://github.com/hapijs/nigel", | ||
"main": "index", | ||
"main": "lib/index.js", | ||
"keywords": [ | ||
@@ -15,20 +15,17 @@ "boyer-moore-horspool", | ||
"engines": { | ||
"node": ">=0.10.32" | ||
"node": ">=4.0.0" | ||
}, | ||
"dependencies": { | ||
"hoek": "2.x.x", | ||
"vise": "1.x.x" | ||
"hoek": "3.x.x", | ||
"vise": "2.x.x" | ||
}, | ||
"devDependencies": { | ||
"lab": "4.x.x" | ||
"code": "2.x.x", | ||
"lab": "7.x.x" | ||
}, | ||
"scripts": { | ||
"test": "make test-cov" | ||
"test": "lab -a code -t 100 -L", | ||
"test-cov-html": "lab -a code -r html -o coverage.html" | ||
}, | ||
"licenses": [ | ||
{ | ||
"type": "BSD", | ||
"url": "http://github.com/hapijs/nigel/raw/master/LICENSE" | ||
} | ||
] | ||
"license": "BSD-3-Clause" | ||
} |
@@ -0,6 +1,9 @@ | ||
'use strict'; | ||
// Load modules | ||
var Lab = require('lab'); | ||
var Nigel = require('..'); | ||
var Vise = require('vise'); | ||
const Code = require('code'); | ||
const Lab = require('lab'); | ||
const Nigel = require('..'); | ||
const Vise = require('vise'); | ||
@@ -10,3 +13,3 @@ | ||
var internals = {}; | ||
const internals = {}; | ||
@@ -16,13 +19,13 @@ | ||
var lab = exports.lab = Lab.script(); | ||
var describe = lab.describe; | ||
var it = lab.it; | ||
var expect = Lab.expect; | ||
const lab = exports.lab = Lab.script(); | ||
const describe = lab.describe; | ||
const it = lab.it; | ||
const expect = Code.expect; | ||
describe('compile()', function () { | ||
describe('compile()', () => { | ||
it('processes needle', function (done) { | ||
it('processes needle', (done) => { | ||
var needle = new Buffer('abcdefghijklmnopqrstuvwxyz'); | ||
const needle = new Buffer('abcdefghijklmnopqrstuvwxyz'); | ||
expect(Nigel.compile(needle)).to.deep.equal({ | ||
@@ -39,5 +42,5 @@ value: needle, | ||
it('throws on empty needle', function (done) { | ||
it('throws on empty needle', (done) => { | ||
expect(function () { | ||
expect(() => { | ||
@@ -50,5 +53,5 @@ Nigel.compile(new Buffer('')); | ||
it('throws on empty needle', function (done) { | ||
it('throws on empty needle', (done) => { | ||
expect(function () { | ||
expect(() => { | ||
@@ -62,8 +65,8 @@ Nigel.compile(); | ||
describe('horspool()', function () { | ||
describe('horspool()', () => { | ||
it('finds needle', function (done) { | ||
it('finds needle', (done) => { | ||
var haystack = new Buffer('abcdefghijklmnopqrstuvwxyz'); | ||
var needle = new Buffer('mnopq'); | ||
const haystack = new Buffer('abcdefghijklmnopqrstuvwxyz'); | ||
const needle = new Buffer('mnopq'); | ||
@@ -74,6 +77,6 @@ expect(Nigel.horspool(haystack, needle)).to.equal(12); | ||
it('does not find needle', function (done) { | ||
it('does not find needle', (done) => { | ||
var haystack = new Buffer('abcdefghijklmnopqrstuvwxyz'); | ||
var needle = new Buffer('mnxpq'); | ||
const haystack = new Buffer('abcdefghijklmnopqrstuvwxyz'); | ||
const needle = new Buffer('mnxpq'); | ||
@@ -84,6 +87,6 @@ expect(Nigel.horspool(haystack, needle)).to.equal(-1); | ||
it('does not find needle (tail match)', function (done) { | ||
it('does not find needle (tail match)', (done) => { | ||
var haystack = new Buffer('q2q2q2q2q'); | ||
var needle = new Buffer('22q'); | ||
const haystack = new Buffer('q2q2q2q2q'); | ||
const needle = new Buffer('22q'); | ||
@@ -94,6 +97,6 @@ expect(Nigel.horspool(haystack, needle)).to.equal(-1); | ||
it('finds needle from position', function (done) { | ||
it('finds needle from position', (done) => { | ||
var haystack = new Buffer('abcdefghijklmnopqrstuvwxyz'); | ||
var needle = new Buffer('mnopq'); | ||
const haystack = new Buffer('abcdefghijklmnopqrstuvwxyz'); | ||
const needle = new Buffer('mnopq'); | ||
@@ -104,6 +107,6 @@ expect(Nigel.horspool(haystack, needle, 11)).to.equal(12); | ||
it('does not find needle from position', function (done) { | ||
it('does not find needle from position', (done) => { | ||
var haystack = new Buffer('abcdefghijklmnopqrstuvwxyz'); | ||
var needle = new Buffer('mnopq'); | ||
const haystack = new Buffer('abcdefghijklmnopqrstuvwxyz'); | ||
const needle = new Buffer('mnopq'); | ||
@@ -114,5 +117,5 @@ expect(Nigel.horspool(haystack, needle, 13)).to.equal(-1); | ||
it('finds needle in vise haystack', function (done) { | ||
it('finds needle in vise haystack', (done) => { | ||
var haystack = new Vise([new Buffer('abcdefghijklmn'), new Buffer('opqrstuvwxyz')]); | ||
const haystack = new Vise([new Buffer('abcdefghijklmn'), new Buffer('opqrstuvwxyz')]); | ||
expect(Nigel.horspool(haystack, new Buffer('mnopq'))).to.equal(12); | ||
@@ -122,5 +125,5 @@ done(); | ||
it('finds needle in pushed vise haystack', function (done) { | ||
it('finds needle in pushed vise haystack', (done) => { | ||
var haystack = new Vise(); | ||
const haystack = new Vise(); | ||
haystack.push(new Buffer('abcdefghijklmn')); | ||
@@ -133,8 +136,8 @@ haystack.push(new Buffer('opqrstuvwxyz')); | ||
describe('all()', function () { | ||
describe('all()', () => { | ||
it('finds needle', function (done) { | ||
it('finds needle', (done) => { | ||
var haystack = new Buffer('abcdefghijklmnopqrstuvwxyz'); | ||
var needle = new Buffer('mnopq'); | ||
const haystack = new Buffer('abcdefghijklmnopqrstuvwxyz'); | ||
const needle = new Buffer('mnopq'); | ||
@@ -145,6 +148,6 @@ expect(Nigel.all(haystack, needle)).to.deep.equal([12]); | ||
it('does not find needle', function (done) { | ||
it('does not find needle', (done) => { | ||
var haystack = new Buffer('abcdefghijklmnopqrstuvwxyz'); | ||
var needle = new Buffer('mno2pq'); | ||
const haystack = new Buffer('abcdefghijklmnopqrstuvwxyz'); | ||
const needle = new Buffer('mno2pq'); | ||
@@ -155,6 +158,6 @@ expect(Nigel.all(haystack, needle)).to.deep.equal([]); | ||
it('finds multiple needles', function (done) { | ||
it('finds multiple needles', (done) => { | ||
var haystack = new Buffer('abc123def123ghi123jkl123mno123pqr123stu123vwx123yz'); | ||
var needle = new Buffer('123'); | ||
const haystack = new Buffer('abc123def123ghi123jkl123mno123pqr123stu123vwx123yz'); | ||
const needle = new Buffer('123'); | ||
@@ -165,6 +168,6 @@ expect(Nigel.all(haystack, needle)).to.deep.equal([3, 9, 15, 21, 27, 33, 39, 45]); | ||
it('finds multiple needles from position', function (done) { | ||
it('finds multiple needles from position', (done) => { | ||
var haystack = new Buffer('abc123def123ghi123jkl123mno123pqr123stu123vwx123yz'); | ||
var needle = new Buffer('123'); | ||
const haystack = new Buffer('abc123def123ghi123jkl123mno123pqr123stu123vwx123yz'); | ||
const needle = new Buffer('123'); | ||
@@ -176,10 +179,10 @@ expect(Nigel.all(haystack, needle, 11)).to.deep.equal([15, 21, 27, 33, 39, 45]); | ||
describe('Stream', function () { | ||
describe('Stream', () => { | ||
it('parses a stream haystack', function (done) { | ||
it('parses a stream haystack', (done) => { | ||
var result = []; | ||
const result = []; | ||
var stream = new Nigel.Stream(new Buffer('123')); | ||
stream.on('close', function () { | ||
const stream = new Nigel.Stream(new Buffer('123')); | ||
stream.on('close', () => { | ||
@@ -190,3 +193,3 @@ expect(result).to.deep.equal(['abc', 1, 'de', 'fg', 1, 'hij1', 1, 'klm', 1, 'nop']); | ||
stream.on('needle', function () { | ||
stream.on('needle', () => { | ||
@@ -196,3 +199,3 @@ result.push(1); | ||
stream.on('haystack', function (chunk) { | ||
stream.on('haystack', (chunk) => { | ||
@@ -211,8 +214,8 @@ result.push(chunk.toString()); | ||
it('flushes data buffers when more recent one is bigger than needle', function (done) { | ||
it('flushes data buffers when more recent one is bigger than needle', (done) => { | ||
var result = []; | ||
const result = []; | ||
var stream = new Nigel.Stream(new Buffer('123')); | ||
stream.on('close', function () { | ||
const stream = new Nigel.Stream(new Buffer('123')); | ||
stream.on('close', () => { | ||
@@ -223,3 +226,3 @@ expect(result).to.deep.equal(['abc', null, 'de', 'fghij', 'klmnop', 'q', null, 'r', 'stuv', 'wxy', 'zabc']); | ||
stream.on('needle', function () { | ||
stream.on('needle', () => { | ||
@@ -229,3 +232,3 @@ result.push(null); | ||
stream.on('haystack', function (chunk, g) { | ||
stream.on('haystack', (chunk, g) => { | ||
@@ -246,8 +249,8 @@ expect(stream._haystack.length).to.be.lessThan(7); | ||
it('parses a stream haystack (partial needle first)', function (done) { | ||
it('parses a stream haystack (partial needle first)', (done) => { | ||
var result = []; | ||
const result = []; | ||
var stream = new Nigel.Stream(new Buffer('123')); | ||
stream.on('close', function () { | ||
const stream = new Nigel.Stream(new Buffer('123')); | ||
stream.on('close', () => { | ||
@@ -258,3 +261,3 @@ expect(result).to.deep.equal([1, 'abc', 1, 'de', 'fg', 1, 'hij1', 1, 'klm', 1, 'nop']); | ||
stream.on('needle', function () { | ||
stream.on('needle', () => { | ||
@@ -264,3 +267,3 @@ result.push(1); | ||
stream.on('haystack', function (chunk) { | ||
stream.on('haystack', (chunk) => { | ||
@@ -280,8 +283,8 @@ result.push(chunk.toString()); | ||
it('parses a stream haystack (partial needle last)', function (done) { | ||
it('parses a stream haystack (partial needle last)', (done) => { | ||
var result = []; | ||
const result = []; | ||
var stream = new Nigel.Stream(new Buffer('123')); | ||
stream.on('close', function () { | ||
const stream = new Nigel.Stream(new Buffer('123')); | ||
stream.on('close', () => { | ||
@@ -292,3 +295,3 @@ expect(result).to.deep.equal([1, 'abc', 1, 'de', 'fg', 1, 'hij1', 1, 'klm', 1, 'nop', '1']); | ||
stream.on('needle', function () { | ||
stream.on('needle', () => { | ||
@@ -298,3 +301,3 @@ result.push(1); | ||
stream.on('haystack', function (chunk) { | ||
stream.on('haystack', (chunk) => { | ||
@@ -314,10 +317,10 @@ result.push(chunk.toString()); | ||
describe('needle()', function () { | ||
describe('needle()', () => { | ||
it('changes needle mid stream', function (done) { | ||
it('changes needle mid stream', (done) => { | ||
var result = []; | ||
const result = []; | ||
var stream = new Nigel.Stream(new Buffer('123')); | ||
stream.on('close', function () { | ||
const stream = new Nigel.Stream(new Buffer('123')); | ||
stream.on('close', () => { | ||
@@ -328,3 +331,3 @@ expect(result).to.deep.equal([1, 'abc', 1, 'de', 'fg', '12', '3hi', 1, 'j11', '23klm', '123', 'no', 1, 'p1']); | ||
stream.on('needle', function () { | ||
stream.on('needle', () => { | ||
@@ -334,3 +337,3 @@ result.push(1); | ||
stream.on('haystack', function (chunk) { | ||
stream.on('haystack', (chunk) => { | ||
@@ -351,8 +354,8 @@ result.push(chunk.toString()); | ||
it('changes needle mid stream (on haystack)', function (done) { | ||
it('changes needle mid stream (on haystack)', (done) => { | ||
var result = []; | ||
const result = []; | ||
var stream = new Nigel.Stream(new Buffer('123')); | ||
stream.on('close', function () { | ||
const stream = new Nigel.Stream(new Buffer('123')); | ||
stream.on('close', () => { | ||
@@ -363,3 +366,3 @@ expect(result).to.deep.equal([1, 'abc', 1, 'de', 'fg', /**/ '12', '3hi', 1, 'j11', '23klm', '123', 'no', 1, 'p1']); | ||
stream.on('needle', function () { | ||
stream.on('needle', () => { | ||
@@ -369,3 +372,3 @@ result.push(1); | ||
stream.on('haystack', function (chunk) { | ||
stream.on('haystack', (chunk) => { | ||
@@ -388,8 +391,8 @@ result.push(chunk.toString()); | ||
it('changes needle mid stream (on needle)', function (done) { | ||
it('changes needle mid stream (on needle)', (done) => { | ||
var result = []; | ||
const result = []; | ||
var stream = new Nigel.Stream(new Buffer('12')); | ||
stream.on('close', function () { | ||
const stream = new Nigel.Stream(new Buffer('12')); | ||
stream.on('close', () => { | ||
@@ -400,3 +403,3 @@ expect(result).to.deep.equal(['a', 1, /**/ '3abc', 1, 'de', 'fg', 1, 'hi45j1', 1, 'klm', 1, 'no45p', '1']); | ||
stream.on('needle', function () { | ||
stream.on('needle', () => { | ||
@@ -409,3 +412,3 @@ result.push(1); | ||
stream.on('haystack', function (chunk) { | ||
stream.on('haystack', (chunk) => { | ||
@@ -425,8 +428,8 @@ result.push(chunk.toString()); | ||
it('retains partial needle before needle', function (done) { | ||
it('retains partial needle before needle', (done) => { | ||
var result = []; | ||
const result = []; | ||
var stream = new Nigel.Stream(new Buffer('\r\n')); | ||
stream.on('close', function () { | ||
const stream = new Nigel.Stream(new Buffer('\r\n')); | ||
stream.on('close', () => { | ||
@@ -437,3 +440,3 @@ expect(result).to.deep.equal(['abc', 1, 'defg', 1, 1, 'hijk\r', 1, 'lmnop\r', 1]); | ||
stream.on('needle', function () { | ||
stream.on('needle', () => { | ||
@@ -443,3 +446,3 @@ result.push(1); | ||
stream.on('haystack', function (chunk) { | ||
stream.on('haystack', (chunk) => { | ||
@@ -453,11 +456,11 @@ result.push(chunk.toString()); | ||
it('emits events in correct order when nesting streams', function (done) { | ||
it('emits events in correct order when nesting streams', (done) => { | ||
var test = '1x2|3|4x|5|6|x7'; | ||
var result = ''; | ||
const test = '1x2|3|4x|5|6|x7'; | ||
let result = ''; | ||
var x = new Nigel.Stream(new Buffer('x')); | ||
var l = new Nigel.Stream(new Buffer('|')); | ||
const x = new Nigel.Stream(new Buffer('x')); | ||
const l = new Nigel.Stream(new Buffer('|')); | ||
x.once('close', function () { | ||
x.once('close', () => { | ||
@@ -467,3 +470,3 @@ l.end(); | ||
l.once('close', function () { | ||
l.once('close', () => { | ||
@@ -474,8 +477,8 @@ expect(result).to.equal(test.replace(/\|/g, '[').replace(/x/g, '*')); | ||
x.on('needle', function () { | ||
x.on('needle', () => { | ||
result += '*'; | ||
result = result + '*'; | ||
}); | ||
x.on('haystack', function (chunk) { | ||
x.on('haystack', (chunk) => { | ||
@@ -485,10 +488,10 @@ l.write(chunk); | ||
l.on('needle', function () { | ||
l.on('needle', () => { | ||
result += '['; | ||
result = result + '['; | ||
}); | ||
l.on('haystack', function (chunk) { | ||
l.on('haystack', (chunk) => { | ||
result += chunk.toString(); | ||
result = result + chunk.toString(); | ||
}); | ||
@@ -501,13 +504,13 @@ | ||
describe('flush()', function () { | ||
describe('flush()', () => { | ||
it('emits events in correct order when nesting streams (partial needle)', function (done) { | ||
it('emits events in correct order when nesting streams (partial needle)', (done) => { | ||
var test = '7vx7vx7vx'; | ||
var result = ''; | ||
const test = '7vx7vx7vx'; | ||
let result = ''; | ||
var x = new Nigel.Stream(new Buffer('x')); | ||
var l = new Nigel.Stream(new Buffer('v|')); | ||
const x = new Nigel.Stream(new Buffer('x')); | ||
const l = new Nigel.Stream(new Buffer('v|')); | ||
x.once('close', function () { | ||
x.once('close', () => { | ||
@@ -517,3 +520,3 @@ l.end(); | ||
l.once('close', function () { | ||
l.once('close', () => { | ||
@@ -524,9 +527,9 @@ expect(result).to.equal(test.replace(/v\|/g, '[').replace(/x/g, '*')); | ||
x.on('needle', function () { | ||
x.on('needle', () => { | ||
l.flush(); | ||
result += '*'; | ||
result = result + '*'; | ||
}); | ||
x.on('haystack', function (chunk) { | ||
x.on('haystack', (chunk) => { | ||
@@ -536,10 +539,10 @@ l.write(chunk); | ||
l.on('needle', function () { | ||
l.on('needle', () => { | ||
result += '['; | ||
result = result + '['; | ||
}); | ||
l.on('haystack', function (chunk) { | ||
l.on('haystack', (chunk) => { | ||
result += chunk.toString(); | ||
result = result + chunk.toString(); | ||
}); | ||
@@ -551,2 +554,2 @@ | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
480
22613
2
8
+ Addedhoek@3.0.44.3.1(transitive)
+ Addedvise@2.1.1(transitive)
- Removedhoek@2.16.3(transitive)
- Removedvise@1.0.0(transitive)
Updatedhoek@3.x.x
Updatedvise@2.x.x