abstract-leveldown
Advanced tools
Comparing version 0.3.0 to 0.4.0
/* Copyright (c) 2013 Rod Vagg, MIT License */ | ||
function AbstractIterator (db) { | ||
this.db = db | ||
this._ended = false | ||
this._nexting = false | ||
} | ||
var AbstractIterator = require('./abstract-iterator') | ||
, AbstractChainedBatch = require('./abstract-chained-batch') | ||
AbstractIterator.prototype.next = function (callback) { | ||
if (typeof callback != 'function') | ||
throw new Error('next() requires a callback argument') | ||
if (this._ended) | ||
throw new Error('cannot call next() after end()') | ||
if (this._nexting) | ||
throw new Error('cannot call next() before previous next() has completed') | ||
this._nexting = true | ||
if (typeof this._next == 'function') { | ||
return this._next(function () { | ||
this._nexting = false | ||
callback.apply(null, arguments) | ||
}.bind(this)) | ||
} | ||
process.nextTick(function () { | ||
this._nexting = false | ||
callback() | ||
}.bind(this)) | ||
} | ||
AbstractIterator.prototype.end = function (callback) { | ||
if (typeof callback != 'function') | ||
throw new Error('end() requires a callback argument') | ||
if (this._ended) | ||
throw new Error('end() already called on iterator') | ||
this._ended = true | ||
if (typeof this._end == 'function') | ||
return this._end(callback) | ||
process.nextTick(callback) | ||
} | ||
function AbstractLevelDOWN (location) { | ||
@@ -103,5 +62,5 @@ if (!arguments.length || location === undefined) | ||
throw new Error('put() requires a callback argument') | ||
var err = this._checkKeyValue(value, 'value', this._isBuffer) | ||
var err = this._checkKeyValue(key, 'key', this._isBuffer) | ||
if (err) return callback(err) | ||
err = this._checkKeyValue(key, 'key', this._isBuffer) | ||
err = this._checkKeyValue(value, 'value', this._isBuffer) | ||
if (err) return callback(err) | ||
@@ -138,13 +97,35 @@ if (!this._isBuffer(key)) key = String(key) | ||
AbstractLevelDOWN.prototype.batch = function (array, options, callback) { | ||
if (!arguments.length) | ||
return this._chainedBatch() | ||
if (typeof options == 'function') | ||
callback = options | ||
if (!Array.isArray(array) && typeof array == 'object') { | ||
options = array | ||
array = undefined | ||
} | ||
if (typeof callback != 'function') | ||
throw new Error('batch(array) requires a callback argument') | ||
if (!Array.isArray(array)) | ||
return callback(new Error('batch(array) requires an array argument')) | ||
if (typeof options != 'object') | ||
options = {} | ||
// TODO: if array == undefined && callback == function, derp | ||
var i = 0 | ||
, l = array.length | ||
, e | ||
, err | ||
for (; i < l; i++) { | ||
e = array[i] | ||
if (typeof e != 'object') continue; | ||
err = this._checkKeyValue(e.type, 'type', this._isBuffer) | ||
if (err) return callback(err) | ||
err = this._checkKeyValue(e.key, 'key', this._isBuffer) | ||
if (err) return callback(err) | ||
if (e.type == 'put') { | ||
err = this._checkKeyValue(e.value, 'value', this._isBuffer) | ||
if (err) return callback(err) | ||
} | ||
} | ||
if (typeof this._batch == 'function') | ||
@@ -180,2 +161,6 @@ return this._batch(array, options, callback) | ||
AbstractLevelDOWN.prototype._chainedBatch = function () { | ||
return new AbstractChainedBatch(this) | ||
} | ||
AbstractLevelDOWN.prototype._isBuffer = function (obj) { | ||
@@ -182,0 +167,0 @@ return Buffer.isBuffer(obj) |
{ | ||
"name" : "abstract-leveldown" | ||
, "description" : "An abstract prototype matching the LevelDOWN API" | ||
, "version" : "0.3.0" | ||
, "version" : "0.4.0" | ||
, "homepage" : "https://github.com/rvagg/node-abstract-leveldown" | ||
@@ -6,0 +6,0 @@ , "contributors" : [ |
91
test.js
@@ -1,11 +0,12 @@ | ||
var tap = require('tap') | ||
, sinon = require('sinon') | ||
, util = require('util') | ||
, testCommon = require('./testCommon') | ||
, AbstractLevelDOWN = require('./').AbstractLevelDOWN | ||
, AbstractIterator = require('./').AbstractIterator | ||
, factory = function (location) { | ||
return new AbstractLevelDOWN(location) | ||
} | ||
const tap = require('tap') | ||
, sinon = require('sinon') | ||
, util = require('util') | ||
, testCommon = require('./testCommon') | ||
, AbstractLevelDOWN = require('./').AbstractLevelDOWN | ||
, AbstractIterator = require('./').AbstractIterator | ||
function factory (location) { | ||
return new AbstractLevelDOWN(location) | ||
} | ||
/*** compatibility with basic LevelDOWN API ***/ | ||
@@ -36,2 +37,8 @@ | ||
require('./abstract/batch-test').setUp(factory, tap.test, testCommon) | ||
require('./abstract/batch-test').args(tap.test) | ||
require('./abstract/chained-batch-test').setUp(factory, tap.test, testCommon) | ||
require('./abstract/chained-batch-test').args(tap.test) | ||
require('./abstract/close-test').close(factory, tap.test, testCommon) | ||
@@ -268,3 +275,3 @@ | ||
test.batch() | ||
test.batch(expectedArray, expectedCb) | ||
@@ -274,7 +281,7 @@ t.equal(spy.callCount, 1, 'got _batch() call') | ||
t.equal(spy.getCall(0).args.length, 3, 'got three arguments') | ||
t.equal(spy.getCall(0).args[0], undefined, 'got expected array argument') | ||
t.deepEqual(spy.getCall(0).args[1], {}, 'got blank options argument') | ||
t.equal(spy.getCall(0).args[2], undefined, 'got expected callback argument') | ||
t.equal(spy.getCall(0).args[0], expectedArray, 'got expected array argument') | ||
t.deepEqual(spy.getCall(0).args[1], {}, 'got expected options argument') | ||
t.equal(spy.getCall(0).args[2], expectedCb, 'got expected callback argument') | ||
test.batch(expectedOptions) | ||
test.batch(expectedArray, expectedOptions, expectedCb) | ||
@@ -284,24 +291,48 @@ t.equal(spy.callCount, 2, 'got _batch() call') | ||
t.equal(spy.getCall(1).args.length, 3, 'got three arguments') | ||
t.equal(spy.getCall(1).args[0], undefined, 'got expected array argument') | ||
t.equal(spy.getCall(1).args[0], expectedArray, 'got expected array argument') | ||
t.deepEqual(spy.getCall(1).args[1], expectedOptions, 'got expected options argument') | ||
t.equal(spy.getCall(1).args[2], undefined, 'got expected callback argument') | ||
t.equal(spy.getCall(1).args[2], expectedCb, 'got expected callback argument') | ||
test.batch(expectedArray, expectedCb) | ||
t.end() | ||
}) | ||
t.equal(spy.callCount, 3, 'got _batch() call') | ||
t.equal(spy.getCall(2).thisValue, test, '`this` on _batch() was correct') | ||
t.equal(spy.getCall(2).args.length, 3, 'got three arguments') | ||
t.equal(spy.getCall(2).args[0], expectedArray, 'got expected array argument') | ||
t.deepEqual(spy.getCall(2).args[1], {}, 'got expected options argument') | ||
t.equal(spy.getCall(2).args[2], expectedCb, 'got expected callback argument') | ||
tap.test('test chained batch() extensibility', function (t) { | ||
var spy = sinon.spy() | ||
, expectedCb = function () {} | ||
, expectedOptions = { options: 1 } | ||
, expectedArray = [ 1, 2 ] | ||
, test | ||
test.batch(expectedArray, expectedOptions, expectedCb) | ||
function Test (location) { | ||
AbstractLevelDOWN.call(this, location) | ||
} | ||
t.equal(spy.callCount, 4, 'got _batch() call') | ||
t.equal(spy.getCall(3).thisValue, test, '`this` on _batch() was correct') | ||
t.equal(spy.getCall(3).args.length, 3, 'got three arguments') | ||
t.equal(spy.getCall(3).args[0], expectedArray, 'got expected array argument') | ||
t.deepEqual(spy.getCall(3).args[1], expectedOptions, 'got expected options argument') | ||
t.equal(spy.getCall(3).args[2], expectedCb, 'got expected callback argument') | ||
util.inherits(Test, AbstractLevelDOWN) | ||
Test.prototype._batch = spy | ||
test = new Test('foobar') | ||
test.batch().put('foo', 'bar').del('bang').write(expectedCb) | ||
t.equal(spy.callCount, 1, 'got _batch() call') | ||
t.equal(spy.getCall(0).thisValue, test, '`this` on _batch() was correct') | ||
t.equal(spy.getCall(0).args.length, 3, 'got three arguments') | ||
t.equal(spy.getCall(0).args[0].length, 2, 'got expected array argument') | ||
t.deepEqual(spy.getCall(0).args[0][0], { type: 'put', key: 'foo', value: 'bar' }, 'got expected array argument[0]') | ||
t.deepEqual(spy.getCall(0).args[0][1], { type: 'del', key: 'bang' }, 'got expected array argument[1]') | ||
t.deepEqual(spy.getCall(0).args[1], {}, 'got expected options argument') | ||
t.equal(spy.getCall(0).args[2], expectedCb, 'got expected callback argument') | ||
test.batch().put('foo', 'bar').del('bang').write(expectedOptions, expectedCb) | ||
t.equal(spy.callCount, 2, 'got _batch() call') | ||
t.equal(spy.getCall(1).thisValue, test, '`this` on _batch() was correct') | ||
t.equal(spy.getCall(1).args.length, 3, 'got three arguments') | ||
t.equal(spy.getCall(1).args[0].length, 2, 'got expected array argument') | ||
t.deepEqual(spy.getCall(1).args[0][0], { type: 'put', key: 'foo', value: 'bar' }, 'got expected array argument[0]') | ||
t.deepEqual(spy.getCall(1).args[0][1], { type: 'del', key: 'bang' }, 'got expected array argument[1]') | ||
t.deepEqual(spy.getCall(1).args[1], expectedOptions, 'got expected options argument') | ||
t.equal(spy.getCall(1).args[2], expectedCb, 'got expected callback argument') | ||
t.end() | ||
@@ -308,0 +339,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
82256
23
1794