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

Commit 0ce815f

Browse files
committed
Modernize syntax and bump standard (Level/community#98)
1 parent 2c20127 commit 0ce815f

File tree

13 files changed

+139
-133
lines changed

13 files changed

+139
-133
lines changed

.github/dependabot.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,5 @@ updates:
55
schedule:
66
interval: monthly
77
ignore:
8-
- dependency-name: buffer
98
- dependency-name: dependency-check
109
- dependency-name: uuid
11-
- dependency-name: standard

index.js

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44

55
module.exports = Level
66

7-
var AbstractLevelDOWN = require('abstract-leveldown').AbstractLevelDOWN
8-
var inherits = require('inherits')
9-
var Iterator = require('./iterator')
10-
var serialize = require('./util/serialize')
11-
var deserialize = require('./util/deserialize')
12-
var support = require('./util/support')
13-
var clear = require('./util/clear')
14-
var createKeyRange = require('./util/key-range')
7+
const AbstractLevelDOWN = require('abstract-leveldown').AbstractLevelDOWN
8+
const inherits = require('inherits')
9+
const Iterator = require('./iterator')
10+
const serialize = require('./util/serialize')
11+
const deserialize = require('./util/deserialize')
12+
const support = require('./util/support')
13+
const clear = require('./util/clear')
14+
const createKeyRange = require('./util/key-range')
1515

16-
var DEFAULT_PREFIX = 'level-js-'
16+
const DEFAULT_PREFIX = 'level-js-'
1717

1818
function Level (location, opts) {
1919
if (!(this instanceof Level)) return new Level(location, opts)
@@ -41,34 +41,33 @@ inherits(Level, AbstractLevelDOWN)
4141
Level.prototype.type = 'level-js'
4242

4343
Level.prototype._open = function (options, callback) {
44-
var req = indexedDB.open(this.prefix + this.location, this.version)
45-
var self = this
44+
const req = indexedDB.open(this.prefix + this.location, this.version)
4645

4746
req.onerror = function () {
4847
callback(req.error || new Error('unknown error'))
4948
}
5049

51-
req.onsuccess = function () {
52-
self.db = req.result
50+
req.onsuccess = () => {
51+
this.db = req.result
5352
callback()
5453
}
5554

56-
req.onupgradeneeded = function (ev) {
57-
var db = ev.target.result
55+
req.onupgradeneeded = (ev) => {
56+
const db = ev.target.result
5857

59-
if (!db.objectStoreNames.contains(self.location)) {
60-
db.createObjectStore(self.location)
58+
if (!db.objectStoreNames.contains(this.location)) {
59+
db.createObjectStore(this.location)
6160
}
6261
}
6362
}
6463

6564
Level.prototype.store = function (mode) {
66-
var transaction = this.db.transaction([this.location], mode)
65+
const transaction = this.db.transaction([this.location], mode)
6766
return transaction.objectStore(this.location)
6867
}
6968

7069
Level.prototype.await = function (request, callback) {
71-
var transaction = request.transaction
70+
const transaction = request.transaction
7271

7372
// Take advantage of the fact that a non-canceled request error aborts
7473
// the transaction. I.e. no need to listen for "request.onerror".
@@ -82,10 +81,11 @@ Level.prototype.await = function (request, callback) {
8281
}
8382

8483
Level.prototype._get = function (key, options, callback) {
85-
var store = this.store('readonly')
84+
const store = this.store('readonly')
85+
let req
8686

8787
try {
88-
var req = store.get(key)
88+
req = store.get(key)
8989
} catch (err) {
9090
return this._nextTick(callback, err)
9191
}
@@ -103,10 +103,11 @@ Level.prototype._get = function (key, options, callback) {
103103
}
104104

105105
Level.prototype._del = function (key, options, callback) {
106-
var store = this.store('readwrite')
106+
const store = this.store('readwrite')
107+
let req
107108

108109
try {
109-
var req = store.delete(key)
110+
req = store.delete(key)
110111
} catch (err) {
111112
return this._nextTick(callback, err)
112113
}
@@ -115,12 +116,13 @@ Level.prototype._del = function (key, options, callback) {
115116
}
116117

117118
Level.prototype._put = function (key, value, options, callback) {
118-
var store = this.store('readwrite')
119+
const store = this.store('readwrite')
120+
let req
119121

120122
try {
121123
// Will throw a DataError or DataCloneError if the environment
122124
// does not support serializing the key or value respectively.
123-
var req = store.put(value, key)
125+
req = store.put(value, key)
124126
} catch (err) {
125127
return this._nextTick(callback, err)
126128
}
@@ -143,10 +145,10 @@ Level.prototype._iterator = function (options) {
143145
Level.prototype._batch = function (operations, options, callback) {
144146
if (operations.length === 0) return this._nextTick(callback)
145147

146-
var store = this.store('readwrite')
147-
var transaction = store.transaction
148-
var index = 0
149-
var error
148+
const store = this.store('readwrite')
149+
const transaction = store.transaction
150+
let index = 0
151+
let error
150152

151153
transaction.onabort = function () {
152154
callback(error || transaction.error || new Error('aborted by user'))
@@ -158,11 +160,13 @@ Level.prototype._batch = function (operations, options, callback) {
158160

159161
// Wait for a request to complete before making the next, saving CPU.
160162
function loop () {
161-
var op = operations[index++]
162-
var key = op.key
163+
const op = operations[index++]
164+
const key = op.key
165+
166+
let req
163167

164168
try {
165-
var req = op.type === 'del' ? store.delete(key) : store.put(op.value, key)
169+
req = op.type === 'del' ? store.delete(key) : store.put(op.value, key)
166170
} catch (err) {
167171
error = err
168172
transaction.abort()
@@ -178,8 +182,11 @@ Level.prototype._batch = function (operations, options, callback) {
178182
}
179183

180184
Level.prototype._clear = function (options, callback) {
185+
let keyRange
186+
let req
187+
181188
try {
182-
var keyRange = createKeyRange(options)
189+
keyRange = createKeyRange(options)
183190
} catch (e) {
184191
// The lower key is greater than the upper key.
185192
// IndexedDB throws an error, but we'll just do nothing.
@@ -193,8 +200,8 @@ Level.prototype._clear = function (options, callback) {
193200
}
194201

195202
try {
196-
var store = this.store('readwrite')
197-
var req = keyRange ? store.delete(keyRange) : store.clear()
203+
const store = this.store('readwrite')
204+
req = keyRange ? store.delete(keyRange) : store.clear()
198205
} catch (err) {
199206
return this._nextTick(callback, err)
200207
}
@@ -213,9 +220,9 @@ Level.prototype.upgrade = function (callback) {
213220
return this._nextTick(callback, new Error('cannot upgrade() before open()'))
214221
}
215222

216-
var it = this.iterator()
217-
var batchOptions = {}
218-
var self = this
223+
const it = this.iterator()
224+
const batchOptions = {}
225+
const self = this
219226

220227
it._deserializeKey = it._deserializeValue = identity
221228
next()
@@ -230,8 +237,8 @@ Level.prototype.upgrade = function (callback) {
230237
return finish(err)
231238
}
232239

233-
var newKey = self._serializeKey(deserialize(key, true))
234-
var newValue = self._serializeValue(deserialize(value, true))
240+
const newKey = self._serializeKey(deserialize(key, true))
241+
const newValue = self._serializeValue(deserialize(value, true))
235242

236243
// To bypass serialization on the old key, use _batch() instead of batch().
237244
// NOTE: if we disable snapshotting (#86) this could lead to a loop of
@@ -259,7 +266,7 @@ Level.destroy = function (location, prefix, callback) {
259266
callback = prefix
260267
prefix = DEFAULT_PREFIX
261268
}
262-
var request = indexedDB.deleteDatabase(prefix + location)
269+
const request = indexedDB.deleteDatabase(prefix + location)
263270
request.onsuccess = function () {
264271
callback()
265272
}

iterator.js

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
'use strict'
22

3-
var inherits = require('inherits')
4-
var AbstractIterator = require('abstract-leveldown').AbstractIterator
5-
var createKeyRange = require('./util/key-range')
6-
var deserialize = require('./util/deserialize')
7-
var noop = function () {}
3+
const inherits = require('inherits')
4+
const AbstractIterator = require('abstract-leveldown').AbstractIterator
5+
const createKeyRange = require('./util/key-range')
6+
const deserialize = require('./util/deserialize')
7+
const noop = function () {}
88

99
module.exports = Iterator
1010

@@ -30,8 +30,10 @@ function Iterator (db, location, options) {
3030
return
3131
}
3232

33+
let keyRange
34+
3335
try {
34-
var keyRange = createKeyRange(options)
36+
keyRange = createKeyRange(options)
3537
} catch (e) {
3638
// The lower key is greater than the upper key.
3739
// IndexedDB throws an error, but we'll just return 0 results.
@@ -45,25 +47,24 @@ function Iterator (db, location, options) {
4547
inherits(Iterator, AbstractIterator)
4648

4749
Iterator.prototype.createIterator = function (location, keyRange, reverse) {
48-
var self = this
49-
var transaction = this.db.db.transaction([location], 'readonly')
50-
var store = transaction.objectStore(location)
51-
var req = store.openCursor(keyRange, reverse ? 'prev' : 'next')
52-
53-
req.onsuccess = function (ev) {
54-
var cursor = ev.target.result
55-
if (cursor) self.onItem(cursor)
50+
const transaction = this.db.db.transaction([location], 'readonly')
51+
const store = transaction.objectStore(location)
52+
const req = store.openCursor(keyRange, reverse ? 'prev' : 'next')
53+
54+
req.onsuccess = (ev) => {
55+
const cursor = ev.target.result
56+
if (cursor) this.onItem(cursor)
5657
}
5758

5859
this._transaction = transaction
5960

6061
// If an error occurs (on the request), the transaction will abort.
61-
transaction.onabort = function () {
62-
self.onAbort(self._transaction.error || new Error('aborted by user'))
62+
transaction.onabort = () => {
63+
this.onAbort(this._transaction.error || new Error('aborted by user'))
6364
}
6465

65-
transaction.oncomplete = function () {
66-
self.onComplete()
66+
transaction.oncomplete = () => {
67+
this.onComplete()
6768
}
6869
}
6970

@@ -98,12 +99,12 @@ Iterator.prototype.maybeNext = function () {
9899
Iterator.prototype._next = function (callback) {
99100
if (this._aborted) {
100101
// The error should be picked up by either next() or end().
101-
var err = this._error
102+
const err = this._error
102103
this._error = null
103104
this._nextTick(callback, err)
104105
} else if (this._cache.length > 0) {
105-
var key = this._cache.shift()
106-
var value = this._cache.shift()
106+
let key = this._cache.shift()
107+
let value = this._cache.shift()
107108

108109
if (this._keys && key !== undefined) {
109110
key = this._deserializeKey(key, this._keyAsBuffer)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"level-community": "^3.0.0",
3939
"level-concat-iterator": "^2.0.0",
4040
"nyc": "^15.0.0",
41-
"standard": "^15.0.1",
41+
"standard": "^16.0.3",
4242
"tape": "^5.0.0",
4343
"uuid": "^3.3.2"
4444
},

0 commit comments

Comments
 (0)