Skip to content

Commit

Permalink
fix(🐛): getXforY() now returns null if x is out of bound (#431)
Browse files Browse the repository at this point in the history
  • Loading branch information
wcandillon authored Apr 5, 2021
1 parent 655f422 commit 0faf0c6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/Paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ const curveIsFound = (c: NullableSelectedCurve): c is SelectedCurve => {
* @summary Return the curves at x. This function assumes that only one curve is available at x
* @worklet
*/
export const selectCurve = (path: Path, x: number): SelectedCurve => {
export const selectCurve = (path: Path, x: number): SelectedCurve | null => {
"worklet";
const result: NullableSelectedCurve = {
from: path.move,
Expand All @@ -292,7 +292,7 @@ export const selectCurve = (path: Path, x: number): SelectedCurve => {
result.from = c.to;
}
if (!curveIsFound(result)) {
throw new Error(`No curve found at ${x}`);
return null;
}
return result;
};
Expand All @@ -312,6 +312,9 @@ export const selectCurve = (path: Path, x: number): SelectedCurve => {
export const getYForX = (path: Path, x: number, precision = 2) => {
"worklet";
const c = selectCurve(path, x);
if (c === null) {
return null;
}
return cubicBezierYForX(
x,
c.from,
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/Paths.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ test("getYForX()", () => {
);
expect(getYForX(p1, 200)).toBe(75);
expect(getYForX(p1, 50)).toBe(151.160325);
expect(() => getYForX(p1, 750)).toThrow();
expect(getYForX(p1, 750)).toBe(null);
});

test("getYForX2()", () => {
Expand Down

0 comments on commit 0faf0c6

Please sign in to comment.