Skip to content

Commit

Permalink
Update GroupingValues type to use Map objects
Browse files Browse the repository at this point in the history
Resolves type error when using a plain object to keep counts of
grouping values:

```
src/actions/measurements.ts(292,56): error TS2538: Type 'false' cannot be used as an index type.
```
  • Loading branch information
joverlee521 committed Dec 7, 2024
1 parent 78e4761 commit adec1ae
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/actions/measurements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ import {
* we can create a stable default order for grouping field values
*/
interface GroupingValues {
[key: string]: {
[key: string]: number
}
[key: string]: Map<MeasurementMetadata, number>
}


Expand Down Expand Up @@ -261,7 +259,7 @@ const parseMeasurementsJSON = (json: MeasurementsJson): MeasurementsState => {
// Create a temp object for groupings to keep track of values and their
// counts so that we can create a stable default order for grouping field values
const groupingsValues: GroupingValues = jsonCollection.groupings.reduce((tempObject, {key}) => {
tempObject[key] = {};
tempObject[key] = new Map();
return tempObject;
}, {});

Expand All @@ -282,8 +280,8 @@ const parseMeasurementsJSON = (json: MeasurementsJson): MeasurementsState => {

// Save grouping field values and counts
if (field in groupingsValues) {
const previousValue = groupingsValues[field][fieldValue];
groupingsValues[field][fieldValue] = previousValue ? previousValue + 1 : 1;
const previousValue = groupingsValues[field].get(fieldValue);
groupingsValues[field].set(fieldValue, previousValue ? previousValue + 1 : 1);
}
});

Expand All @@ -298,7 +296,7 @@ const parseMeasurementsJSON = (json: MeasurementsJson): MeasurementsState => {
// Must be done after looping through measurements to build `groupingsValues` object
collection.groupings = new Map(
jsonCollection.groupings.map(({key, order}) => {
const valuesByCount = Object.entries(groupingsValues[key])
const valuesByCount = [...groupingsValues[key].entries()]
// Use the grouping values' counts to sort the values, highest count first
.sort(([, valueCountA], [, valueCountB]) => valueCountB - valueCountA)
// Filter out values that already exist in provided order from JSON
Expand Down

0 comments on commit adec1ae

Please sign in to comment.