Skip to content

Commit

Permalink
RN-702: Re-instated the behaviour where 'exclude' merge strategy remo…
Browse files Browse the repository at this point in the history
…ves the column from the table
  • Loading branch information
rohan-bes committed Oct 28, 2022
1 parent 51cb551 commit a250dd8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,11 @@ describe('mergeRows', () => {
},
]);
expect(transform(TransformTable.fromRows(MERGEABLE_ANALYTICS))).toStrictEqual(
TransformTable.fromRows(
[{ period: '20200101' }, { period: '20200102' }, { period: '20200103' }],
['period', 'organisationUnit', 'BCD1', 'BCD2'], // excludes values, but keeps columns
),
TransformTable.fromRows([
{ period: '20200101' },
{ period: '20200102' },
{ period: '20200103' },
]),
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,32 @@ const addRowToGroup = (groupsByKey: Record<string, Group>, groupKey: string, row
};

const mergeGroups = (groups: Group[], params: MergeRowsParams) => {
return groups.map(group => {
const excludedColumns = new Set<string>();
const mergedRows = groups.map(group => {
const mergedRow: Row = {};
Object.entries(group).forEach(([columnName, groupValues]) => {
const mergeStrategy = params.getMergeStrategy(columnName);

// We track the excluded columns so that we can remove them from the TransformTable later
if (mergeStrategy === 'exclude') {
excludedColumns.add(columnName);
}

const mergedValue = mergeStrategies[mergeStrategy](groupValues);
if (mergedValue !== undefined) {
mergedRow[columnName] = mergedValue;
}
});
return mergedRow;
});
return { mergedRows, excludedColumns: Array.from(excludedColumns) };
};

const mergeRows = (table: TransformTable, params: MergeRowsParams) => {
const groups = groupRows(table, params);
const mergedRows = mergeGroups(groups, params);
return new TransformTable(table.getColumns(), mergedRows);
const { mergedRows, excludedColumns } = mergeGroups(groups, params);
const columns = table.getColumns().filter(columnName => !excludedColumns.includes(columnName));
return new TransformTable(columns, mergedRows);
};

const buildParams = (params: unknown): MergeRowsParams => {
Expand Down

0 comments on commit a250dd8

Please sign in to comment.