Skip to content

Commit

Permalink
fix: find min-max values for both features & values
Browse files Browse the repository at this point in the history
  • Loading branch information
AdriSolid committed Feb 1, 2021
1 parent b4520dc commit f917ff9
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/widgets/operations/aggregation/values.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
import { AggregationTypes } from '../../AggregationTypes';

const isFeature = (val) => typeof val === 'object';

This comment has been minimized.

Copy link
@VictorVelarde

VictorVelarde Feb 1, 2021

Contributor

isFeature doesn't check we have 'properties', and that is what it's being used, right?


const sum = (v, column) =>
v.reduce(
(a, b) => (typeof b === 'object' ? a + (b.properties[column] || 0) : a + b),
0
);
v.reduce((a, b) => (isFeature(b) ? a + (b.properties[column] || 0) : a + b), 0);

const findMinOrMaxValue = (type, val, column) => {

This comment has been minimized.

Copy link
@VictorVelarde

VictorVelarde Feb 1, 2021

Contributor

val is not a value, right? but a set of features, IMO we should better rename the variable. We can use 'values' if you want to avoid 'features'

const infinityWithSignus = type === AggregationTypes.MAX ? -Infinity : Infinity;

// features
if (isFeature(val[0]) && column) {
return Math[type](...val.map((v) => v.properties[column] || 0), infinityWithSignus);
}

// values
return val.reduce((a, b) => Math[type](a, b), infinityWithSignus);
};

export const aggregationFunctions = {
[AggregationTypes.COUNT]: (val) => val.length,
[AggregationTypes.MIN]: (val) => val.reduce((a, b) => Math.min(a, b), Infinity),
[AggregationTypes.MAX]: (val) => val.reduce((a, b) => Math.max(a, b), -Infinity),
[AggregationTypes.MIN]: (val, column) =>
findMinOrMaxValue(AggregationTypes.MIN, val, column),
[AggregationTypes.MAX]: (val, column) =>
findMinOrMaxValue(AggregationTypes.MAX, val, column),
[AggregationTypes.SUM]: (val, column) => sum(val, column),
[AggregationTypes.AVG]: (val, column) => sum(val, column) / val.length
};

0 comments on commit f917ff9

Please sign in to comment.