From 45a0954b6f85b6bf9bbf08c7ca20e72378bd97c8 Mon Sep 17 00:00:00 2001 From: Jeremy Daly Date: Thu, 12 Dec 2019 11:40:32 -0500 Subject: [PATCH] fix #30 by conditionally collapsing arrays --- index.js | 21 +++++---------------- index.test.js | 11 +++++------ 2 files changed, 10 insertions(+), 22 deletions(-) diff --git a/index.js b/index.js index ec2849d..695b5ff 100644 --- a/index.js +++ b/index.js @@ -102,8 +102,8 @@ const pick = (obj,values) => Object.keys(obj).reduce((acc,x) => values.includes(x) ? Object.assign(acc,{ [x]: obj[x] }) : acc ,{}) -// Utility function for flattening arrays - deprecated -// const flatten = arr => arr.reduce((acc,x) => acc.concat(x),[]) +// Utility function for flattening arrays +const flatten = arr => arr.reduce((acc,x) => acc.concat(x),[]) // Normize parameters so that they are all in standard format const normalizeParams = params => params.reduce((acc,p) => @@ -112,15 +112,6 @@ const normalizeParams = params => params.reduce((acc,p) => : acc.concat(splitParams(p)) ,[]) // end reduce -// // Annotate parameters with correct types -// const annotateParams = params => params.reduce((acc,p) => -// Array.isArray(p) ? acc.concat([annotateParams(p)]) -// : Object.keys(p).length === 2 && p.name && p.value ? acc.concat(p) -// : acc.concat( -// formatParam(Object.keys(p)[0],Object.values(p)[0]) -// ) -// ,[]) // end reduce - // Prepare parameters const processParams = (sql,sqlParams,params,row=0) => { @@ -294,10 +285,10 @@ const mergeConfig = (initialConfig,args) => /********************************************************************/ // Query function (use standard form for `this` context) -const query = async function(config,...args) { +const query = async function(config,..._args) { - // Deprecated this since it was collapsing batches - // const args = flatten(_args) + // Flatten array if nested arrays (fixes #30) + const args = Array.isArray(_args[0]) ? flatten(_args) : _args // Parse and process sql const sql = parseSQL(args) @@ -339,8 +330,6 @@ const query = async function(config,...args) { let result = await (isBatch ? config.RDS.batchExecuteStatement(params).promise() : config.RDS.executeStatement(params).promise()) - // FOR DEBUGGING: console.log(JSON.stringify(result,null,2)) - // Format and return the results return formatResults( result, diff --git a/index.test.js b/index.test.js index 54bc9d9..0cd4df1 100644 --- a/index.test.js +++ b/index.test.js @@ -31,12 +31,11 @@ describe('utility', () => { expect(result).toEqual({ a: 1, c: 3 }) }) - // Deprecated - // test('flatten', async () => { - // const flatten = dataApiClient.__get__('flatten') - // let result = flatten([[1,2,3],4,[5,6],7,8]) - // expect(result).toEqual([1,2,3,4,5,6,7,8]) - // }) + test('flatten', async () => { + const flatten = dataApiClient.__get__('flatten') + let result = flatten([[1,2,3],4,[5,6],7,8]) + expect(result).toEqual([1,2,3,4,5,6,7,8]) + }) }) // end utility