Remove getArgs dependency

This commit is contained in:
Timothy Warren 2016-11-14 21:22:29 -05:00
parent c90b1b1ba0
commit c034604c94
4 changed files with 46 additions and 61 deletions

View File

@ -1,6 +1,5 @@
'use strict';
const getArgs = require('getargs');
const helpers = require('./helpers');
const QueryParser = require('./QueryParser');
const State = require('./State');
@ -130,43 +129,38 @@ class QueryBuilderBase {
* @private
* @return {Array} - modified state array
*/
_mixedSet (/* $letName, $valType, $key, [$val] */) {
const argPattern = '$letName:string, $valType:string, $key:object|string|number, [$val]';
let args = getArgs(argPattern, arguments);
_mixedSet (letName, valType, key, val) {
let obj = {};
if (helpers.isScalar(args.$key) && !helpers.isUndefined(args.$val)) {
if (helpers.isScalar(key) && !helpers.isUndefined(val)) {
// Convert key/val pair to a simple object
obj[args.$key] = args.$val;
} else if (helpers.isScalar(args.$key) && helpers.isUndefined(args.$val)) {
obj[key] = val;
} else if (helpers.isScalar(key) && helpers.isUndefined(val)) {
// If just a string for the key, and no value, create a simple object with duplicate key/val
obj[args.$key] = args.$key;
obj[key] = key;
} else {
obj = args.$key;
obj = key;
}
Object.keys(obj).forEach(k => {
// If a single value for the return
if (['key', 'value'].indexOf(args.$valType) !== -1) {
let pushVal = (args.$valType === 'key') ? k : obj[k];
this.state[args.$letName].push(pushVal);
if (['key', 'value'].indexOf(valType) !== -1) {
let pushVal = (valType === 'key') ? k : obj[k];
this.state[letName].push(pushVal);
} else {
this.state[args.$letName][k] = obj[k];
this.state[letName][k] = obj[k];
}
});
return this.state[args.$letName];
return this.state[letName];
}
_whereMixedSet (/* key, val */) {
let args = getArgs('key:string|object, [val]', arguments);
_whereMixedSet (key, val) {
this.state.whereMap = [];
this.state.rawWhereValues = [];
this._mixedSet('whereMap', 'both', args.key, args.val);
this._mixedSet('rawWhereValues', 'value', args.key, args.val);
this._mixedSet('whereMap', 'both', key, val);
this._mixedSet('rawWhereValues', 'value', key, val);
}
_fixConjunction (conj) {
@ -227,21 +221,19 @@ class QueryBuilderBase {
this.state.whereMap = {};
}
_whereIn (/* key, val, inClause, conj */) {
let args = getArgs('key:string, val:array, inClause:string, conj:string', arguments);
args.key = this.driver.quoteIdentifiers(args.key);
let params = Array(args.val.length);
_whereIn (key, val, inClause, conj) {
key = this.driver.quoteIdentifiers(key);
let params = Array(val.length);
params.fill('?');
args.val.forEach(value => {
val.forEach(value => {
this.state.whereValues.push(value);
});
args.conj = (this.state.queryMap.length > 0) ? ` ${args.conj} ` : ' WHERE ';
let str = `${args.key} ${args.inClause} (${params.join(',')}) `;
conj = (this.state.queryMap.length > 0) ? ` ${conj} ` : ' WHERE ';
let str = `${key} ${inClause} (${params.join(',')}) `;
this._appendMap(args.conj, str, 'whereIn');
this._appendMap(conj, str, 'whereIn');
}
_run (type, table, sql, vals) {

View File

@ -2,7 +2,6 @@
const Adapter = require('../Adapter');
const Result = require('../Result');
const getArgs = require('getargs');
const helpers = require('../helpers');
const pg = require('pg');
const url = require('url');

View File

@ -70,6 +70,6 @@
"lint": "npm-run-all lint:tests lint:src && happy",
"lint:src": "eslint ./lib",
"lint:tests": "eslint ./test",
"test": "mocha -R dot"
"test": "mocha -R spec"
}
}

View File

@ -3,7 +3,6 @@
const expect = require('chai').expect;
// Use the base driver as a mock for testing
const getArgs = require('getargs');
const helpers = require('../lib/helpers');
const driver = require('../lib/Driver');
@ -15,43 +14,38 @@ const State = require('../lib/State');
// Simulate query builder state
let state = new State();
let mixedSet = function mixedSet (/* $letName, $valType, $key, [$val] */) {
const argPattern = '$letName:string, $valType:string, $key:object|string|number, [$val]';
let args = getArgs(argPattern, arguments);
let mixedSet = function mixedSet(letName, valType, key, val) {
let obj = {};
if (helpers.isScalar(args.$key) && !helpers.isUndefined(args.$val)) {
if (helpers.isScalar(key) && !helpers.isUndefined(val)) {
// Convert key/val pair to a simple object
obj[args.$key] = args.$val;
} else if (helpers.isScalar(args.$key) && helpers.isUndefined(args.$val)) {
obj[key] = val;
} else if (helpers.isScalar(key) && helpers.isUndefined(val)) {
// If just a string for the key, and no value, create a simple object with duplicate key/val
obj[args.$key] = args.$key;
obj[key] = key;
} else {
obj = args.$key;
obj = key;
}
Object.keys(obj).forEach(k => {
// If a single value for the return
if (['key', 'value'].indexOf(args.$valType) !== -1) {
let pushVal = (args.$valType === 'key') ? k : obj[k];
state[args.$letName].push(pushVal);
if (['key', 'value'].indexOf(valType) !== -1) {
let pushVal = (valType === 'key') ? k : obj[k];
state[letName].push(pushVal);
} else {
state[args.$letName][k] = obj[k];
state[letName][k] = obj[k];
}
});
return state[args.$letName];
return state[letName];
};
let whereMock = function () {
let args = getArgs('key:string|object, [val]', arguments);
let whereMock = function (key, val) {
state.whereMap = [];
state.whereValues = [];
mixedSet('rawWhereValues', 'value', args.key, args.val);
mixedSet('whereMap', 'both', args.key, args.val);
mixedSet('rawWhereValues', 'value', key, val);
mixedSet('whereMap', 'both', key, val);
};
// -----------------------------------------------------------------------------