Skip to content

Commit

Permalink
Breaking: modernize syntax and bump standard (Level/community#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
vweevers committed Apr 10, 2021
1 parent 0f9b2e6 commit c1f98c3
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 64 deletions.
1 change: 0 additions & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ updates:
- dependency-name: dependency-check
- dependency-name: airtap
- dependency-name: nyc
- dependency-name: standard
45 changes: 23 additions & 22 deletions memdown.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
var inherits = require('inherits')
var AbstractLevelDOWN = require('abstract-leveldown').AbstractLevelDOWN
var AbstractIterator = require('abstract-leveldown').AbstractIterator
var ltgt = require('ltgt')
var createRBT = require('functional-red-black-tree')
var Buffer = require('safe-buffer').Buffer
'use strict'

const inherits = require('inherits')
const AbstractLevelDOWN = require('abstract-leveldown').AbstractLevelDOWN
const AbstractIterator = require('abstract-leveldown').AbstractIterator
const ltgt = require('ltgt')
const createRBT = require('functional-red-black-tree')
const Buffer = require('safe-buffer').Buffer

// In Node, use global.setImmediate. In the browser, use a consistent
// microtask library to give consistent microtask experience to all browsers
var setImmediate = require('./immediate')
var NONE = {}
const setImmediate = require('./immediate')
const NONE = Symbol('none')

// TODO (perf): replace ltgt.compare with a simpler, buffer-only comparator
function gt (value) {
Expand All @@ -33,7 +35,7 @@ function MemIterator (db, options) {

if (this._limit === -1) this._limit = Infinity

var tree = db._store
const tree = db._store

this.keyAsBuffer = options.keyAsBuffer !== false
this.valueAsBuffer = options.valueAsBuffer !== false
Expand Down Expand Up @@ -87,8 +89,8 @@ function MemIterator (db, options) {
inherits(MemIterator, AbstractIterator)

MemIterator.prototype._next = function (callback) {
var key
var value
let key
let value

if (this._done++ >= this._limit) return setImmediate(callback)
if (!this._tree.valid) return setImmediate(callback)
Expand Down Expand Up @@ -169,9 +171,8 @@ function MemDOWN () {
inherits(MemDOWN, AbstractLevelDOWN)

MemDOWN.prototype._open = function (options, callback) {
var self = this
setImmediate(function callNext () {
callback(null, self)
setImmediate(() => {
callback(null, this)
})
}

Expand All @@ -184,7 +185,7 @@ MemDOWN.prototype._serializeValue = function (value) {
}

MemDOWN.prototype._put = function (key, value, options, callback) {
var iter = this._store.find(key)
const iter = this._store.find(key)

if (iter.valid) {
this._store = iter.update(value)
Expand All @@ -196,7 +197,7 @@ MemDOWN.prototype._put = function (key, value, options, callback) {
}

MemDOWN.prototype._get = function (key, options, callback) {
var value = this._store.get(key)
let value = this._store.get(key)

if (typeof value === 'undefined') {
// 'NotFound' error, consistent with LevelDOWN API
Expand All @@ -220,12 +221,12 @@ MemDOWN.prototype._del = function (key, options, callback) {
}

MemDOWN.prototype._batch = function (array, options, callback) {
var i = -1
var key
var value
var iter
var len = array.length
var tree = this._store
let i = -1
let key
let value
let iter
const len = array.length
let tree = this._store

while (++i < len) {
key = array[i].key
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"level-community": "^3.0.0",
"level-concat-iterator": "^2.0.0",
"nyc": "^14.0.0",
"standard": "^14.0.0",
"standard": "^16.0.3",
"tape": "^5.0.1"
},
"hallmark": {
Expand Down
82 changes: 42 additions & 40 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
var test = require('tape')
var suite = require('abstract-leveldown/test')
var concat = require('level-concat-iterator')
var memdown = require('.').default
var ltgt = require('ltgt')
var Buffer = require('safe-buffer').Buffer
var noop = function () { }

var testCommon = suite.common({
'use strict'

const test = require('tape')
const suite = require('abstract-leveldown/test')
const concat = require('level-concat-iterator')
const memdown = require('.').default
const ltgt = require('ltgt')
const Buffer = require('safe-buffer').Buffer
const noop = function () { }

const testCommon = suite.common({
test: test,
factory: function () {
return memdown()
Expand All @@ -25,7 +27,7 @@ suite(testCommon)

// Additional tests for this implementation
test('unsorted entry, sorted iterator', function (t) {
var db = testCommon.factory()
const db = testCommon.factory()

db.open(function (err) {
t.ifError(err, 'no open error')
Expand All @@ -50,7 +52,7 @@ test('unsorted entry, sorted iterator', function (t) {
t.notOk(err, 'no error')
t.equal(data.length, 7, 'correct number of entries')

var expected = [
const expected = [
{ key: 'a', value: 'A' },
{ key: 'b', value: 'B' },
{ key: 'c', value: 'C' },
Expand All @@ -68,7 +70,7 @@ test('unsorted entry, sorted iterator', function (t) {
})

test('reading while putting', function (t) {
var db = testCommon.factory()
const db = testCommon.factory()

db.open(function (err) {
t.ifError(err, 'no open error')
Expand All @@ -77,7 +79,7 @@ test('reading while putting', function (t) {
db.put('c', 'C', noop)
db.put('e', 'E', noop)

var iterator = db.iterator({ keyAsBuffer: false, valueAsBuffer: false })
const iterator = db.iterator({ keyAsBuffer: false, valueAsBuffer: false })

iterator.next(function (err, key, value) {
t.ifError(err, 'no next error')
Expand All @@ -97,7 +99,7 @@ test('reading while putting', function (t) {
})

test('reading while deleting', function (t) {
var db = testCommon.factory()
const db = testCommon.factory()

db.open(function (err) {
t.ifError(err, 'no open error')
Expand All @@ -107,7 +109,7 @@ test('reading while deleting', function (t) {
db.put('c', 'C', noop)
db.put('e', 'E', noop)

var iterator = db.iterator({ keyAsBuffer: false, valueAsBuffer: false })
const iterator = db.iterator({ keyAsBuffer: false, valueAsBuffer: false })

iterator.next(function (err, key, value) {
t.ifError(err, 'no next error')
Expand All @@ -127,15 +129,15 @@ test('reading while deleting', function (t) {
})

test('reverse ranges', function (t) {
var db = testCommon.factory()
const db = testCommon.factory()

db.open(function (err) {
t.ifError(err, 'no open error')

db.put('a', 'A', noop)
db.put('c', 'C', noop)

var iterator = db.iterator({
const iterator = db.iterator({
keyAsBuffer: false,
valueAsBuffer: false,
lte: 'b',
Expand All @@ -152,7 +154,7 @@ test('reverse ranges', function (t) {
})

test('delete while iterating', function (t) {
var db = testCommon.factory()
const db = testCommon.factory()

db.open(function (err) {
t.ifError(err, 'no open error')
Expand All @@ -161,7 +163,7 @@ test('delete while iterating', function (t) {
db.put('b', 'B', noop)
db.put('c', 'C', noop)

var iterator = db.iterator({
const iterator = db.iterator({
keyAsBuffer: false,
valueAsBuffer: false,
gte: 'a'
Expand All @@ -187,15 +189,15 @@ test('delete while iterating', function (t) {
})

test('iterator with byte range', function (t) {
var db = testCommon.factory()
const db = testCommon.factory()

db.open(function (err) {
t.ifError(err, 'no open error')

db.put(Buffer.from('a0', 'hex'), 'A', function (err) {
t.ifError(err)

var iterator = db.iterator({ valueAsBuffer: false, lt: Buffer.from('ff', 'hex') })
const iterator = db.iterator({ valueAsBuffer: false, lt: Buffer.from('ff', 'hex') })

iterator.next(function (err, key, value) {
t.notOk(err, 'no error')
Expand All @@ -210,8 +212,8 @@ test('iterator with byte range', function (t) {
test('iterator does not clone buffers', function (t) {
t.plan(4)

var db = testCommon.factory()
var buf = Buffer.from('a')
const db = testCommon.factory()
const buf = Buffer.from('a')

db.open(function (err) {
t.ifError(err, 'no open error')
Expand All @@ -229,7 +231,7 @@ test('iterator does not clone buffers', function (t) {
test('iterator stringifies buffer input', function (t) {
t.plan(4)

var db = testCommon.factory()
const db = testCommon.factory()

db.open(function (err) {
t.ifError(err, 'no open error')
Expand All @@ -245,13 +247,13 @@ test('iterator stringifies buffer input', function (t) {
})

test('backing rbtree is buffer-aware', function (t) {
var db = testCommon.factory()
const db = testCommon.factory()

db.open(function (err) {
t.ifError(err, 'no open error')

var one = Buffer.from('80', 'hex')
var two = Buffer.from('c0', 'hex')
const one = Buffer.from('80', 'hex')
const two = Buffer.from('c0', 'hex')

t.ok(two.toString() === one.toString(), 'would be equal when not buffer-aware')
t.ok(ltgt.compare(two, one) > 0, 'but greater when buffer-aware')
Expand Down Expand Up @@ -280,7 +282,7 @@ test('backing rbtree is buffer-aware', function (t) {
test('empty value in batch', function (t) {
t.plan(6)

var db = testCommon.factory()
const db = testCommon.factory()

db.open(function (err) {
t.ifError(err, 'no open error')
Expand Down Expand Up @@ -313,7 +315,7 @@ test('empty value in batch', function (t) {
})

test('empty buffer key in batch', function (t) {
var db = testCommon.factory()
const db = testCommon.factory()

db.open(function (err) {
t.ifError(err, 'no open error')
Expand All @@ -330,7 +332,7 @@ test('empty buffer key in batch', function (t) {
})

test('buffer key in batch', function (t) {
var db = testCommon.factory()
const db = testCommon.factory()

db.open(function (err) {
t.ifError(err, 'no open error')
Expand All @@ -354,7 +356,7 @@ test('buffer key in batch', function (t) {
test('put multiple times', function (t) {
t.plan(5)

var db = testCommon.factory()
const db = testCommon.factory()

db.open(function (err) {
t.ifError(err, 'no open error')
Expand All @@ -377,7 +379,7 @@ test('put multiple times', function (t) {
test('put as string, get as buffer and vice versa', function (t) {
t.plan(7)

var db = testCommon.factory()
const db = testCommon.factory()

db.open(function (err) {
t.ifError(err, 'no open error')
Expand Down Expand Up @@ -405,7 +407,7 @@ test('put as string, get as buffer and vice versa', function (t) {
test('put as string, iterate as buffer', function (t) {
t.plan(4)

var db = testCommon.factory()
const db = testCommon.factory()

db.open(function (err) {
t.ifError(err, 'no open error')
Expand All @@ -424,7 +426,7 @@ test('put as string, iterate as buffer', function (t) {
test('put as buffer, iterate as string', function (t) {
t.plan(4)

var db = testCommon.factory()
const db = testCommon.factory()

db.open(function (err) {
t.ifError(err, 'no open error')
Expand All @@ -443,18 +445,18 @@ test('put as buffer, iterate as string', function (t) {
test('number keys', function (t) {
t.plan(5)

var db = testCommon.factory()
var numbers = [-Infinity, 0, 12, 2, +Infinity]
var strings = numbers.map(String)
var buffers = numbers.map(stringBuffer)
const db = testCommon.factory()
const numbers = [-Infinity, 0, 12, 2, +Infinity]
const strings = numbers.map(String)
const buffers = numbers.map(stringBuffer)

db.open(function (err) {
t.ifError(err, 'no open error')

db.batch(numbers.map(putKey), noop)

var iterator1 = db.iterator({ keyAsBuffer: false })
var iterator2 = db.iterator({ keyAsBuffer: true })
const iterator1 = db.iterator({ keyAsBuffer: false })
const iterator2 = db.iterator({ keyAsBuffer: true })

concat(iterator1, function (err, entries) {
t.ifError(err, 'no iterator error')
Expand Down

0 comments on commit c1f98c3

Please sign in to comment.