Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

vise

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vise - npm Package Compare versions

Comparing version 0.0.0 to 1.0.0

96

lib/index.js

@@ -16,3 +16,2 @@ // Load modules

this._offset = 0;
this._last = null; // Keep track of last looked to speed up consecutive searches

@@ -30,12 +29,12 @@ if (chunks) {

Hoek.assert(typeof chunk === 'string' || Buffer.isBuffer(chunk), 'Chunk must be string or buffer');
Hoek.assert(Buffer.isBuffer(chunk), 'Chunk must be a buffer');
this._chunks.push({
var item = {
data: chunk,
isBuffer: typeof chunk !== 'string',
length: chunk.length,
offset: this.length,
offset: this.length + this._offset,
index: this._chunks.length
});
};
this._chunks.push(item);
this.length += chunk.length;

@@ -48,9 +47,9 @@ };

if (!length) {
return;
return [];
}
var prevOffset = this._offset;
var item = this._chunkAt(length);
var dropTo = this._chunks.length;
this._last = null;
this._offset = 0;

@@ -65,6 +64,19 @@

var chunks = [];
for (var i = 0; i < dropTo; ++i) {
this._chunks.shift();
var chunk = this._chunks.shift();
if (i === 0 &&
prevOffset) {
chunks.push(chunk.data.slice(prevOffset));
}
else {
chunks.push(chunk.data);
}
}
if (this._offset) {
chunks.push(item.chunk.data.slice(dropTo ? 0 : prevOffset, this._offset));
}
// Recalculate existing chunks

@@ -82,9 +94,11 @@

