Skip to content

Commit 44ce3e7

Browse files
committed
fix: review fixes
1 parent a758f8f commit 44ce3e7

3 files changed

Lines changed: 30 additions & 30 deletions

File tree

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export { default as useHoverDirty } from './useHoverDirty';
3535
export { default as useIdle } from './useIdle';
3636
export { default as useIntersection } from './useIntersection';
3737
export { default as useInterval } from './useInterval';
38+
export { default as useInterpolations } from './useInterpolations';
3839
export { default as useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect';
3940
export { default as useKey } from './useKey';
4041
export { default as createBreakpoint } from './factory/createBreakpoint';
@@ -95,7 +96,6 @@ export { default as useTimeout } from './useTimeout';
9596
export { default as useTimeoutFn } from './useTimeoutFn';
9697
export { default as useTitle } from './useTitle';
9798
export { default as useToggle } from './useToggle';
98-
export { default as useInterpolations } from './useInterpolations';
9999
export { default as useTween } from './useTween';
100100
export { default as useUnmount } from './useUnmount';
101101
export { default as useUnmountPromise } from './useUnmountPromise';

src/useInterpolations.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
import { useMemo } from "react";
2-
import useTween from "./useTween";
1+
import { useMemo } from 'react';
2+
import useTween from './useTween';
33

44
export type InterpolationMap = Record<string, readonly [number, number]>;
55

66
const formatMapEntryValue = (value: unknown): string => {
7+
// Best-effort stringify for dev error logs; avoids throwing on circular structures.
78
try {
89
const json = JSON.stringify(value);
9-
if (typeof json === "string") {
10-
return json;
11-
}
10+
return json;
1211
} catch {
1312
// ignore
1413
}
@@ -18,21 +17,23 @@ const formatMapEntryValue = (value: unknown): string => {
1817

1918
const useInterpolations = <T extends InterpolationMap>(
2019
map: T,
21-
easingName: string = "inCirc",
20+
easingName: string = 'inCirc',
2221
ms: number = 200,
2322
delay: number = 0
2423
): { [K in keyof T]: number } => {
2524
const t = useTween(easingName, ms, delay);
2625

2726
return useMemo(() => {
28-
if (process.env.NODE_ENV !== "production") {
29-
if (!map || typeof map !== "object") {
27+
if (process.env.NODE_ENV !== 'production') {
28+
if (!map || typeof map !== 'object') {
3029
console.error('useInterpolations() expected "map" to be an object.');
3130
return {} as { [K in keyof T]: number };
3231
}
32+
}
3333

34-
const keys = Object.keys(map) as Array<keyof T>;
34+
const keys = Object.keys(map) as Array<keyof T>;
3535

36+
if (process.env.NODE_ENV !== 'production') {
3637
for (const key of keys) {
3738
const value = map[key];
3839
const keyString = String(key);
@@ -43,7 +44,7 @@ const useInterpolations = <T extends InterpolationMap>(
4344
);
4445
return {} as { [K in keyof T]: number };
4546
}
46-
if (typeof value[0] !== "number" || typeof value[1] !== "number") {
47+
if (typeof value[0] !== 'number' || typeof value[1] !== 'number') {
4748
console.error(
4849
`useInterpolations() expected map["${keyString}"] to contain numbers, got [${typeof value[0]}, ${typeof value[1]}].`
4950
);
@@ -59,7 +60,6 @@ const useInterpolations = <T extends InterpolationMap>(
5960
}
6061

6162
const result = {} as { [K in keyof T]: number };
62-
const keys = Object.keys(map) as Array<keyof T>;
6363
for (const key of keys) {
6464
const [start, end] = map[key];
6565
result[key] = start + (end - start) * t;

tests/useInterpolations.test.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import { renderHook } from "@testing-library/react-hooks";
2-
import * as useTween from "../src/useTween";
3-
import useInterpolations from "../src/useInterpolations";
1+
import { renderHook } from '@testing-library/react-hooks';
2+
import * as useTween from '../src/useTween';
3+
import useInterpolations from '../src/useInterpolations';
44

55
let spyUseTween;
66

77
beforeEach(() => {
8-
spyUseTween = jest.spyOn(useTween, "default").mockReturnValue(0.5);
8+
spyUseTween = jest.spyOn(useTween, 'default').mockReturnValue(0.5);
99
});
1010

1111
afterEach(() => {
1212
jest.restoreAllMocks();
1313
});
1414

15-
it("should interpolate map values with default parameters", () => {
15+
it('should interpolate map values with default parameters', () => {
1616
const { result } = renderHook(() =>
1717
useInterpolations({
1818
left: [0, 100],
@@ -25,17 +25,17 @@ it("should interpolate map values with default parameters", () => {
2525
expect(result.current.top).toBe(100);
2626
expect(result.current.opacity).toBe(0.5);
2727
expect(spyUseTween).toHaveBeenCalledTimes(1);
28-
expect(spyUseTween).toHaveBeenCalledWith("inCirc", 200, 0);
28+
expect(spyUseTween).toHaveBeenCalledWith('inCirc', 200, 0);
2929
});
3030

31-
it("should interpolate map values with custom parameters", () => {
31+
it('should interpolate map values with custom parameters', () => {
3232
const { result } = renderHook(() =>
3333
useInterpolations(
3434
{
3535
x: [10, 20],
3636
y: [-5, 5],
3737
},
38-
"outCirc",
38+
'outCirc',
3939
500,
4040
100
4141
)
@@ -44,10 +44,10 @@ it("should interpolate map values with custom parameters", () => {
4444
expect(result.current.x).toBe(15);
4545
expect(result.current.y).toBe(0);
4646
expect(spyUseTween).toHaveBeenCalledTimes(1);
47-
expect(spyUseTween).toHaveBeenCalledWith("outCirc", 500, 100);
47+
expect(spyUseTween).toHaveBeenCalledWith('outCirc', 500, 100);
4848
});
4949

50-
it("should interpolate at t=0", () => {
50+
it('should interpolate at t=0', () => {
5151
spyUseTween.mockReturnValue(0);
5252

5353
const { result } = renderHook(() =>
@@ -61,7 +61,7 @@ it("should interpolate at t=0", () => {
6161
expect(result.current.top).toBe(20);
6262
});
6363

64-
it("should interpolate at t=1", () => {
64+
it('should interpolate at t=1', () => {
6565
spyUseTween.mockReturnValue(1);
6666

6767
const { result } = renderHook(() =>
@@ -75,12 +75,12 @@ it("should interpolate at t=1", () => {
7575
expect(result.current.top).toBe(80);
7676
});
7777

78-
describe("when invalid map is provided", () => {
78+
describe('when invalid map is provided', () => {
7979
beforeEach(() => {
80-
jest.spyOn(console, "error").mockImplementation(() => {});
80+
jest.spyOn(console, 'error').mockImplementation(() => {});
8181
});
8282

83-
it("should log an error when map is not an object", () => {
83+
it('should log an error when map is not an object', () => {
8484
const { result } = renderHook(() =>
8585
useInterpolations(null as unknown as Record<string, readonly [number, number]>)
8686
);
@@ -92,7 +92,7 @@ describe("when invalid map is provided", () => {
9292
);
9393
});
9494

95-
it("should log an error when map value is not a tuple", () => {
95+
it('should log an error when map value is not a tuple', () => {
9696
const { result } = renderHook(() =>
9797
useInterpolations({
9898
left: [10] as unknown as readonly [number, number],
@@ -106,10 +106,10 @@ describe("when invalid map is provided", () => {
106106
);
107107
});
108108

109-
it("should log an error when map value contains non-numbers", () => {
109+
it('should log an error when map value contains non-numbers', () => {
110110
const { result } = renderHook(() =>
111111
useInterpolations({
112-
left: ["0", 100] as unknown as readonly [number, number],
112+
left: ['0', 100] as unknown as readonly [number, number],
113113
})
114114
);
115115

@@ -120,7 +120,7 @@ describe("when invalid map is provided", () => {
120120
);
121121
});
122122

123-
it("should log an error when map value contains non-finite numbers", () => {
123+
it('should log an error when map value contains non-finite numbers', () => {
124124
const { result } = renderHook(() =>
125125
useInterpolations({
126126
left: [0, Infinity],

0 commit comments

Comments
 (0)