subleveldown
Advanced tools
Comparing version 1.0.1 to 1.0.2
var sub = require('./') | ||
var memdb = require('memdb') | ||
var levelup = require('levelup') | ||
var memdown = require('memdown') | ||
var db = memdb() | ||
var db = levelup('test', {db:memdown}) | ||
@@ -6,0 +7,0 @@ var test = sub(db, 'test', {valueEncoding:'utf-8'}) |
{ | ||
"name": "subleveldown", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "sublevels implemented using leveldowns", | ||
@@ -12,3 +12,3 @@ "main": "index.js", | ||
"devDependencies": { | ||
"memdb": "^0.2.0" | ||
"memdown": "^1.0.0" | ||
}, | ||
@@ -29,4 +29,4 @@ "repository": { | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "node test" | ||
} | ||
} |
@@ -9,2 +9,4 @@ # subleveldown | ||
[![build status](http://img.shields.io/travis/mafintosh/subleveldown.svg?style=flat)](http://travis-ci.org/mafintosh/subleveldown) | ||
## Usage | ||
@@ -30,4 +32,11 @@ | ||
## API | ||
#### `subdb = sub(db, [prefix], [options])` | ||
Returns a levelup instance that uses the subleveldown with `prefix`. | ||
The `options` argument is passed to the [levelup](https://github.com/rvagg/node-levelup) constructor | ||
## License | ||
MIT |
@@ -5,2 +5,8 @@ var util = require('util') | ||
var concat = function(prefix, key) { | ||
if (typeof key === 'string' && key.length) return prefix+key | ||
if (Buffer.isBuffer(key) && key.length) return Buffer.concat([new Buffer(prefix), key]) | ||
return key | ||
} | ||
var SubIterator = function(ite, prefix) { | ||
@@ -40,6 +46,6 @@ this.iterator = ite | ||
gt: function(x) { | ||
return self.prefix+(x || '') | ||
return concat(self.prefix, (x || '')) | ||
}, | ||
lt: function(x) { | ||
return self.prefix+(x || '\xff') | ||
return concat(self.prefix, (x || '\xff')) | ||
} | ||
@@ -78,15 +84,17 @@ } | ||
SubDown.prototype.put = function(key, value, opts, cb) { | ||
this.leveldown.put(this.prefix+key, value, opts, cb) | ||
this.leveldown.put(concat(this.prefix, key), value, opts, cb) | ||
} | ||
SubDown.prototype.get = function(key, opts, cb) { | ||
this.leveldown.get(this.prefix+key, opts, cb) | ||
this.leveldown.get(concat(this.prefix, key), opts, cb) | ||
} | ||
SubDown.prototype.del = function(key, opts, cb) { | ||
this.leveldown.del(this.prefix+key, opts, cb) | ||
this.leveldown.del(concat(this.prefix, key), opts, cb) | ||
} | ||
SubDown.prototype.batch = SubDown.prototype._batch = function(operations, opts, cb) { | ||
SubDown.prototype.batch = | ||
SubDown.prototype._batch = function(operations, opts, cb) { | ||
if (arguments.length === 0) return new abstract.AbstractChainedBatch(this) | ||
if (!Array.isArray(operations)) return this.leveldown.batch.apply(null, arguments) | ||
@@ -96,6 +104,6 @@ var subops = new Array(operations.length) | ||
var o = operations[i] | ||
subops[i] = {type:o.type, key:this.prefix+o.key, value:o.value} | ||
subops[i] = {type:o.type, key:concat(this.prefix, o.key), value:o.value} | ||
} | ||
this.leveldown.batch(operations, opts, cb) | ||
this.leveldown.batch(subops, opts, cb) | ||
} | ||
@@ -131,7 +139,13 @@ | ||
xopts.valueAsBuffer = opts.valueAsBuffer | ||
return opts | ||
xopts.reverse = opts.reverse | ||
return xopts | ||
} | ||
var fixRange = function(opts) { | ||
return (!opts.reverse || (!opts.end && !opts.start)) ? opts : {start:opts.end, end:opts.start} | ||
} | ||
SubDown.prototype.iterator = function(opts) { | ||
var xopts = extend(wrap(opts, this._wrap), opts) | ||
if (!opts) opts = {} | ||
var xopts = extend(wrap(fixRange(opts), this._wrap), opts) | ||
return new SubIterator(this.leveldown.iterator(xopts), this.prefix) | ||
@@ -138,0 +152,0 @@ } |
var test = require('tape') | ||
var memdb = require('memdb') | ||
var memdown = require('memdown') | ||
var subdown = require('../subdown') | ||
var levelup = require('levelup') | ||
var testCommon = require('./common') | ||
@@ -8,3 +9,3 @@ var testBuffer = new Buffer('this-is-test-data') | ||
var down = function(loc) { | ||
return subdown(memdb(loc), 'test') | ||
return subdown(levelup(loc, {db:memdown}), 'test') | ||
} | ||
@@ -11,0 +12,0 @@ |
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
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
230
0
41
10954
10