Skip to content

Commit

Permalink
Refactored lib code to let/const
Browse files Browse the repository at this point in the history
- this was supported from Node v6 and we support Node v10 so there
  shouldn't be a problem there
  • Loading branch information
daniellockyer committed Apr 30, 2022
1 parent a235752 commit f924bbb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 38 deletions.
8 changes: 4 additions & 4 deletions lib/sqlite3-binding.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var binary = require('@mapbox/node-pre-gyp');
var path = require('path');
var binding_path = binary.find(path.resolve(path.join(__dirname,'../package.json')));
var binding = require(binding_path);
const binary = require('@mapbox/node-pre-gyp');
const path = require('path');
const binding_path = binary.find(path.resolve(path.join(__dirname,'../package.json')));
const binding = require(binding_path);
module.exports = exports = binding;
58 changes: 30 additions & 28 deletions lib/sqlite3.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
var path = require('path');
var sqlite3 = require('./sqlite3-binding.js');
var EventEmitter = require('events').EventEmitter;
const path = require('path');
const sqlite3 = require('./sqlite3-binding.js');
const EventEmitter = require('events').EventEmitter;
module.exports = exports = sqlite3;

function normalizeMethod (fn) {
return function (sql) {
var errBack;
var args = Array.prototype.slice.call(arguments, 1);
let errBack;
const args = Array.prototype.slice.call(arguments, 1);

if (typeof args[args.length - 1] === 'function') {
var callback = args[args.length - 1];
const callback = args[args.length - 1];
errBack = function(err) {
if (err) {
callback(err);
}
};
}
var statement = new Statement(this, sql, errBack);
const statement = new Statement(this, sql, errBack);
return fn.call(this, statement, args);
};
}

function inherits(target, source) {
for (var k in source.prototype)
for (const k in source.prototype)
target.prototype[k] = source.prototype[k];
}

Expand All @@ -32,18 +33,18 @@ sqlite3.cached = {
return new Database(file, a, b);
}

var db;
let db;
file = path.resolve(file);
function cb() { callback.call(db, null); }

if (!sqlite3.cached.objects[file]) {
db = sqlite3.cached.objects[file] = new Database(file, a, b);
}
else {
// Make sure the callback is called.
db = sqlite3.cached.objects[file];
var callback = (typeof a === 'number') ? b : a;
const callback = (typeof a === 'number') ? b : a;
if (typeof callback === 'function') {
function cb() { callback.call(db, null); }
if (db.open) process.nextTick(cb);
else db.once('open', cb);
}
Expand All @@ -55,9 +56,9 @@ sqlite3.cached = {
};


var Database = sqlite3.Database;
var Statement = sqlite3.Statement;
var Backup = sqlite3.Backup;
const Database = sqlite3.Database;
const Statement = sqlite3.Statement;
const Backup = sqlite3.Backup;

inherits(Database, EventEmitter);
inherits(Statement, EventEmitter);
Expand Down Expand Up @@ -102,7 +103,7 @@ Database.prototype.map = normalizeMethod(function(statement, params) {
// Database#backup(filename, [callback])
// Database#backup(filename, destName, sourceName, filenameIsDest, [callback])
Database.prototype.backup = function() {
var backup;
let backup;
if (arguments.length <= 2) {
// By default, we write the main database out to the main database of the named file.
// This is the most likely use of the backup api.
Expand All @@ -117,22 +118,23 @@ Database.prototype.backup = function() {
};

Statement.prototype.map = function() {
var params = Array.prototype.slice.call(arguments);
var callback = params.pop();
const params = Array.prototype.slice.call(arguments);
const callback = params.pop();
params.push(function(err, rows) {
if (err) return callback(err);
var result = {};
const result = {};
if (rows.length) {
var keys = Object.keys(rows[0]), key = keys[0];
const keys = Object.keys(rows[0]);
const key = keys[0];
if (keys.length > 2) {
// Value is an object
for (var i = 0; i < rows.length; i++) {
for (let i = 0; i < rows.length; i++) {
result[rows[i][key]] = rows[i];
}
} else {
var value = keys[1];
const value = keys[1];
// Value is a plain value
for (i = 0; i < rows.length; i++) {
for (let i = 0; i < rows.length; i++) {
result[rows[i][key]] = rows[i][value];
}
}
Expand All @@ -142,28 +144,28 @@ Statement.prototype.map = function() {
return this.all.apply(this, params);
};

var isVerbose = false;
let isVerbose = false;

var supportedEvents = [ 'trace', 'profile', 'insert', 'update', 'delete' ];
const supportedEvents = [ 'trace', 'profile', 'insert', 'update', 'delete' ];

Database.prototype.addListener = Database.prototype.on = function(type) {
var val = EventEmitter.prototype.addListener.apply(this, arguments);
const val = EventEmitter.prototype.addListener.apply(this, arguments);
if (supportedEvents.indexOf(type) >= 0) {
this.configure(type, true);
}
return val;
};

Database.prototype.removeListener = function(type) {
var val = EventEmitter.prototype.removeListener.apply(this, arguments);
const val = EventEmitter.prototype.removeListener.apply(this, arguments);
if (supportedEvents.indexOf(type) >= 0 && !this._events[type]) {
this.configure(type, false);
}
return val;
};

Database.prototype.removeAllListeners = function(type) {
var val = EventEmitter.prototype.removeAllListeners.apply(this, arguments);
const val = EventEmitter.prototype.removeAllListeners.apply(this, arguments);
if (supportedEvents.indexOf(type) >= 0) {
this.configure(type, false);
}
Expand All @@ -173,7 +175,7 @@ Database.prototype.removeAllListeners = function(type) {
// Save the stack trace over EIO callbacks.
sqlite3.verbose = function() {
if (!isVerbose) {
var trace = require('./trace');
const trace = require('./trace');
[
'prepare',
'get',
Expand Down
12 changes: 6 additions & 6 deletions lib/trace.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
// Inspired by https://github.com/tlrobinson/long-stack-traces
var util = require('util');
const util = require('util');

function extendTrace(object, property, pos) {
var old = object[property];
const old = object[property];
object[property] = function() {
var error = new Error();
var name = object.constructor.name + '#' + property + '(' +
const error = new Error();
const name = object.constructor.name + '#' + property + '(' +
Array.prototype.slice.call(arguments).map(function(el) {
return util.inspect(el, false, 0);
}).join(', ') + ')';

if (typeof pos === 'undefined') pos = -1;
if (pos < 0) pos += arguments.length;
var cb = arguments[pos];
const cb = arguments[pos];
if (typeof arguments[pos] === 'function') {
arguments[pos] = function replacement() {
var err = arguments[0];
const err = arguments[0];
if (err && err.stack && !err.__augmented) {
err.stack = filter(err).join('\n');
err.stack += '\n--> in ' + name;
Expand Down

0 comments on commit f924bbb

Please sign in to comment.