Skip to content

Commit

Permalink
Moved findAccessor out dimensions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuznietsov committed Feb 11, 2022
1 parent 81c04a8 commit 724c0fc
Showing 1 changed file with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,23 @@ const getAccessorByIndex = (accessor: number, columns: Datatable['columns']) =>
const getAccessorById = (accessor: DatatableColumn['id'], columns: Datatable['columns']) =>
columns.find((c) => c.id === accessor);

export const findAccessor = (accessor: string | number, columns: DatatableColumn[]) => {
const foundAccessor =
typeof accessor === 'number'
? getAccessorByIndex(accessor, columns)
: getAccessorById(accessor, columns);

if (foundAccessor === undefined) {
throw new Error(
i18n.translate('visualizations.function.visDimension.error.accessor', {
defaultMessage: 'Column name or index provided is invalid',
})
);
}

return foundAccessor;
};

export const visDimension = (): ExpressionFunctionDefinition<
'visdimension',
Datatable,
Expand Down Expand Up @@ -73,19 +90,7 @@ export const visDimension = (): ExpressionFunctionDefinition<
},
},
fn: (input, args) => {
const accessor =
typeof args.accessor === 'number'
? getAccessorByIndex(args.accessor, input.columns)
: getAccessorById(args.accessor, input.columns);

if (accessor === undefined) {
throw new Error(
i18n.translate('visualizations.function.visDimension.error.accessor', {
defaultMessage: 'Column name or index provided is invalid',
})
);
}

const accessor = findAccessor(args.accessor, input.columns);
return {
type: 'vis_dimension',
accessor,
Expand Down

0 comments on commit 724c0fc

Please sign in to comment.