Skip to content
This repository was archived by the owner on Dec 1, 2024. It is now read-only.

Commit 762d989

Browse files
committed
Breaking: modernize syntax and bump standard (Level/community#98)
1 parent 14969c8 commit 762d989

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+373
-381
lines changed

.github/dependabot.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ updates:
77
ignore:
88
- dependency-name: dependency-check
99
- dependency-name: sinon
10-
- dependency-name: airtap
1110
- dependency-name: nyc
12-
- dependency-name: standard
1311

1412
# Pinned to 1.1.10 for browser compatibility (later versions depend
1513
# on `queue-microtask` which uses arrow functions and promises).

lib/batch.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
var WriteError = require('level-errors').WriteError
2-
var catering = require('catering')
3-
var getCallback = require('./common').getCallback
4-
var getOptions = require('./common').getOptions
1+
'use strict'
2+
3+
const WriteError = require('level-errors').WriteError
4+
const catering = require('catering')
5+
const getCallback = require('./common').getCallback
6+
const getOptions = require('./common').getOptions
57

68
function Batch (levelup) {
79
// TODO (next major): remove this._levelup alias
@@ -18,7 +20,7 @@ Batch.prototype.put = function (key, value) {
1820
throw new WriteError(e)
1921
}
2022

21-
this.ops.push({ type: 'put', key: key, value: value })
23+
this.ops.push({ type: 'put', key, value })
2224
this.length++
2325

2426
return this
@@ -31,7 +33,7 @@ Batch.prototype.del = function (key) {
3133
throw new WriteError(err)
3234
}
3335

34-
this.ops.push({ type: 'del', key: key })
36+
this.ops.push({ type: 'del', key })
3537
this.length++
3638

3739
return this
@@ -51,8 +53,8 @@ Batch.prototype.clear = function () {
5153
}
5254

5355
Batch.prototype.write = function (options, callback) {
54-
var levelup = this._levelup
55-
var ops = this.ops
56+
const levelup = this._levelup
57+
const ops = this.ops
5658

5759
callback = getCallback(options, callback)
5860
callback = catering.fromCallback(callback)

lib/common.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict'
2+
13
exports.getCallback = function (options, callback) {
24
return typeof options === 'function' ? options : callback
35
}

lib/levelup.js

Lines changed: 49 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
var EventEmitter = require('events').EventEmitter
2-
var inherits = require('util').inherits
3-
var extend = require('xtend')
4-
var DeferredLevelDOWN = require('deferred-leveldown')
5-
var IteratorStream = require('level-iterator-stream')
6-
var Batch = require('./batch')
7-
var errors = require('level-errors')
8-
var supports = require('level-supports')
9-
var assert = require('assert')
10-
var catering = require('catering')
11-
var getCallback = require('./common').getCallback
12-
var getOptions = require('./common').getOptions
13-
14-
var WriteError = errors.WriteError
15-
var ReadError = errors.ReadError
16-
var NotFoundError = errors.NotFoundError
17-
var OpenError = errors.OpenError
18-
var InitializationError = errors.InitializationError
1+
'use strict'
2+
3+
const EventEmitter = require('events').EventEmitter
4+
const inherits = require('util').inherits
5+
const extend = require('xtend')
6+
const DeferredLevelDOWN = require('deferred-leveldown')
7+
const IteratorStream = require('level-iterator-stream')
8+
const Batch = require('./batch')
9+
const errors = require('level-errors')
10+
const supports = require('level-supports')
11+
const assert = require('assert')
12+
const catering = require('catering')
13+
const getCallback = require('./common').getCallback
14+
const getOptions = require('./common').getOptions
15+
16+
const WriteError = errors.WriteError
17+
const ReadError = errors.ReadError
18+
const NotFoundError = errors.NotFoundError
19+
const OpenError = errors.OpenError
20+
const InitializationError = errors.InitializationError
1921

2022
// Possible AbstractLevelDOWN#status values:
2123
// - 'new' - newly created, not opened or closed
@@ -30,8 +32,7 @@ function LevelUP (db, options, callback) {
3032
return new LevelUP(db, options, callback)
3133
}
3234

33-
var error
34-
var self = this
35+
let error
3536

3637
EventEmitter.call(this)
3738
this.setMaxListeners(Infinity)
@@ -56,9 +57,9 @@ function LevelUP (db, options, callback) {
5657
this.options = getOptions(options)
5758
this._db = db
5859
this.db = new DeferredLevelDOWN(db)
59-
this.open(callback || function (err) {
60-
if (err) self.emit('error', err)
61-
})
60+
this.open(callback || ((err) => {
61+
if (err) this.emit('error', err)
62+
}))
6263

6364
// Create manifest based on deferred-leveldown's
6465
this.supports = supports(this.db.supports, {
@@ -70,23 +71,21 @@ function LevelUP (db, options, callback) {
7071
})
7172

7273
// Experimental: enrich levelup interface
73-
Object.keys(this.supports.additionalMethods).forEach(function (method) {
74-
if (this[method] != null) return
74+
for (const method of Object.keys(this.supports.additionalMethods)) {
75+
if (this[method] != null) continue
7576

7677
// Don't do this.db[method].bind() because this.db is dynamic.
77-
this[method] = function () {
78-
return this.db[method].apply(this.db, arguments)
78+
this[method] = function (...args) {
79+
return this.db[method](...args)
7980
}
80-
}, this)
81+
}
8182
}
8283

8384
LevelUP.prototype.emit = EventEmitter.prototype.emit
8485
LevelUP.prototype.once = EventEmitter.prototype.once
8586
inherits(LevelUP, EventEmitter)
8687

8788
LevelUP.prototype.open = function (opts, callback) {
88-
var self = this
89-
9089
if (typeof opts === 'function') {
9190
callback = opts
9291
opts = null
@@ -99,39 +98,37 @@ LevelUP.prototype.open = function (opts, callback) {
9998
}
10099

101100
if (this.isOpen()) {
102-
process.nextTick(callback, null, self)
101+
process.nextTick(callback, null, this)
103102
return callback.promise
104103
}
105104

106105
if (this._isOpening()) {
107-
this.once('open', function () { callback(null, self) })
106+
this.once('open', () => { callback(null, this) })
108107
return callback.promise
109108
}
110109

111110
this.emit('opening')
112111

113-
this.db.open(opts, function (err) {
112+
this.db.open(opts, (err) => {
114113
if (err) {
115114
return callback(new OpenError(err))
116115
}
117-
self.db = self._db
118-
callback(null, self)
119-
self.emit('open')
120-
self.emit('ready')
116+
this.db = this._db
117+
callback(null, this)
118+
this.emit('open')
119+
this.emit('ready')
121120
})
122121

123122
return callback.promise
124123
}
125124

126125
LevelUP.prototype.close = function (callback) {
127-
var self = this
128-
129126
callback = catering.fromCallback(callback)
130127

131128
if (this.isOpen()) {
132-
this.db.close(function () {
133-
self.emit('closed')
134-
callback.apply(null, arguments)
129+
this.db.close((err, ...rest) => {
130+
this.emit('closed')
131+
callback(err, ...rest)
135132
})
136133
this.emit('closing')
137134
this.db = new DeferredLevelDOWN(this._db)
@@ -140,8 +137,8 @@ LevelUP.prototype.close = function (callback) {
140137
} else if (this.db.status === 'closing') {
141138
this.once('closed', callback)
142139
} else if (this._isOpening()) {
143-
this.once('open', function () {
144-
self.close(callback)
140+
this.once('open', () => {
141+
this.close(callback)
145142
})
146143
}
147144

@@ -186,8 +183,6 @@ LevelUP.prototype.get = function (key, options, callback) {
186183
}
187184

188185
LevelUP.prototype.put = function (key, value, options, callback) {
189-
var self = this
190-
191186
callback = getCallback(options, callback)
192187
callback = catering.fromCallback(callback)
193188

@@ -197,20 +192,18 @@ LevelUP.prototype.put = function (key, value, options, callback) {
197192

198193
options = getOptions(options)
199194

200-
this.db.put(key, value, options, function (err) {
195+
this.db.put(key, value, options, (err) => {
201196
if (err) {
202197
return callback(new WriteError(err))
203198
}
204-
self.emit('put', key, value)
199+
this.emit('put', key, value)
205200
callback()
206201
})
207202

208203
return callback.promise
209204
}
210205

211206
LevelUP.prototype.del = function (key, options, callback) {
212-
var self = this
213-
214207
callback = getCallback(options, callback)
215208
callback = catering.fromCallback(callback)
216209

@@ -220,11 +213,11 @@ LevelUP.prototype.del = function (key, options, callback) {
220213

221214
options = getOptions(options)
222215

223-
this.db.del(key, options, function (err) {
216+
this.db.del(key, options, (err) => {
224217
if (err) {
225218
return callback(new WriteError(err))
226219
}
227-
self.emit('del', key)
220+
this.emit('del', key)
228221
callback()
229222
})
230223

@@ -236,8 +229,6 @@ LevelUP.prototype.batch = function (arr, options, callback) {
236229
return new Batch(this)
237230
}
238231

239-
var self = this
240-
241232
if (typeof arr === 'function') callback = arr
242233
else callback = getCallback(options, callback)
243234

@@ -249,11 +240,11 @@ LevelUP.prototype.batch = function (arr, options, callback) {
249240

250241
options = getOptions(options)
251242

252-
this.db.batch(arr, options, function (err) {
243+
this.db.batch(arr, options, (err) => {
253244
if (err) {
254245
return callback(new WriteError(err))
255246
}
256-
self.emit('batch', arr)
247+
this.emit('batch', arr)
257248
callback()
258249
})
259250

@@ -265,8 +256,6 @@ LevelUP.prototype.iterator = function (options) {
265256
}
266257

267258
LevelUP.prototype.clear = function (options, callback) {
268-
var self = this
269-
270259
callback = getCallback(options, callback)
271260
options = getOptions(options)
272261
callback = catering.fromCallback(callback)
@@ -275,11 +264,11 @@ LevelUP.prototype.clear = function (options, callback) {
275264
return callback.promise
276265
}
277266

278-
this.db.clear(options, function (err) {
267+
this.db.clear(options, (err) => {
279268
if (err) {
280269
return callback(new WriteError(err))
281270
}
282-
self.emit('clear', options)
271+
this.emit('clear', options)
283272
callback()
284273
})
285274

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"safe-buffer": "^5.1.0",
4646
"simple-concat": "^1.0.0",
4747
"sinon": "^7.4.2",
48-
"standard": "^15.0.0",
48+
"standard": "^16.0.3",
4949
"tape": "^5.2.2",
5050
"trickle": "0.0.2"
5151
},

test/batch-test.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
var levelup = require('../lib/levelup')
2-
var errors = levelup.errors
3-
var each = require('async-each')
4-
var series = require('run-series')
5-
var discardable = require('./util/discardable')
1+
const levelup = require('../lib/levelup')
2+
const errors = levelup.errors
3+
const each = require('async-each')
4+
const series = require('run-series')
5+
const discardable = require('./util/discardable')
66

77
module.exports = function (test, testCommon) {
88
test('array-form batch(): multiple puts', function (t) {
@@ -124,11 +124,11 @@ module.exports = function (test, testCommon) {
124124

125125
test('chained batch(): options', function (t) {
126126
discardable(t, testCommon, function (db, done) {
127-
var batch = db.batch()
128-
var underlying = batch
127+
const batch = db.batch()
128+
let underlying = batch
129129
while (underlying.batch) underlying = underlying.batch
130130

131-
var write = underlying.write.bind(underlying)
131+
const write = underlying.write.bind(underlying)
132132
underlying.write = function (options, cb) {
133133
t.same(options, { foo: 'bar' })
134134
write(options, cb)
@@ -144,9 +144,9 @@ module.exports = function (test, testCommon) {
144144

145145
testCommon.promises && test('chained batch(): promise interface - options', function (t) {
146146
discardable(t, testCommon, function (db, done) {
147-
var batch = db.batch()
147+
const batch = db.batch()
148148

149-
var write = batch.batch.write.bind(batch.batch)
149+
const write = batch.batch.write.bind(batch.batch)
150150
batch.batch.write = function (options, cb) {
151151
t.same(options, { foo: 'bar' })
152152
write(options, cb)
@@ -194,7 +194,7 @@ module.exports = function (test, testCommon) {
194194

195195
test('chained batch(): exposes ops queue length', function (t) {
196196
discardable(t, testCommon, function (db, done) {
197-
var batch = db.batch()
197+
const batch = db.batch()
198198
.put('one', '1')
199199
.del('two')
200200
.put('three', '3')
@@ -288,7 +288,7 @@ module.exports = function (test, testCommon) {
288288

289289
test('chained batch() arguments', function (t) {
290290
discardable(t, testCommon, function (db, done) {
291-
var batch = db.batch()
291+
const batch = db.batch()
292292

293293
t.test('chained batch() arguments: batch#put() with missing `value`', function (t) {
294294
throws(t, batch.put.bind(batch, 'foo1'), function (err) {
@@ -360,7 +360,7 @@ module.exports = function (test, testCommon) {
360360
t.is(err.message, 'write() already called on this batch')
361361
}
362362

363-
var batch = db.batch()
363+
const batch = db.batch()
364364
batch.put('foo', 'bar').put('boom', 'bang').del('foo').write(function (err) {
365365
t.ifError(err, 'no batch error')
366366

0 commit comments

Comments
 (0)