From 8ed73fa1758bfc8c0b1285ab84d953da62db44be Mon Sep 17 00:00:00 2001 From: ppisljar Date: Tue, 20 Oct 2020 23:36:36 -0700 Subject: [PATCH] applying review feedback --- .../__snapshots__/tabify_docs.test.ts.snap | 8 ++++---- src/plugins/data/common/search/tabify/index.ts | 18 ++++++++++++++---- .../common/search/tabify/tabify_docs.test.ts | 8 ++++---- .../data/common/search/tabify/tabify_docs.ts | 17 ++++++++--------- 4 files changed, 30 insertions(+), 21 deletions(-) diff --git a/src/plugins/data/common/search/tabify/__snapshots__/tabify_docs.test.ts.snap b/src/plugins/data/common/search/tabify/__snapshots__/tabify_docs.test.ts.snap index fecf8b9a9792ef..d5ddaa31b8ac3b 100644 --- a/src/plugins/data/common/search/tabify/__snapshots__/tabify_docs.test.ts.snap +++ b/src/plugins/data/common/search/tabify/__snapshots__/tabify_docs.test.ts.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`tabifyDocs converts fields by default: converts fields by default 1`] = ` +exports[`tabifyDocs converts fields by default 1`] = ` Object { "columns": Array [ Object { @@ -49,7 +49,7 @@ Object { } `; -exports[`tabifyDocs converts source if option is set: converts source if option is set 1`] = ` +exports[`tabifyDocs converts source if option is set 1`] = ` Object { "columns": Array [ Object { @@ -74,7 +74,7 @@ Object { } `; -exports[`tabifyDocs skips nested fields if option is set: skips nested fields if option is set 1`] = ` +exports[`tabifyDocs skips nested fields if option is set 1`] = ` Object { "columns": Array [ Object { @@ -125,7 +125,7 @@ Object { } `; -exports[`tabifyDocs works without provided index pattern: works without provided index pattern 1`] = ` +exports[`tabifyDocs works without provided index pattern 1`] = ` Object { "columns": Array [ Object { diff --git a/src/plugins/data/common/search/tabify/index.ts b/src/plugins/data/common/search/tabify/index.ts index f7f69769d9c4f5..9e6657f5e8d83e 100644 --- a/src/plugins/data/common/search/tabify/index.ts +++ b/src/plugins/data/common/search/tabify/index.ts @@ -17,14 +17,24 @@ * under the License. */ +import { SearchResponse } from 'elasticsearch'; import { SearchSource } from '../search_source'; import { tabifyAggResponse } from './tabify'; -import { tabifyDocs } from './tabify_docs'; +import { tabifyDocs, TabifyDocsOptions } from './tabify_docs'; +import { TabbedResponseWriterOptions } from './types'; -export const tabify = (searchSource: SearchSource, esResponse: any, opts: any) => { +export const tabify = ( + searchSource: SearchSource, + esResponse: SearchResponse, + opts: Partial | TabifyDocsOptions +) => { return !esResponse.aggregations - ? tabifyDocs(esResponse, searchSource.getField('index'), opts) - : tabifyAggResponse(searchSource.getField('aggs'), esResponse, opts); + ? tabifyDocs(esResponse, searchSource.getField('index'), opts as TabifyDocsOptions) + : tabifyAggResponse( + searchSource.getField('aggs'), + esResponse, + opts as Partial + ); }; export { tabifyAggResponse } from './tabify'; diff --git a/src/plugins/data/common/search/tabify/tabify_docs.test.ts b/src/plugins/data/common/search/tabify/tabify_docs.test.ts index 6eb65510228002..a1218928561c66 100644 --- a/src/plugins/data/common/search/tabify/tabify_docs.test.ts +++ b/src/plugins/data/common/search/tabify/tabify_docs.test.ts @@ -57,21 +57,21 @@ describe('tabifyDocs', () => { it('converts fields by default', () => { const table = tabifyDocs(response, index); - expect(table).toMatchSnapshot('converts fields by default'); + expect(table).toMatchSnapshot(); }); it('converts source if option is set', () => { const table = tabifyDocs(response, index, { source: true }); - expect(table).toMatchSnapshot('converts source if option is set'); + expect(table).toMatchSnapshot(); }); it('skips nested fields if option is set', () => { const table = tabifyDocs(response, index, { shallow: true }); - expect(table).toMatchSnapshot('skips nested fields if option is set'); + expect(table).toMatchSnapshot(); }); it('works without provided index pattern', () => { const table = tabifyDocs(response); - expect(table).toMatchSnapshot('works without provided index pattern'); + expect(table).toMatchSnapshot(); }); }); diff --git a/src/plugins/data/common/search/tabify/tabify_docs.ts b/src/plugins/data/common/search/tabify/tabify_docs.ts index 123e3fcbff879f..78ebee9e657172 100644 --- a/src/plugins/data/common/search/tabify/tabify_docs.ts +++ b/src/plugins/data/common/search/tabify/tabify_docs.ts @@ -18,9 +18,9 @@ */ import { SearchResponse } from 'elasticsearch'; -import { first, each, isPlainObject } from 'lodash'; +import { isPlainObject } from 'lodash'; import { IndexPattern } from '../../index_patterns/index_patterns'; -import { Datatable, DatatableColumn } from '../../../../expressions/common'; +import { Datatable, DatatableColumn, DatatableColumnType } from '../../../../expressions/common'; export function flattenHit( hit: Record, @@ -37,12 +37,11 @@ export function flattenHit( if (!shallow) { const isNestedField = field?.type === 'nested'; - const isArrayOfObjects = Array.isArray(val) && isPlainObject(first(val)); - if (isArrayOfObjects && !isNestedField) { - each(val as object, (v) => flatten(v, key + '.')); + if (Array.isArray(val) && !isNestedField) { + val.forEach((v) => isPlainObject(v) && flatten(v, key + '.')); continue; } - } else if (flat[key] !== void 0) { + } else if (flat[key] !== undefined) { continue; } @@ -86,9 +85,9 @@ export const tabifyDocs = ( const flat = flattenHit(toConvert, index, params.shallow); for (const [key, value] of Object.entries(flat)) { const field = index?.fields.getByName(key); - if (!columns.find((c) => c.id === (field?.name || key))) { - const fieldName = field?.name || key; - const fieldType = (field?.type as any) || typeof value; + const fieldName = field?.name || key; + if (!columns.find((c) => c.id === fieldName)) { + const fieldType = (field?.type as DatatableColumnType) || typeof value; const formatter = field && index?.getFormatterForField(field); columns.push({ id: fieldName,