Skip to content

Commit

Permalink
batch array: assert elements are objects
Browse files Browse the repository at this point in the history
  • Loading branch information
vweevers committed Nov 25, 2017
1 parent 4fdc758 commit a2621ad
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
8 changes: 4 additions & 4 deletions abstract-leveldown.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ AbstractLevelDOWN.prototype.batch = function (array, options, callback) {
if (!options || typeof options != 'object')
options = {}

var serialized = []
var serialized = new Array(array.length)

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

var e = Object.assign({}, array[i])
var err
Expand All @@ -181,7 +181,7 @@ AbstractLevelDOWN.prototype.batch = function (array, options, callback) {
if (e.type !== 'del')
e.value = this._serializeValue(e.value)

serialized.push(e)
serialized[i] = e
}

if (typeof this._batch == 'function')
Expand Down
12 changes: 12 additions & 0 deletions abstract/batch-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ module.exports.args = function (test) {
t.end()
})
})

;[null, undefined, 1, true].forEach(function (element) {
var type = element === null ? 'null' : typeof element

test('test batch() with ' + type + ' element', function (t) {
db.batch([element], function (err) {
t.ok(err, 'got error')
t.equal(err.message, 'batch(array) element must be an object and not `null`', 'correct error message')
t.end()
})
})
})
}

module.exports.batch = function (test) {
Expand Down

0 comments on commit a2621ad

Please sign in to comment.