Skip to content

Commit

Permalink
feat: Add mapping by value in sorters
Browse files Browse the repository at this point in the history
  • Loading branch information
bprusinowski committed Apr 11, 2023
1 parent 84fc15a commit 60cdbc3
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions app/utils/sorting-values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ export const makeDimensionValueSorters = (
const filterValues = dimensionFilter.values;
values = values.filter((dv) => filterValues[dv.value]);
}

const valuesByValue = uniqueMapBy(
values.filter((x) => x.identifier || x.position),
(dv) => dv.value
);
// Index values that have an identifier or a position
// Warning: if two values have the same label and have an identifier / position
// there could be problems as we could select the "wrong" value for the order
Expand All @@ -79,21 +84,32 @@ export const makeDimensionValueSorters = (
(dv) => dv.label
);

const getLabel = (label?: string) => label;
const getIdentifier = (label?: string) => {
const identifier = label
? maybeInt(valuesByLabel.get(label)?.identifier) ?? Infinity
const getByValueOrLabel = (valueOrLabel: string) => {
return valuesByValue.get(valueOrLabel) ?? valuesByLabel.get(valueOrLabel);
};

const getLabel = (valueOrLabel?: string) => {
const label = valueOrLabel ? getByValueOrLabel(valueOrLabel)?.label : "";
return label;
};
const getIdentifier = (valueOrLabel?: string) => {
const identifier = valueOrLabel
? maybeInt(getByValueOrLabel(valueOrLabel)?.identifier) ?? Infinity
: Infinity;
return identifier;
};
const getPosition = (label?: string) =>
label ? valuesByLabel.get(label)?.position ?? Infinity : Infinity;
const getPosition = (valueOrLabel?: string) => {
const position = valueOrLabel
? getByValueOrLabel(valueOrLabel)?.position ?? Infinity
: Infinity;
return position;
};

const getSum = (label?: string) =>
label ? sumsBySegment?.[label] ?? Infinity : Infinity;
const getSum = (valueOrLabel?: string) =>
valueOrLabel ? sumsBySegment?.[valueOrLabel] ?? Infinity : Infinity;

const getMeasure = (label?: string) =>
label ? measureBySegment?.[label] ?? Infinity : Infinity;
const getMeasure = (valueOrLabel?: string) =>
valueOrLabel ? measureBySegment?.[valueOrLabel] ?? Infinity : Infinity;

let sorters: ((dv: string) => string | undefined | number)[] = [];

Expand Down

0 comments on commit 60cdbc3

Please sign in to comment.