this.length -= this._offset;
return chunks;
};
internals.Vise.prototype.charCodeAt = function (pos) {
internals.Vise.prototype.at = internals.Vise.prototype.readUInt8 = function (pos) {
var item = this._chunkAt(pos);
return (item ? (item.chunk.isBuffer ? item.chunk.data[item.offset] : item.chunk.data.charCodeAt(item.offset)) : undefined);
return item ? item.chunk.data[item.offset] : undefined;
};

@@ -101,13 +115,6 @@

var start = 0;
var offset = (this._last ? pos - this._last.offset : -1);
if (offset >= 0) {
start = this._last.index + (offset < this._last.length ? 0 : 1);
}
for (var i = start, il = this._chunks.length; i < il; ++i) {
for (var i = 0, il = this._chunks.length; i < il; ++i) {
var chunk = this._chunks[i];
offset = pos - chunk.offset;
var offset = pos - chunk.offset;
if (offset < chunk.length) {
this._last = chunk;
return { chunk: chunk, offset: offset };

@@ -119,1 +126,50 @@ }

};
internals.Vise.prototype.chunks = function () {
var chunks = [];
for (var i = 0, il = this._chunks.length; i < il; ++i) {
var chunk = this._chunks[i];
if (i === 0 &&
this._offset) {
chunks.push(chunk.data.slice(this._offset));
}
else {
chunks.push(chunk.data);
}
}
return chunks;
};
internals.Vise.prototype.startsWith = function (value, pos, length) {
pos = pos || 0;
length = length ? Math.min(value.length, length) : value.length;
if (pos + length > this.length) { // Not enough length to fit value
return false;
}
var start = this._chunkAt(pos);
if (!start) {
return false;
}
for (var k = start.chunk.index, kl = this._chunks.length, j = 0; k < kl && j < length; ++k) {
var chunk = this._chunks[k];
var offset = (k === start.chunk.index ? start.offset : 0);
for (var i = offset, il = chunk.length; i < il && j < length; ++i, ++j) {
if (chunk.data[i] !== value[j]) {
return false;
}
}
}
return true;
};
{
"name": "vise",
"description": "Treat multiple buffers or strings as one",
"version": "0.0.0",
"description": "Treat multiple buffers as one",
"version": "1.0.0",
"repository": "git://github.com/hapijs/vise",

@@ -9,3 +9,2 @@ "main": "index",

"buffer",
"string",
"array",

@@ -12,0 +11,0 @@ "merge",

#vise
Treat multiple buffers or strings as one.
Treat multiple buffers as one.

@@ -5,0 +5,0 @@ [![Build Status](https://secure.travis-ci.org/hapijs/vise.png)](http://travis-ci.org/hapijs/vise)

@@ -26,24 +26,16 @@ // Load modules

expect(vise.charCodeAt(content.length)).to.equal(undefined);
expect(vise.charCodeAt(content.length + 1)).to.equal(undefined);
expect(vise.charCodeAt(content.length + 100)).to.equal(undefined);
expect(vise.charCodeAt(-1)).to.equal(undefined);
expect(vise.at(content.length)).to.equal(undefined);
expect(vise.at(content.length + 1)).to.equal(undefined);
expect(vise.at(content.length + 100)).to.equal(undefined);
expect(vise.at(-1)).to.equal(undefined);
for (var i = 0, il = content.length; i < il; ++i) {
expect(vise.charCodeAt(i)).to.equal(content.charCodeAt(i));
expect(vise.at(i)).to.equal(content.charCodeAt(i));
}
for (i = content.length - 1; i >= 0; --i) {
expect(vise.charCodeAt(i)).to.equal(content.charCodeAt(i));
expect(vise.at(i)).to.equal(content.charCodeAt(i));
}
};
it('combines strings', function (done) {
var data = ['abcde', 'fgh', 'ijk'];
var vise = new Vise(data);
validate(vise, 'abcdefghijk');
done();
});
it('combines buffers', function (done) {

@@ -57,18 +49,2 @@

it('combines buffers and strings', function (done) {
var data = ['abcde', new Buffer('fgh'), 'ijk'];
var vise = new Vise(data);
validate(vise, 'abcdefghijk');
done();
});
it('combines single string', function (done) {
var data = 'abcde';
var vise = new Vise(data);
validate(vise, 'abcde');
done();
});
it('combines single buffer', function (done) {

@@ -87,3 +63,3 @@

expect(vise.length).to.equal(0);
expect(vise.charCodeAt(0)).to.equal(undefined);
expect(vise.at(0)).to.equal(undefined);
done();

@@ -97,3 +73,3 @@ });

new Vise(123);
}).to.throw('Chunk must be string or buffer');
}).to.throw('Chunk must be a buffer');

@@ -103,2 +79,12 @@ done();

describe('length', function () {
it('reflects total legnth', function (done) {
var vise = new Vise([new Buffer('abcdefghijklmn'), new Buffer('opqrstuvwxyz')]);
expect(vise.length).to.equal(26);
done();
});
});
describe('push()', function () {

@@ -108,7 +94,7 @@

var data = ['abcde', 'fgh'];
var data = [new Buffer('abcde'), new Buffer('fgh')];
var vise = new Vise(data);
validate(vise, 'abcdefgh');
vise.push('ijk');
vise.push(new Buffer('ijk'));
validate(vise, 'abcdefghijk');

@@ -122,4 +108,4 @@ done();

expect(vise.length).to.equal(0);
expect(vise.charCodeAt(0)).to.equal(undefined);
vise.push('abcde');
expect(vise.at(0)).to.equal(undefined);
vise.push(new Buffer('abcde'));
validate(vise, 'abcde');

@@ -134,22 +120,22 @@ done();

var data = ['abcde', 'fgh', 'ijk'];
var data = [new Buffer('abcde'), new Buffer('fgh'), new Buffer('ijk')];
var vise = new Vise(data);
validate(vise, 'abcdefghijk');
vise.shift(2);
expect(vise.shift(2)).to.deep.equal([new Buffer('ab')]);
validate(vise, 'cdefghijk');
vise.shift(2);
expect(vise.shift(2)).to.deep.equal([new Buffer('cd')]);
validate(vise, 'efghijk');
vise.shift(0);
expect(vise.shift(0)).to.deep.equal([]);
validate(vise, 'efghijk');
vise.shift(1);
expect(vise.shift(1)).to.deep.equal([new Buffer('e')]);
validate(vise, 'fghijk');
vise.shift(4);
expect(vise.shift(4)).to.deep.equal([new Buffer('fgh'), new Buffer('i')]);
validate(vise, 'jk');
vise.shift(4);
expect(vise.shift(4)).to.deep.equal([new Buffer('jk')]);
validate(vise, '');

@@ -159,3 +145,125 @@

});
it('keeps track of chunks offset', function (done) {
var vise = new Vise();
vise.push(new Buffer('acb123de'));
vise.shift(3);
vise.shift(3);
vise.push(new Buffer('fg12'));
vise.push(new Buffer('3hij1'));
validate(vise, 'defg123hij1');
done();
});
it('removes multiple chunks', function (done) {
var data = [new Buffer('abcde'), new Buffer('fgh'), new Buffer('ijk')];
var vise = new Vise(data);
validate(vise, 'abcdefghijk');
vise.shift(10);
validate(vise, 'k');
done();
});
});
describe('chunks()', function (done) {
it('returns remaining chunks', function (done) {
var data = [new Buffer('abcde'), new Buffer('fgh'), new Buffer('ijk')];
var vise = new Vise(data);
expect(vise.chunks()).to.deep.equal(data);
vise.shift(2);
expect(vise.chunks()).to.deep.equal([new Buffer('cde'), new Buffer('fgh'), new Buffer('ijk')]);
vise.shift(2);
expect(vise.chunks()).to.deep.equal([new Buffer('e'), new Buffer('fgh'), new Buffer('ijk')]);
vise.shift(0);
expect(vise.chunks()).to.deep.equal([new Buffer('e'), new Buffer('fgh'), new Buffer('ijk')]);
vise.shift(1);
expect(vise.chunks()).to.deep.equal([new Buffer('fgh'), new Buffer('ijk')]);
vise.shift(4);
expect(vise.chunks()).to.deep.equal([new Buffer('jk')]);
vise.shift(4);
expect(vise.chunks()).to.deep.equal([]);
done();
});
});
describe('startsWith()', function () {
it('compares single chunk (smaller)', function (done) {
var vise = new Vise(new Buffer('abcdefghijkl'));
expect(vise.startsWith(new Buffer('abcd'))).to.equal(true);
done();
});
it('compares single chunk (subset)', function (done) {
var vise = new Vise(new Buffer('abcdefghijkl'));
expect(vise.startsWith(new Buffer('abce'), 0, 3)).to.equal(true);
done();
});
it('compares single chunk (different)', function (done) {
var vise = new Vise(new Buffer('abcdefghijkl'));
expect(vise.startsWith(new Buffer('asd'))).to.equal(false);
done();
});
it('compares single chunk (offset)', function (done) {
var vise = new Vise(new Buffer('abcdefghijkl'));
expect(vise.startsWith(new Buffer('bcd'), 1)).to.equal(true);
done();
});
it('compares single chunk (same)', function (done) {
var vise = new Vise(new Buffer('abcdefghijkl'));
expect(vise.startsWith(new Buffer('abcdefghijkl'))).to.equal(true);
done();
});
it('compares single chunk (bigger)', function (done) {
var vise = new Vise(new Buffer('abcdefghijkl'));
expect(vise.startsWith(new Buffer('abcdefghijklx'))).to.equal(false);
done();
});
it('compares multiple chunks', function (done) {
var vise = new Vise([new Buffer('a'), new Buffer('b'), new Buffer('cdefghijkl')]);
expect(vise.startsWith(new Buffer('abcd'))).to.equal(true);
done();
});
it('compares multiple chunks (mismatch)', function (done) {
var vise = new Vise([new Buffer('a'), new Buffer('b'), new Buffer('cdefghijkl')]);
expect(vise.startsWith(new Buffer('acd'))).to.equal(false);
done();
});
it('compares with invalid offset', function (done) {
var vise = new Vise(new Buffer('abcdefghijkl'));
expect(vise.startsWith(new Buffer('bcd'), -1)).to.equal(false);
done();
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc