Skip to content

Commit

Permalink
implement _seek instead of seek
Browse files Browse the repository at this point in the history
  • Loading branch information
vweevers committed Sep 22, 2018
1 parent 3b8b845 commit 9be66ac
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 24 deletions.
13 changes: 2 additions & 11 deletions iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,9 @@ function Iterator (db, options) {

util.inherits(Iterator, AbstractIterator)

Iterator.prototype.seek = function (target) {
if (this._ended) {
throw new Error('cannot call seek() after end()')
}
if (this._nexting) {
throw new Error('cannot call seek() before next() has completed')
}
if (typeof target !== 'string' && !Buffer.isBuffer(target)) {
throw new Error('seek() requires a string or buffer key')
}
Iterator.prototype._seek = function (target) {
if (target.length === 0) {
throw new Error('cannot seek() to an empty key')
throw new Error('cannot seek() to an empty target')
}

this.cache = null
Expand Down
18 changes: 5 additions & 13 deletions test/iterator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,13 @@ const iota = require('iota-array')
const lexi = require('lexicographic-integer')
const util = require('util')

make('iterator throws if key is not a string or buffer', function (db, t, done) {
var keys = [null, undefined, 1, true, false]
var pending = keys.length
make('iterator#seek throws if target is empty', function (db, t, done) {
var targets = [null, undefined, '', Buffer.alloc(0), []]
var pending = targets.length

keys.forEach(function (key) {
var error
targets.forEach(function (target) {
var ite = db.iterator()

try {
ite.seek(key)
} catch (e) {
error = e
}

t.ok(error, 'had error from seek()')
t.throws(ite.seek.bind(ite, target), /Error: cannot seek\(\) to an empty target/)
ite.end(end)
})

Expand Down

0 comments on commit 9be66ac

Please sign in to comment.