Skip to content

Commit 156e205

Browse files
committed
refactor: Drop lodash in favor of native JavaScript
Replace all lodash helpers (type checks, each/map/reduce/reject/range/tail) with native equivalents. Also fixes a latent bug in connection.js allType(), which always returned true.
1 parent 9e31ad3 commit 156e205

11 files changed

Lines changed: 183 additions & 242 deletions

lib/callablestatement.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* jshint node: true */
22
"use strict";
3-
var _ = require('lodash');
43
var PreparedStatement = require('./preparedstatement');
54

65
function CallableStatement(cs) {
@@ -122,8 +121,8 @@ CallableStatement.prototype.getDate = function(arg1, arg2, callback) {
122121

123122
// Check arguments for validity, and return error if invalid
124123
var validArgs = (
125-
(_.isNumber(args[0]) || _.isString(args[0])) &&
126-
(_.isUndefined(args[1]) || _.isObject(args[1]))
124+
(typeof args[0] === 'number' || typeof args[0] === 'string') &&
125+
(args[1] === undefined || (typeof args[1] === 'object' && args[1] !== null))
127126
);
128127
if (! validArgs) {
129128
return callback(new Error("INVALID ARGUMENTS"));

lib/connection.js

Lines changed: 13 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* jshint node: true */
22
"use strict";
3-
var _ = require("lodash");
43
var jinst = require("./jinst");
54
var CallableStatement = require('./callablestatement');
65
var PreparedStatement = require('./preparedstatement');
@@ -97,16 +96,7 @@ Connection.prototype.createStatement = function(arg1, arg2, arg3, callback) {
9796
callback = args.pop();
9897

9998
// Check arguments for validity, and return error if invalid
100-
var invalidArgs = false;
101-
_.forEach(args, function(arg) {
102-
if (! _.isNumber(arg)) {
103-
invalidArgs = true;
104-
// Lodash break
105-
return false;
106-
}
107-
});
108-
109-
if (invalidArgs) {
99+
if (! allType(args, 'number')) {
110100
return callback(new Error("INVALID ARGUMENTS"));
111101
}
112102

@@ -308,13 +298,9 @@ Connection.prototype.prepareCall = function(sql, rstype, rsconcurrency, rsholdab
308298
};
309299

310300
function allType(array, type) {
311-
_.each(array, function(el) {
312-
if (typeof el !== type) {
313-
return false;
314-
}
301+
return array.every(function(el) {
302+
return typeof el === type;
315303
});
316-
317-
return true;
318304
}
319305

320306
/**
@@ -351,34 +337,13 @@ Connection.prototype.prepareStatement = function(sql, arg1, arg2, arg3, callback
351337
// be numbers if given, except for the special case when the first
352338
// of these arguments is an array and no other arguments are given.
353339
// In this special case, the array must be a string or number array.
354-
//
355-
// NOTE: _.tail returns all but the first argument, so we are only
356-
// processing arg1, arg2, and arg3; and not sql (or callback, which
357-
// was already removed from the args array).
358-
var invalidArgs = false;
359-
_.forEach(_.tail(args), function(arg, idx) {
360-
// Check for the special case where arg1 can be an array of strings or numbers
361-
// if arg2 and arg3 are not given
362-
if (idx === 0 && _.isArray(arg) && _.isUndefined(args[2]) && _.isUndefined(args[3])) {
363-
if (! (allType(arg, 'string') || allType(arg, 'number'))) {
364-
invalidArgs = true;
365-
366-
// Lodash break
367-
return false;
368-
}
369-
370-
// Lodash continue
371-
return;
372-
}
373-
374-
// Other than the special case above, these args must be numbers
375-
if (! _.isNumber(arg)) {
376-
invalidArgs = true;
377-
378-
// Lodash break
379-
return false;
380-
}
381-
});
340+
var invalidArgs;
341+
if (args.length === 2 && Array.isArray(args[1])) {
342+
invalidArgs = ! (allType(args[1], 'string') || allType(args[1], 'number'));
343+
} else {
344+
// args[0] is sql (callback was already removed from the args array)
345+
invalidArgs = ! allType(args.slice(1), 'number');
346+
}
382347

383348
if (invalidArgs) {
384349
return callback(new Error(errMsg));
@@ -414,11 +379,6 @@ Connection.prototype.rollback = function(savepoint, callback) {
414379
// Pull the callback off the end of the arguments
415380
callback = args.pop();
416381

417-
// Check arguments for validity, and return error if invalid
418-
// if (! _.isObject(args[0])) {
419-
// return callback(new Error("INVALID ARGUMENTS"));
420-
// }
421-
422382
// Push a callback handler onto the arguments
423383
args.push(function(err) {
424384
if (err) {
@@ -461,9 +421,9 @@ Connection.prototype.setClientInfo = function(props, name, value, callback) {
461421

462422
// Check arguments for validity, manipulate the args array appropriately,
463423
// and return error if invalid
464-
if (_.isObject(args[0]) && _.isUndefined(args[1]) && _.isUndefined(args[2])) {
424+
if (typeof args[0] === 'object' && args[0] !== null && args[1] === undefined && args[2] === undefined) {
465425
// Do nothing
466-
} else if (_.isNull(args[0]) && _.isString(args[1]) && _.isString(args[2])) {
426+
} else if (args[0] === null && typeof args[1] === 'string' && typeof args[2] === 'string') {
467427
// Remove first argument (props) from args array
468428
args.shift();
469429
} else {
@@ -515,7 +475,7 @@ Connection.prototype.setSavepoint = function(name, callback) {
515475
callback = args.pop();
516476

517477
// Check arguments for validity, and return error if invalid
518-
if (! (_.isUndefined(args[0]) || _.isString(args[0]))) {
478+
if (! (args[0] === undefined || typeof args[0] === 'string')) {
519479
return callback(new Error("INVALID ARGUMENTS"));
520480
}
521481

0 commit comments

Comments
 (0)