Skip to content

Commit

Permalink
Moved getNextToAccessorColumn to accessor.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuznietsov committed Sep 24, 2021
1 parent 6ab5eca commit bf1197c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,26 @@

import { ExpressionValueVisDimension } from '../../../../../../visualizations/public';

const hasNextValue = (index: number, arrLength: number) => index < arrLength - 1;

const getNextValue = <T>(index: number, arr: T[]) =>
hasNextValue(index, arr.length) ? arr[index + 1] : undefined;

interface BaseColumn {
id: string | number;
}

export const getNextToAccessorColumn = <T extends BaseColumn>(
columns: T[],
accessor: ExpressionValueVisDimension['accessor']
) => {
if (typeof accessor === 'number') {
return getNextValue(accessor, columns);
}
const colIndex = columns.findIndex((column) => column.id === accessor.id);
return getNextValue(colIndex, columns);
};

export const getValueByAccessor = <T extends Record<string, any>>(
data: T,
accessor: ExpressionValueVisDimension['accessor'] | string
Expand All @@ -19,7 +39,7 @@ export const getValueByAccessor = <T extends Record<string, any>>(
return typeof accessor === 'string' ? data[accessor] : data[accessor.id];
};

export const getColumnByAccessor = <T extends { id: string | number }>(
export const getColumnByAccessor = <T extends BaseColumn>(
columns: T[],
accessor: ExpressionValueVisDimension['accessor'] | null
) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
*/

import { toArray } from 'lodash';
import { ExpressionValueVisDimension } from '../../../../../../visualizations/common';
import { getFormatService } from '../../../services';
import { Table } from '../../types';
import type { Dimensions } from '../../../../../pie/public';
import { getColumnByAccessor } from '../accessor';
import { getNextToAccessorColumn } from '../accessor/accessor';

interface Slice {
name: string;
Expand All @@ -26,17 +26,6 @@ interface Slice {
};
}

const getNextToAccessorColumn = (
columns: Table['columns'],
accessor: ExpressionValueVisDimension['accessor']
) => {
if (typeof accessor === 'number') {
return columns[accessor + 1];
}
const colIndex = columns.findIndex((column) => column.id === accessor.id);
return columns[colIndex + 1];
};

export const buildHierarchicalData = (table: Table, { metric, buckets = [] }: Dimensions) => {
let slices: Slice[];
const names: { [key: string]: string } = {};
Expand All @@ -62,7 +51,7 @@ export const buildHierarchicalData = (table: Table, { metric, buckets = [] }: Di
const bucketValueColumn = getNextToAccessorColumn(table.columns, bucket.accessor);
const bucketFormatter = getFormatService().deserialize(bucket.format);
const name = bucketFormatter.convert(row[bucketColumn.id]);
const size = row[bucketValueColumn.id] as number;
const size = bucketValueColumn ? (row[bucketValueColumn.id] as number) : 0;
names[name] = name;

let slice = dataLevel.find((dataLevelSlice) => dataLevelSlice.name === name);
Expand Down

0 comments on commit bf1197c

Please sign in to comment.