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

View File

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

View File

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

View File

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