Comparing version 1.0.2 to 2.0.0
@@ -10,3 +10,9 @@ var ltgt = require('ltgt') | ||
} | ||
, ensureBuffer = function (value) { | ||
if (value && !Buffer.isBuffer(value)) | ||
value = new Buffer(value) | ||
return value | ||
} | ||
Keydir.prototype._sortedIndexOf = function (key) { | ||
@@ -19,3 +25,3 @@ var low = 0 | ||
mid = (low + high) >>> 1 | ||
this._keys[mid] < key ? low = mid + 1 : high = mid | ||
ltgt.compare(this._keys[mid], key) < 0 ? low = mid + 1 : high = mid | ||
} | ||
@@ -26,5 +32,7 @@ return low | ||
Keydir.prototype.put = function (key) { | ||
key = ensureBuffer(key) | ||
var ix = this._sortedIndexOf(key) | ||
if (ix >= this.len || this._keys[ix] !== key) { | ||
if (ix >= this._len || ltgt.compare(this._keys[ix], key) !== 0) { | ||
this._keys.splice(ix, 0, key) | ||
@@ -36,4 +44,7 @@ this._len++ | ||
Keydir.prototype.del = function (key) { | ||
key = ensureBuffer(key) | ||
var ix = this._sortedIndexOf(key) | ||
if (this._keys[ix] == key) { | ||
if (ltgt.compare(this._keys[ix], key) === 0) { | ||
this._keys.splice(ix, 1) | ||
@@ -44,2 +55,7 @@ this._len--; | ||
Keydir.prototype.clear = function () { | ||
this._keys.length = 0 | ||
this._len = 0 | ||
} | ||
Keydir.prototype.keys = function () { | ||
@@ -50,6 +66,6 @@ return this._keys | ||
Keydir.prototype._rangeIndexes = function (options) { | ||
var lowerBound = ltgt.lowerBound(options) | ||
var lowerBound = ensureBuffer(ltgt.lowerBound(options)) | ||
, fromIdx = lowerBound ? | ||
this._sortedIndexOf(lowerBound) : 0 | ||
, upperBound = ltgt.upperBound(options) | ||
, upperBound = ensureBuffer(ltgt.upperBound(options)) | ||
// toIdx - the id to slice to (exclusive) | ||
@@ -59,11 +75,16 @@ , toIdx = upperBound ? | ||
if (ltgt.lowerBoundExclusive(options) && this._keys[fromIdx] === lowerBound) | ||
if (ltgt.lowerBoundExclusive(options) && | ||
ltgt.compare(this._keys[fromIdx], lowerBound) === 0) { | ||
fromIdx++ | ||
} | ||
// behave correcly when the upperBound is between two keys | ||
if (upperBound && this._keys[toIdx - 1] > upperBound) | ||
if (upperBound && ltgt.compare(this._keys[toIdx - 1], upperBound) > 0) { | ||
toIdx-- | ||
} | ||
if (ltgt.upperBoundExclusive(options) && this._keys[toIdx - 1] === upperBound) | ||
else if (ltgt.upperBoundExclusive(options) && | ||
ltgt.compare(this._keys[toIdx - 1], upperBound) === 0) { | ||
toIdx-- | ||
} | ||
@@ -77,2 +98,3 @@ if (options.limit && options.limit !== -1) { | ||
} | ||
return { from: fromIdx, to: toIdx } | ||
@@ -79,0 +101,0 @@ } |
{ | ||
"name": "keydir", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"description": "A sorted list of keys with support for level-* ranges", | ||
@@ -5,0 +5,0 @@ "main": "keydir.js", |
105
test.js
var test = require('tape') | ||
, keydir = require('./keydir') | ||
, transformArray = function (array) { | ||
return array.map(function (string) { return new Buffer(string) }) | ||
} | ||
@@ -10,3 +13,3 @@ test('put()', function (t) { | ||
dir.keys() | ||
, [] | ||
, transformArray([]) | ||
) | ||
@@ -17,3 +20,3 @@ | ||
dir.keys() | ||
, [ 'foo' ] | ||
, transformArray([ 'foo' ]) | ||
) | ||
@@ -24,3 +27,3 @@ | ||
dir.keys() | ||
, [ 'bar', 'foo' ] | ||
, transformArray([ 'bar', 'foo' ]) | ||
) | ||
@@ -31,3 +34,3 @@ | ||
dir.keys() | ||
, [ 'bar', 'foo' ] | ||
, transformArray([ 'bar', 'foo' ]) | ||
) | ||
@@ -38,3 +41,3 @@ | ||
dir.keys() | ||
, [ 'bar', 'bas', 'foo' ] | ||
, transformArray([ 'bar', 'bas', 'foo' ]) | ||
) | ||
@@ -56,3 +59,3 @@ | ||
dir.keys() | ||
, [ 'bar', 'baz', 'foo' ] | ||
, transformArray([ 'bar', 'baz', 'foo' ]) | ||
) | ||
@@ -62,3 +65,3 @@ dir.del('foo') | ||
dir.keys() | ||
, [ 'bar', 'baz' ] | ||
, transformArray([ 'bar', 'baz' ]) | ||
) | ||
@@ -68,3 +71,3 @@ dir.del('bar') | ||
dir.keys() | ||
, [ 'baz' ] | ||
, transformArray([ 'baz' ]) | ||
) | ||
@@ -74,2 +77,12 @@ t.end() | ||
test('clear()', function (t) { | ||
var dir = keydir() | ||
dir.put('beep') | ||
dir.put('boop') | ||
dir.clear() | ||
t.deepEqual(dir.keys(), []) | ||
t.end() | ||
}) | ||
test('range()', function (t) { | ||
@@ -89,3 +102,3 @@ var dir = keydir() | ||
dir.range() | ||
, [ 'foo1', 'foo2', 'foo3', 'foo4', 'foo5' ] | ||
, transformArray([ 'foo1', 'foo2', 'foo3', 'foo4', 'foo5' ]) | ||
) | ||
@@ -95,3 +108,3 @@ | ||
dir.range({ limit: 3 }) | ||
, [ 'foo1', 'foo2', 'foo3' ] | ||
, transformArray([ 'foo1', 'foo2', 'foo3' ]) | ||
) | ||
@@ -101,3 +114,3 @@ | ||
dir.range({ limit: -1 }) | ||
, [ 'foo1', 'foo2', 'foo3', 'foo4', 'foo5' ] | ||
, transformArray([ 'foo1', 'foo2', 'foo3', 'foo4', 'foo5' ]) | ||
) | ||
@@ -107,3 +120,3 @@ | ||
dir.range({ reverse: true }) | ||
, [ 'foo5', 'foo4', 'foo3', 'foo2', 'foo1' ] | ||
, transformArray([ 'foo5', 'foo4', 'foo3', 'foo2', 'foo1' ]) | ||
) | ||
@@ -113,3 +126,3 @@ | ||
dir.range({ limit: 3, reverse: true }) | ||
, [ 'foo5', 'foo4', 'foo3' ] | ||
, transformArray([ 'foo5', 'foo4', 'foo3' ]) | ||
) | ||
@@ -119,3 +132,3 @@ | ||
dir.range({ gte: 'foo2', limit: 3 }) | ||
, [ 'foo2', 'foo3', 'foo4' ] | ||
, transformArray([ 'foo2', 'foo3', 'foo4' ]) | ||
) | ||
@@ -125,3 +138,3 @@ | ||
dir.range({ gt: 'foo1', limit: 3 }) | ||
, [ 'foo2', 'foo3', 'foo4' ] | ||
, transformArray([ 'foo2', 'foo3', 'foo4' ]) | ||
) | ||
@@ -131,3 +144,3 @@ | ||
dir.range({ lte: 'foo4', limit: 3, reverse: true }) | ||
, [ 'foo4', 'foo3', 'foo2' ] | ||
, transformArray([ 'foo4', 'foo3', 'foo2' ]) | ||
) | ||
@@ -137,3 +150,3 @@ | ||
dir.range({ lt: 'foo5', limit: 3, reverse: true }) | ||
, [ 'foo4', 'foo3', 'foo2' ] | ||
, transformArray([ 'foo4', 'foo3', 'foo2' ]) | ||
) | ||
@@ -143,3 +156,3 @@ | ||
dir.range({ gt: 'foo2', lt: 'foo5', limit: 3}) | ||
, [ 'foo3', 'foo4' ] | ||
, transformArray([ 'foo3', 'foo4' ]) | ||
) | ||
@@ -149,3 +162,3 @@ | ||
dir.range({ gt: 'foo2', lt: 'foo5', limit: 3, reverse: true}) | ||
, [ 'foo4', 'foo3' ] | ||
, transformArray([ 'foo4', 'foo3' ]) | ||
) | ||
@@ -155,3 +168,3 @@ | ||
dir.range({ lt: 'foo1.5' }) | ||
, [ 'foo1' ] | ||
, transformArray([ 'foo1' ]) | ||
) | ||
@@ -161,3 +174,3 @@ | ||
dir.range({ lte: 'foo1.5' }) | ||
, [ 'foo1' ] | ||
, transformArray([ 'foo1' ]) | ||
) | ||
@@ -167,3 +180,3 @@ | ||
dir.range({ gt: 'foo4.5' }) | ||
, [ 'foo5' ] | ||
, transformArray([ 'foo5' ]) | ||
) | ||
@@ -173,6 +186,52 @@ | ||
dir.range({ gte: 'foo4.5' }) | ||
, [ 'foo5' ] | ||
, transformArray([ 'foo5' ]) | ||
) | ||
dir.clear() | ||
dir.put('00') | ||
dir.put('01') | ||
dir.put('02') | ||
dir.put('03') | ||
t.deepEqual( | ||
dir.range({ lt: '0' }) | ||
, [] | ||
) | ||
t.end() | ||
}) | ||
test('Buffers & Strings', function (t) { | ||
var dir = keydir() | ||
dir.put(new Buffer([ 128 ])) | ||
dir.put('beep boop') | ||
t.deepEqual( | ||
dir.range() | ||
, [ new Buffer('beep boop'), new Buffer([ 128 ]) ] | ||
) | ||
t.deepEqual( | ||
dir.range({ gt: new Buffer([ 127 ]) }) | ||
, [ new Buffer( [ 128 ] ) ] | ||
) | ||
t.deepEqual( | ||
dir.range({ gt: new Buffer([ 129 ]) }) | ||
, [ ] | ||
) | ||
dir.del(new Buffer('beep boop')) | ||
t.deepEqual( | ||
dir.range() | ||
, [ new Buffer([ 128 ]) ] | ||
) | ||
dir.put(new Buffer([ 127 ])) | ||
t.deepEqual( | ||
dir.range() | ||
, [ new Buffer([ 127 ]), new Buffer([ 128 ]) ] | ||
) | ||
t.end() | ||
}) |
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
9745
252