Skip to content

Commit

Permalink
Refactor parseData to handle generic source type
Browse files Browse the repository at this point in the history
  • Loading branch information
guidomodarelli committed Sep 11, 2024
1 parent f522c87 commit 4162918
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { parseData } from './data-grid-service';
import { SearchResponse } from '../../../../../../src/core/server';

describe('describe-grid-test', () => {
describe('parseData', () => {
it('should parse data extract source fields correctly', () => {
const resultsHits: SearchResponse['hits']['hits'] = [
{
_id: 'id-1',
_index: 'index-1',
_type: 'type-1',
_score: 1,
_source: {
test: true,
},
},
];

const expectedResult = [
{
_id: 'id-1',
_index: 'index-1',
_type: 'type-1',
_score: 1,
test: true,
},
];

expect(parseData(resultsHits)).toEqual(expectedResult);
});

it('should parse data handle invalid hits', () => {
const resultsHits: SearchResponse['hits']['hits'] = [
// @ts-expect-error
undefined,
// @ts-expect-error
null,
// @ts-expect-error
0,
];

const expectedResult = [{}, {}, {}];

expect(parseData(resultsHits)).toEqual(expectedResult);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@ import { tDataGridColumn } from './use-data-grid';
import { cellFilterActions } from './cell-filter-actions';
import { FILTER_OPERATOR, PatternDataSourceFilterManager } from '../data-source';

export const parseData = (resultsHits: SearchResponse['hits']['hits']): any[] => {
type ParseData<T> = {
source: T,
_id: string,
_index: string,
_type: string,
_score: number,
} | {}

export const parseData = <T = unknown>(resultsHits: SearchResponse<T>['hits']['hits']): ParseData<T>[] => {
const data = resultsHits.map((hit) => {
if (!hit) {
return {};
}
const source = hit._source as object;
const source = hit._source as T;
const data = {
...source,
_id: hit._id,
Expand Down

0 comments on commit 4162918

Please sign in to comment.