Skip to content

Commit

Permalink
Support constructing without location (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
vweevers authored Oct 13, 2019
1 parent db6dd96 commit 8f48baa
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 9 deletions.
13 changes: 10 additions & 3 deletions level-packager.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@ var encode = require('encoding-down')

function packager (leveldown) {
function Level (location, options, callback) {
if (typeof options === 'function') {
if (typeof location === 'function') {
callback = location
} else if (typeof options === 'function') {
callback = options
}
if (typeof options !== 'object' || options === null) {
options = {}

if (!isObject(options)) {
options = isObject(location) ? location : {}
}

return levelup(encode(leveldown(location), options), options, callback)
}

function isObject (o) {
return typeof o === 'object' && o !== null
}

['destroy', 'repair'].forEach(function (m) {
if (typeof leveldown[m] === 'function') {
Level[m] = function () {
Expand Down
121 changes: 115 additions & 6 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,40 @@ test('Level constructor relays .destroy and .repair if they exist', function (t)
}
})

test('Level constructor with default options', function (t) {
test('Level constructor', function (t) {
t.plan(3)
function Down () {
return {
open: function (opts, cb) {
t.same(opts, {
createIfMissing: true,
errorIfExists: false,

// This is a side effect of encoding-down (mutating options)
keyEncoding: 'utf8',
valueEncoding: 'utf8'
})
}
}
}
var levelup = packager(Down)()
t.is(levelup.options.keyEncoding, 'utf8')
t.is(levelup.options.valueEncoding, 'utf8')
})

test('Level constructor with location', function (t) {
t.plan(4)
function Down (location) {
t.is(location, 'location', 'location is correct')
return {
open: function (opts, cb) {}
open: function (opts, cb) {
t.same(opts, {
createIfMissing: true,
errorIfExists: false,
keyEncoding: 'utf8',
valueEncoding: 'utf8'
})
}
}
}
var levelup = packager(Down)('location')
Expand All @@ -46,12 +74,38 @@ test('Level constructor with default options', function (t) {
})

test('Level constructor with callback', function (t) {
t.plan(3)
function Down () {
return {
open: function (opts, cb) {
t.same(opts, {
createIfMissing: true,
errorIfExists: false,
keyEncoding: 'utf8',
valueEncoding: 'utf8'
})
process.nextTick(cb)
}
}
}
packager(Down)(function (err, db) {
t.error(err)
t.ok(db, 'db set in callback')
})
})

test('Level constructor with location & callback', function (t) {
t.plan(4)
function Down (location) {
t.is(location, 'location', 'location is correct')
return {
open: function (opts, cb) {
t.pass('open called')
t.same(opts, {
createIfMissing: true,
errorIfExists: false,
keyEncoding: 'utf8',
valueEncoding: 'utf8'
})
process.nextTick(cb)
}
}
Expand All @@ -62,12 +116,19 @@ test('Level constructor with callback', function (t) {
})
})

test('Level constructor with custom options', function (t) {
t.plan(3)
test('Level constructor with location & options', function (t) {
t.plan(4)
var Down = function (location) {
t.is(location, 'location', 'location is correct')
return {
open: function (opts, cb) {}
open: function (opts, cb) {
t.same(opts, {
createIfMissing: true,
errorIfExists: false,
keyEncoding: 'binary',
valueEncoding: 'binary'
})
}
}
}
var levelup = packager(Down)('location', {
Expand All @@ -77,3 +138,51 @@ test('Level constructor with custom options', function (t) {
t.is(levelup.options.keyEncoding, 'binary')
t.is(levelup.options.valueEncoding, 'binary')
})

test('Level constructor with options', function (t) {
t.plan(3)
var Down = function () {
return {
open: function (opts, cb) {
t.same(opts, {
createIfMissing: true,
errorIfExists: false,
keyEncoding: 'binary',
valueEncoding: 'binary'
})
}
}
}
var levelup = packager(Down)({
keyEncoding: 'binary',
valueEncoding: 'binary'
})
t.is(levelup.options.keyEncoding, 'binary')
t.is(levelup.options.valueEncoding, 'binary')
})

test('Level constructor with options & callback', function (t) {
t.plan(5)
var Down = function () {
return {
open: function (opts, cb) {
t.same(opts, {
createIfMissing: true,
errorIfExists: false,
keyEncoding: 'binary',
valueEncoding: 'binary'
})
process.nextTick(cb)
}
}
}
var levelup = packager(Down)({
keyEncoding: 'binary',
valueEncoding: 'binary'
}, function (err, db) {
t.error(err)
t.ok(db, 'db set in callback')
})
t.is(levelup.options.keyEncoding, 'binary')
t.is(levelup.options.valueEncoding, 'binary')
})

0 comments on commit 8f48baa

Please sign in to comment.