Comparing version 1.0.0 to 1.0.1
@@ -44,12 +44,37 @@ var ltgt = require('ltgt') | ||
Keydir.prototype.range = function (opts) { | ||
opts = opts || {} | ||
var keys = this._keys.filter(ltgt.filter(opts)) | ||
Keydir.prototype.range = function (options) { | ||
options = options || {} | ||
if (opts.reverse) | ||
keys.reverse() | ||
var lowerBound = ltgt.lowerBound(options) | ||
, fromIdx = lowerBound ? | ||
this._sortedIndexOf(lowerBound) : 0 | ||
, upperBound = ltgt.upperBound(options) | ||
// toIdx - the id to slice to (exclusive) | ||
, toIdx = upperBound ? | ||
this._sortedIndexOf(upperBound) + 1 : this._keys.length | ||
, keys | ||
if (opts.limit && opts.limit !== -1) | ||
keys = keys.slice(0, opts.limit) | ||
if (ltgt.lowerBoundExclusive(options) && this._keys[fromIdx] === lowerBound) | ||
fromIdx++ | ||
// behave correcly when the upperBound is between two keys | ||
if (upperBound && this._keys[toIdx - 1] > upperBound) | ||
toIdx-- | ||
if (ltgt.upperBoundExclusive(options) && this._keys[toIdx - 1] === upperBound) | ||
toIdx-- | ||
if (options.limit && options.limit !== -1) { | ||
if (options.reverse) { | ||
fromIdx = Math.max(toIdx - options.limit, fromIdx) | ||
} else { | ||
toIdx = Math.min(fromIdx + options.limit, toIdx) | ||
} | ||
} | ||
keys = this._keys.slice(fromIdx, toIdx) | ||
if (options.reverse) | ||
keys = keys.reverse() | ||
return keys | ||
@@ -56,0 +81,0 @@ } |
{ | ||
"name": "keydir", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "A sorted list of keys with support for level-* ranges", | ||
@@ -5,0 +5,0 @@ "main": "keydir.js", |
45
test.js
@@ -103,3 +103,48 @@ var test = require('tape') | ||
t.deepEqual( | ||
dir.range({ gt: 'foo1', limit: 3 }) | ||
, [ 'foo2', 'foo3', 'foo4' ] | ||
) | ||
t.deepEqual( | ||
dir.range({ lte: 'foo4', limit: 3, reverse: true }) | ||
, [ 'foo4', 'foo3', 'foo2' ] | ||
) | ||
t.deepEqual( | ||
dir.range({ lt: 'foo5', limit: 3, reverse: true }) | ||
, [ 'foo4', 'foo3', 'foo2' ] | ||
) | ||
t.deepEqual( | ||
dir.range({ gt: 'foo2', lt: 'foo5', limit: 3}) | ||
, [ 'foo3', 'foo4' ] | ||
) | ||
t.deepEqual( | ||
dir.range({ gt: 'foo2', lt: 'foo5', limit: 3, reverse: true}) | ||
, [ 'foo4', 'foo3' ] | ||
) | ||
t.deepEqual( | ||
dir.range({ lt: 'foo1.5' }) | ||
, [ 'foo1' ] | ||
) | ||
t.deepEqual( | ||
dir.range({ lte: 'foo1.5' }) | ||
, [ 'foo1' ] | ||
) | ||
t.deepEqual( | ||
dir.range({ gt: 'foo4.5' }) | ||
, [ 'foo5' ] | ||
) | ||
t.deepEqual( | ||
dir.range({ gte: 'foo4.5' }) | ||
, [ 'foo5' ] | ||
) | ||
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
7547
181