-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: VictorVelarde <victor.velarde@gmail.com>
- Loading branch information
1 parent
b4520dc
commit f653ce9
Showing
5 changed files
with
59 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { aggregationFunctions } from 'src/widgets/operations/aggregation/values'; | ||
import { AggregationTypes } from 'src/widgets/AggregationTypes'; | ||
|
||
const VALUES = [1, 2, 3, 4, 5]; | ||
|
||
describe('Aggregation functions', () => { | ||
test(AggregationTypes.COUNT, () => { | ||
const func = aggregationFunctions[AggregationTypes.COUNT]; | ||
expect(func(VALUES)).toEqual(5); | ||
}); | ||
|
||
test(AggregationTypes.AVG, () => { | ||
const func = aggregationFunctions[AggregationTypes.AVG]; | ||
expect(func(VALUES)).toEqual(3); | ||
}); | ||
|
||
test(AggregationTypes.MIN, () => { | ||
const func = aggregationFunctions[AggregationTypes.MIN]; | ||
expect(func(VALUES)).toEqual(1); | ||
}); | ||
|
||
test(AggregationTypes.MAX, () => { | ||
const func = aggregationFunctions[AggregationTypes.MAX]; | ||
expect(func(VALUES)).toEqual(5); | ||
}); | ||
|
||
test(AggregationTypes.SUM, () => { | ||
const func = aggregationFunctions[AggregationTypes.SUM]; | ||
expect(func(VALUES)).toEqual(15); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
function isFeature(value) { | ||
return typeof value === 'object' && 'properties' in value; | ||
} | ||
|
||
function getValueFromFeature(feature, column) { | ||
if (!isFeature(feature)) { | ||
return 0; | ||
} | ||
|
||
const value = feature.properties[column]; | ||
const isValidValue = Number.isFinite(value); | ||
return isValidValue ? value : 0; | ||
} | ||
|
||
export function pickValuesFromFeatures(features, column) { | ||
return features.map((feat) => getValueFromFeature(feat, column)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,11 @@ | ||
import { AggregationTypes } from '../../AggregationTypes'; | ||
|
||
const sum = (v, column) => | ||
v.reduce( | ||
(a, b) => (typeof b === 'object' ? a + (b.properties[column] || 0) : a + b), | ||
0 | ||
); | ||
const sum = (values) => values.reduce((a, b) => a + b, 0); | ||
|
||
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.SUM]: (val, column) => sum(val, column), | ||
[AggregationTypes.AVG]: (val, column) => sum(val, column) / val.length | ||
[AggregationTypes.COUNT]: (values) => values.length, | ||
[AggregationTypes.MIN]: (values) => values.reduce((a, b) => Math.min(a, b), Infinity), | ||
[AggregationTypes.MAX]: (values) => values.reduce((a, b) => Math.max(a, b), -Infinity), | ||
[AggregationTypes.SUM]: (values) => sum(values), | ||
[AggregationTypes.AVG]: (values) => sum(values) / values.length | ||
}; |