diff --git a/index.js b/index.js index 1573e22..0915bb7 100644 --- a/index.js +++ b/index.js @@ -77,6 +77,11 @@ DB.prototype._iterator = function (opts) { return new Iterator(this, opts) } +DB.prototype._clear = function (opts, callback) { + opts = this.codec.encodeLtgt(opts) + this.db.clear(opts, callback) +} + DB.prototype.approximateSize = function (start, end, opts, cb) { start = this.codec.encodeKey(start, opts) end = this.codec.encodeKey(end, opts) diff --git a/package.json b/package.json index 697ee63..1684dbd 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "prepublishOnly": "npm run dependency-check" }, "dependencies": { - "abstract-leveldown": "^6.0.0", + "abstract-leveldown": "^6.1.1", "inherits": "^2.0.3", "level-codec": "^9.0.0", "level-errors": "^2.0.0" diff --git a/test/index.js b/test/index.js index edbb63c..3d815f1 100644 --- a/test/index.js +++ b/test/index.js @@ -401,6 +401,31 @@ test('iterator skips values if options.values is false', function (t) { }) }) +test('iterator encodes range options', function (t) { + t.plan(7) + + var keyEncoding = { + encode: function (key) { + return 'encoded_' + key + }, + buffer: false + } + + var db = encdown({ + iterator: function (options) { + t.is(options.start, 'encoded_1') + t.is(options.end, 'encoded_2') + t.is(options.gt, 'encoded_3') + t.is(options.gte, 'encoded_4') + t.is(options.lt, 'encoded_5') + t.is(options.lte, 'encoded_6') + t.is(options.foo, 7) + } + }, { keyEncoding }) + + db.iterator({ start: 1, end: 2, gt: 3, gte: 4, lt: 5, lte: 6, foo: 7 }) +}) + test('iterator does not strip nullish range options', function (t) { t.plan(12) @@ -624,3 +649,104 @@ test('encodes nullish seek target', function (t) { t.same(targets, ['null', 'undefined'], 'encoded') }) + +test('clear() forwards default options', function (t) { + t.plan(3) + + var down = { + clear: function (options, callback) { + t.is(options.reverse, false) + t.is(options.limit, -1) + t.is(callback, noop) + } + } + + encdown(down).clear(noop) +}) + +test('clear() forwards error from underlying store', function (t) { + t.plan(1) + + var down = { + clear: function (options, cb) { + process.nextTick(cb, new Error('error from store')) + } + } + + encdown(down).clear(function (err) { + t.is(err.message, 'error from store') + }) +}) + +test('clear() encodes range options', function (t) { + t.plan(5) + + var keyEncoding = { + encode: function (key) { + return 'encoded_' + key + }, + buffer: false + } + + var db = encdown({ + clear: function (options) { + t.is(options.gt, 'encoded_1') + t.is(options.gte, 'encoded_2') + t.is(options.lt, 'encoded_3') + t.is(options.lte, 'encoded_4') + t.is(options.foo, 5) + } + }, { keyEncoding }) + + db.clear({ gt: 1, gte: 2, lt: 3, lte: 4, foo: 5 }, noop) +}) + +test('clear() does not strip nullish range options', function (t) { + t.plan(12) + + encdown({ + clear: function (options) { + t.is(options.gt, null) + t.is(options.gte, null) + t.is(options.lt, null) + t.is(options.lte, null) + } + }).clear({ + gt: null, + gte: null, + lt: null, + lte: null + }, noop) + + encdown({ + clear: function (options) { + t.ok(hasOwnProperty.call(options, 'gt')) + t.ok(hasOwnProperty.call(options, 'gte')) + t.ok(hasOwnProperty.call(options, 'lt')) + t.ok(hasOwnProperty.call(options, 'lte')) + + t.is(options.gt, undefined) + t.is(options.gte, undefined) + t.is(options.lt, undefined) + t.is(options.lte, undefined) + } + }).clear({ + gt: undefined, + gte: undefined, + lt: undefined, + lte: undefined + }, noop) +}) + +test('clear() does not add nullish range options', function (t) { + t.plan(4) + + encdown({ + clear: function (options) { + t.notOk(hasOwnProperty.call(options, 'gt')) + t.notOk(hasOwnProperty.call(options, 'gte')) + t.notOk(hasOwnProperty.call(options, 'lt')) + t.notOk(hasOwnProperty.call(options, 'lte')) + } + }).clear({}, noop) +})