Skip to content

Commit

Permalink
code simplifications
Browse files Browse the repository at this point in the history
  • Loading branch information
mbondyra committed Nov 3, 2020
1 parent 910b2be commit c08bd88
Showing 1 changed file with 7 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,22 +133,20 @@ export const movingAverage: ExpressionFunctionMovingAverage = {
const bucketIdentifier = getBucketIdentifier(row, by);
const lastNValues = lastNValuesByBucket[bucketIdentifier];
const currentValue = newRow[inputColumnId];
if (lastNValues != null && lastNValues.length && currentValue != null) {
if (lastNValues != null && currentValue != null) {
const sum = lastNValues.reduce((acc, current) => acc + current, 0);
newRow[outputColumnId] = sum / lastNValues.length;
} else {
newRow[outputColumnId] = undefined;
}

if (currentValue != null && lastNValues != null) {
lastNValuesByBucket[bucketIdentifier] = [...lastNValues, Number(currentValue)].slice(
-window
);
} else if (currentValue != null) {
lastNValuesByBucket[bucketIdentifier] = [Number(currentValue)];
} else if (currentValue == null && lastNValues == null) {
lastNValuesByBucket[bucketIdentifier] = undefined;
if (currentValue != null) {
lastNValuesByBucket[bucketIdentifier] = [
...(lastNValues || []),
Number(currentValue),
].slice(-window);
}

return newRow;
}),
};
Expand Down

0 comments on commit c08bd88

Please sign in to comment.