Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Respecting max/min opacities, and adding tests. #20555

Merged
merged 9 commits into from
Jun 30, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,24 @@ export const getOpacity = (
extremeValue: number,
minOpacity = MIN_OPACITY_BOUNDED,
maxOpacity = MAX_OPACITY,
) =>
extremeValue === cutoffPoint
? maxOpacity
: round(
Math.abs(
((maxOpacity - minOpacity) / (extremeValue - cutoffPoint)) *
(value - cutoffPoint),
) + minOpacity,
2,
);
) => {
if (extremeValue === cutoffPoint) {
return maxOpacity;
}
const result = round(
Math.abs(
((maxOpacity - minOpacity) / (extremeValue - cutoffPoint)) *
(value - cutoffPoint),
) + minOpacity,
2,
);

if (result < minOpacity) {
return minOpacity;
}

return result > maxOpacity ? maxOpacity : result;
};

export const getColorFunction = (
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ describe('getOpacity', () => {
expect(getOpacity(100, 100, 50)).toEqual(0.05);
expect(getOpacity(100, 100, 100, 0, 0.8)).toEqual(0.8);
expect(getOpacity(100, 100, 50, 0, 1)).toEqual(0);
expect(getOpacity(999, 100, 50, 0, 1)).toEqual(1);
expect(getOpacity(-999, 100, 50, 0, 1)).toEqual(0.05);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Math.abs inside the function will make sure result is never negative. We can replace this test with one that passes a high minOpacity like 0.9 and parameters that make result be less than this value to test result < minOpacity

Suggested change
expect(getOpacity(-999, 100, 50, 0, 1)).toEqual(0.05);

});
});

Expand Down