Skip to content

Commit

Permalink
Simplify (see Level/supports#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
vweevers committed Oct 6, 2019
1 parent 2263d2f commit b36dafe
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 56 deletions.
44 changes: 11 additions & 33 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,27 @@ var AbstractIterator = require('abstract-leveldown').AbstractIterator
var inherits = require('inherits')
var Codec = require('level-codec')
var EncodingError = require('level-errors').EncodingError
var specialMethods = ['approximateSize', 'compactRange']
var rangeMethods = ['approximateSize', 'compactRange']

module.exports = DB.default = DB

function DB (db, opts) {
if (!(this instanceof DB)) return new DB(db, opts)
if (db.supports && db.supports.encodings) throw new Error('Double encoding')

AbstractLevelDOWN.call(this, db.supports)
this.supports.encodings = true

// TODO (future major): remove this fallback
specialMethods.forEach(function (m) {
rangeMethods.forEach(function (m) {
// TODO (future major): remove this fallback
if (typeof db[m] === 'function' && !this.supports.additionalMethods[m]) {
this.supports.additionalMethods[m] = {
args: [{ type: 'key' }, { type: 'key' }, { type: 'options' }]
this.supports.additionalMethods[m] = true
}

if (this.supports.additionalMethods[m]) {
this[m] = function (start, end, opts, cb) {
start = this.codec.encodeKey(start, opts)
end = this.codec.encodeKey(end, opts)
return this.db[m](start, end, opts, cb)
}
}
}, this)
Expand All @@ -32,29 +37,6 @@ function DB (db, opts) {

this.db = db
this.codec = new Codec(opts)

Object.keys(this.supports.additionalMethods).forEach(function (m) {
if (this[m] != null) return

var signature = this.supports.additionalMethods[m]
var args = (signature && signature.args) || []

this[m] = function () {
var i = Math.min(arguments.length, args.length)
var opts

while (i--) {
if (args[i].type === 'options' && !opts && isOptions(arguments[i])) {
opts = arguments[i]
arguments[i] = this.codec.encodeLtgt(opts)
} else if (args[i].type === 'key') {
arguments[i] = this.codec.encodeKey(arguments[i], opts)
}
}

return this.db[m].apply(this.db, arguments)
}
}, this)
}

inherits(DB, AbstractLevelDOWN)
Expand Down Expand Up @@ -189,7 +171,3 @@ Batch.prototype._clear = function () {
Batch.prototype._write = function (opts, cb) {
this.batch.write(opts, cb)
}

function isOptions (o) {
return typeof o === 'object' && o !== null
}
67 changes: 44 additions & 23 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -579,47 +579,68 @@ test('iterator catches decoding error from valueEncoding', function (t) {
})
})

test('encodes start and end of approximateSize()', function (t) {
t.plan(2)
test('proxies approximateSize() if it exists', function (t) {
t.is(typeof encdown({ approximateSize: noop }).approximateSize, 'function')
t.ok(encdown({ approximateSize: noop }).supports.additionalMethods.approximateSize)
t.is(encdown({}).approximateSize, undefined)
t.notOk(encdown({}).supports.additionalMethods.approximateSize)
t.end()
})

var down = {
test('proxies compactRange() if it exists', function (t) {
t.is(typeof encdown({ compactRange: noop }).compactRange, 'function')
t.ok(encdown({ compactRange: noop }).supports.additionalMethods.compactRange)
t.is(encdown({}).compactRange, undefined)
t.notOk(encdown({}).supports.additionalMethods.compactRange)
t.end()
})

test('encodes start and end of approximateSize()', function (t) {
var db = encdown({
approximateSize: function (start, end) {
t.is(start, '1')
t.is(end, '2')
t.end()
}
}
})

encdown(down).approximateSize(1, 2, noop)
db.approximateSize(1, 2, noop)
})

test('encodes start and end of approximateSize() with custom encoding', function (t) {
t.plan(2)
test('encodes start and end of compactRange()', function (t) {
var db = encdown({
compactRange: function (start, end) {
t.is(start, '1')
t.is(end, '2')
t.end()
}
})

var down = {
db.compactRange(1, 2, noop)
})

test('encodes start and end of approximateSize() with custom encoding', function (t) {
var db = encdown({
approximateSize: function (start, end) {
t.is(start, '"a"')
t.is(end, '"b"')
t.end()
}
}
})

encdown(down).approximateSize('a', 'b', { keyEncoding: 'json' }, noop)
db.approximateSize('a', 'b', { keyEncoding: 'json' }, noop)
})

test('encodes options of additionalMethod', function (t) {
t.plan(1)

var down = {
supports: {
additionalMethods: {
foo: { args: [{ type: 'options' }] }
}
},
foo: function (options) {
t.is(options.gt, '"a"')
test('encodes start and end of compactRange() with custom encoding', function (t) {
var db = encdown({
compactRange: function (start, end) {
t.is(start, '"a"')
t.is(end, '"b"')
t.end()
}
}
})

encdown(down).foo({ gt: 'a', keyEncoding: 'json' })
db.compactRange('a', 'b', { keyEncoding: 'json' }, noop)
})

test('encodes seek target', function (t) {
Expand Down

0 comments on commit b36dafe

Please sign in to comment.