Skip to content

Commit

Permalink
Modernize syntax and bump standard from 14.x to 16.x
Browse files Browse the repository at this point in the history
  • Loading branch information
vweevers committed Apr 9, 2021
1 parent 9d4171f commit cfe8040
Show file tree
Hide file tree
Showing 32 changed files with 386 additions and 373 deletions.
1 change: 0 additions & 1 deletion .github/dependabot.yml
Expand Up @@ -6,4 +6,3 @@ updates:
interval: monthly
ignore:
- dependency-name: dependency-check
- dependency-name: standard
8 changes: 5 additions & 3 deletions abstract-chained-batch.js
@@ -1,4 +1,6 @@
var nextTick = require('./next-tick')
'use strict'

const nextTick = require('./next-tick')

function AbstractChainedBatch (db) {
if (typeof db !== 'object' || db === null) {
Expand All @@ -19,7 +21,7 @@ AbstractChainedBatch.prototype._checkWritten = function () {
AbstractChainedBatch.prototype.put = function (key, value) {
this._checkWritten()

var err = this.db._checkKey(key) || this.db._checkValue(value)
const err = this.db._checkKey(key) || this.db._checkValue(value)
if (err) throw err

key = this.db._serializeKey(key)
Expand All @@ -37,7 +39,7 @@ AbstractChainedBatch.prototype._put = function (key, value) {
AbstractChainedBatch.prototype.del = function (key) {
this._checkWritten()

var err = this.db._checkKey(key)
const err = this.db._checkKey(key)
if (err) throw err

key = this.db._serializeKey(key)
Expand Down
24 changes: 12 additions & 12 deletions abstract-iterator.js
@@ -1,4 +1,6 @@
var nextTick = require('./next-tick')
'use strict'

const nextTick = require('./next-tick')

function AbstractIterator (db) {
if (typeof db !== 'object' || db === null) {
Expand All @@ -11,29 +13,27 @@ function AbstractIterator (db) {
}

AbstractIterator.prototype.next = function (callback) {
var self = this

if (typeof callback !== 'function') {
throw new Error('next() requires a callback argument')
}

if (self._ended) {
if (this._ended) {
nextTick(callback, new Error('cannot call next() after end()'))
return self
return this
}

if (self._nexting) {
if (this._nexting) {
nextTick(callback, new Error('cannot call next() before previous next() has completed'))
return self
return this
}

self._nexting = true
self._next(function () {
self._nexting = false
callback.apply(null, arguments)
this._nexting = true
this._next((err, ...rest) => {
this._nexting = false
callback(err, ...rest)
})

return self
return this
}

AbstractIterator.prototype._next = function (callback) {
Expand Down
67 changes: 33 additions & 34 deletions abstract-leveldown.js
@@ -1,10 +1,12 @@
var supports = require('level-supports')
var Buffer = require('buffer').Buffer
var AbstractIterator = require('./abstract-iterator')
var AbstractChainedBatch = require('./abstract-chained-batch')
var nextTick = require('./next-tick')
var hasOwnProperty = Object.prototype.hasOwnProperty
var rangeOptions = ['lt', 'lte', 'gt', 'gte']
'use strict'

const supports = require('level-supports')
const Buffer = require('buffer').Buffer
const AbstractIterator = require('./abstract-iterator')
const AbstractChainedBatch = require('./abstract-chained-batch')
const nextTick = require('./next-tick')
const hasOwnProperty = Object.prototype.hasOwnProperty
const rangeOptions = ['lt', 'lte', 'gt', 'gte']

function AbstractLevelDOWN (manifest) {
this.status = 'new'
Expand All @@ -16,8 +18,7 @@ function AbstractLevelDOWN (manifest) {
}

AbstractLevelDOWN.prototype.open = function (options, callback) {
var self = this
var oldStatus = this.status
const oldStatus = this.status

if (typeof options === 'function') callback = options

Expand All @@ -31,12 +32,12 @@ AbstractLevelDOWN.prototype.open = function (options, callback) {
options.errorIfExists = !!options.errorIfExists

this.status = 'opening'
this._open(options, function (err) {
this._open(options, (err) => {
if (err) {
self.status = oldStatus
this.status = oldStatus
return callback(err)
}
self.status = 'open'
this.status = 'open'
callback()
})
}
Expand All @@ -46,20 +47,19 @@ AbstractLevelDOWN.prototype._open = function (options, callback) {
}

AbstractLevelDOWN.prototype.close = function (callback) {
var self = this
var oldStatus = this.status
const oldStatus = this.status

if (typeof callback !== 'function') {
throw new Error('close() requires a callback argument')
}

this.status = 'closing'
this._close(function (err) {
this._close((err) => {
if (err) {
self.status = oldStatus
this.status = oldStatus
return callback(err)
}
self.status = 'closed'
this.status = 'closed'
callback()
})
}
Expand All @@ -75,7 +75,7 @@ AbstractLevelDOWN.prototype.get = function (key, options, callback) {
throw new Error('get() requires a callback argument')
}

var err = this._checkKey(key)
const err = this._checkKey(key)
if (err) return nextTick(callback, err)

key = this._serializeKey(key)
Expand All @@ -98,7 +98,7 @@ AbstractLevelDOWN.prototype.put = function (key, value, options, callback) {
throw new Error('put() requires a callback argument')
}

var err = this._checkKey(key) || this._checkValue(value)
const err = this._checkKey(key) || this._checkValue(value)
if (err) return nextTick(callback, err)

key = this._serializeKey(key)
Expand All @@ -120,7 +120,7 @@ AbstractLevelDOWN.prototype.del = function (key, options, callback) {
throw new Error('del() requires a callback argument')
}

var err = this._checkKey(key)
const err = this._checkKey(key)
if (err) return nextTick(callback, err)

key = this._serializeKey(key)
Expand Down Expand Up @@ -155,26 +155,26 @@ AbstractLevelDOWN.prototype.batch = function (array, options, callback) {

if (typeof options !== 'object' || options === null) options = {}

var serialized = new Array(array.length)
const serialized = new Array(array.length)

for (var i = 0; i < array.length; i++) {
for (let i = 0; i < array.length; i++) {
if (typeof array[i] !== 'object' || array[i] === null) {
return nextTick(callback, new Error('batch(array) element must be an object and not `null`'))
}

var e = Object.assign({}, array[i])
const e = Object.assign({}, array[i])

if (e.type !== 'put' && e.type !== 'del') {
return nextTick(callback, new Error("`type` must be 'put' or 'del'"))
}

var err = this._checkKey(e.key)
const err = this._checkKey(e.key)
if (err) return nextTick(callback, err)

e.key = this._serializeKey(e.key)

if (e.type === 'put') {
var valueErr = this._checkValue(e.value)
const valueErr = this._checkValue(e.value)
if (valueErr) return nextTick(callback, valueErr)

e.value = this._serializeValue(e.value)
Expand Down Expand Up @@ -211,25 +211,24 @@ AbstractLevelDOWN.prototype._clear = function (options, callback) {
options.keyAsBuffer = true
options.valueAsBuffer = true

var iterator = this._iterator(options)
var emptyOptions = {}
var self = this
const iterator = this._iterator(options)
const emptyOptions = {}

var next = function (err) {
const next = (err) => {
if (err) {
return iterator.end(function () {
callback(err)
})
}

iterator.next(function (err, key) {
iterator.next((err, key) => {
if (err) return next(err)
if (key === undefined) return iterator.end(callback)

// This could be optimized by using a batch, but the default _clear
// is not meant to be fast. Implementations have more room to optimize
// if they override _clear. Note: using _del bypasses key serialization.
self._del(key, emptyOptions, next)
this._del(key, emptyOptions, next)
})
}

Expand All @@ -250,16 +249,16 @@ AbstractLevelDOWN.prototype._setupIteratorOptions = function (options) {
}

function cleanRangeOptions (db, options) {
var result = {}
const result = {}

for (var k in options) {
for (const k in options) {
if (!hasOwnProperty.call(options, k)) continue

if (k === 'start' || k === 'end') {
throw new Error('Legacy range options ("start" and "end") have been removed')
}

var opt = options[k]
let opt = options[k]

if (isRangeOption(k)) {
// Note that we don't reject nullish and empty options here. While
Expand Down
2 changes: 2 additions & 0 deletions index.js
@@ -1,3 +1,5 @@
'use strict'

exports.AbstractLevelDOWN = require('./abstract-leveldown')
exports.AbstractIterator = require('./abstract-iterator')
exports.AbstractChainedBatch = require('./abstract-chained-batch')
2 changes: 2 additions & 0 deletions next-tick-browser.js
@@ -1 +1,3 @@
'use strict'

module.exports = require('immediate')
2 changes: 2 additions & 0 deletions next-tick.js
@@ -1 +1,3 @@
'use strict'

module.exports = process.nextTick
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -31,7 +31,7 @@
"level-community": "^3.0.0",
"nyc": "^15.1.0",
"sinon": "^10.0.0",
"standard": "^14.0.0",
"standard": "^16.0.3",
"tape": "^5.0.1"
},
"hallmark": {
Expand Down

0 comments on commit cfe8040

Please sign in to comment.