Skip to content

Commit b661b64

Browse files
committed
Move interpolation types to a separate file
1 parent 001fcda commit b661b64

File tree

13 files changed

+297
-203
lines changed

13 files changed

+297
-203
lines changed

src/animated/AnimatedArray.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import { InterpolationConfig, Interpolator } from '../types/interpolation'
12
import { AnimatedArrayWithChildren } from './Animated'
23
import AnimatedInterpolation from './AnimatedInterpolation'
34
import AnimatedValue from './AnimatedValue'
4-
import { InterpolationConfig } from './createInterpolation'
55

66
export default class AnimatedArray extends AnimatedArrayWithChildren {
77
payload: AnimatedValue[]
@@ -21,6 +21,10 @@ export default class AnimatedArray extends AnimatedArrayWithChildren {
2121

2222
getValue = () => this.payload.map(v => v.getValue())
2323

24-
interpolate = (config: InterpolationConfig, arg: any) =>
25-
new AnimatedInterpolation(this, config, arg)
24+
interpolate<In extends string | number, Out extends string | number>(
25+
range: number[] | InterpolationConfig<Out> | Interpolator<In, Out>,
26+
output?: Out[]
27+
) {
28+
return new AnimatedInterpolation(this, range as number[], output!)
29+
}
2630
}
Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,57 @@
1+
import {
2+
AnimatedValueFromInterpolation,
3+
InterpolationConfig,
4+
Interpolator,
5+
} from '../types/interpolation'
16
import Animated, { AnimatedArrayWithChildren } from './Animated'
2-
import createInterpolation, { InterpolationConfig } from './createInterpolation'
7+
import createInterpolator from './createInterpolator'
38

4-
export default class AnimatedInterpolation extends AnimatedArrayWithChildren {
5-
calc: any
9+
export default class AnimatedInterpolation<
10+
Value extends number | string = number | string
11+
> extends AnimatedArrayWithChildren {
12+
calc: Interpolator<number, Value>
613

714
constructor(
815
parents: Animated | Animated[],
9-
config: InterpolationConfig,
10-
arg: any
16+
range: number[] | InterpolationConfig<Value> | Interpolator<Value>,
17+
output?: Value[]
1118
) {
1219
super()
1320
this.payload =
1421
// AnimatedArrays should unfold, except AnimatedInterpolation which is taken as is
1522
parents instanceof AnimatedArrayWithChildren &&
16-
!(parents as AnimatedInterpolation).updateConfig
23+
!('updateConfig' in parents)
1724
? parents.payload
1825
: Array.isArray(parents)
1926
? parents
2027
: [parents]
21-
this.calc = createInterpolation(config as any, arg)
28+
this.calc = createInterpolator(range as number[], output!)
2229
}
2330

24-
getValue = () => this.calc(...this.payload.map(value => value.getValue()))
31+
getValue = (): Value =>
32+
(this.calc as any)(...this.payload.map(value => value.getValue()))
2533

26-
updateConfig = (config: InterpolationConfig, arg: any) => {
27-
this.calc = createInterpolation(config as any, arg)
34+
updateConfig(
35+
range: number[] | InterpolationConfig<Value> | Interpolator<Value>,
36+
output?: Value[]
37+
) {
38+
this.calc = createInterpolator<Value>(range as number[], output!)
2839
}
2940

30-
interpolate = (config: InterpolationConfig, arg: any) =>
31-
new AnimatedInterpolation(this, config, arg)
41+
interpolate<In extends string | number, Out extends string | number = In>(
42+
range: number[] | InterpolationConfig<Out> | Interpolator<In, Out>,
43+
output?: Out[]
44+
) {
45+
return new AnimatedInterpolation(this, range as number[], output!)
46+
}
3247
}
3348

34-
export const interpolate = (
49+
export const interpolate: AnimatedValueFromInterpolation = <
50+
In extends number | string,
51+
Out extends number | string = In
52+
>(
3553
parents: Animated | Animated[],
36-
config: InterpolationConfig,
37-
arg: any
38-
) => parents && new AnimatedInterpolation(parents, config, arg)
54+
range: number[] | InterpolationConfig<Out> | Interpolator<In, Out>,
55+
output?: Out[]
56+
) =>
57+
parents && new AnimatedInterpolation<Out>(parents, range as number[], output!)

src/animated/AnimatedValue.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { InterpolationConfig, Interpolator } from '../types/interpolation'
12
import { AnimatedWithChildren } from './Animated'
23
import AnimatedInterpolation from './AnimatedInterpolation'
3-
import { InterpolationConfig } from './createInterpolation'
44

55
/**
66
* Animated works by building a directed acyclic graph of dependencies
@@ -75,6 +75,10 @@ export default class AnimatedValue extends AnimatedWithChildren {
7575
this.setValue(value, true)
7676
}
7777

78-
interpolate = (config: InterpolationConfig, arg: any) =>
79-
new AnimatedInterpolation(this, config, arg)
78+
interpolate<In extends string | number, Out extends string | number>(
79+
range: number[] | InterpolationConfig<Out> | Interpolator<In, Out>,
80+
output?: Out[]
81+
) {
82+
return new AnimatedInterpolation(this, range as number[], output!)
83+
}
8084
}

src/animated/Globals.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { MutableRefObject, ReactType } from 'react'
2-
import createStringInterpolation from '../shared/stringInterpolation'
2+
import { InterpolatorFromConfig } from '../types/interpolation'
33
import AnimatedStyle from './AnimatedStyle'
44

55
type ApplyPropsFunction = (node?: any, props?: any) => undefined | false
@@ -30,8 +30,8 @@ export function injectFrame(raf: typeof requestFrame, caf: typeof cancelFrame) {
3030
cancelFrame = caf
3131
}
3232

33-
export let interpolation: typeof createStringInterpolation
34-
export function injectStringInterpolation(fn: typeof interpolation) {
33+
export let interpolation: InterpolatorFromConfig<string>
34+
export function injectStringInterpolator(fn: typeof interpolation) {
3535
interpolation = fn
3636
}
3737

src/animated/createInterpolation.ts

Lines changed: 0 additions & 139 deletions
This file was deleted.

src/animated/createInterpolation.test.ts renamed to src/animated/createInterpolator.test.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import colorNames from '../shared/colors'
2-
import createStringInterpolation from '../shared/stringInterpolation'
3-
import createInterpolation from './createInterpolation'
2+
import createStringInterpolator from '../shared/stringInterpolation'
3+
import createInterpolator from './createInterpolator'
44
import * as Globals from './Globals'
55

66
beforeAll(() => {
77
Globals.injectColorNames(colorNames)
8-
Globals.injectStringInterpolation(createStringInterpolation)
8+
Globals.injectStringInterpolator(createStringInterpolator)
99
})
1010

1111
describe('Interpolation', () => {
1212
it('should work with defaults', () => {
13-
const interpolation = createInterpolation({
13+
const interpolation = createInterpolator({
1414
range: [0, 1],
1515
output: [0, 1],
1616
})
@@ -22,14 +22,14 @@ describe('Interpolation', () => {
2222
})
2323

2424
it('should work with interpolation function as argument', () => {
25-
const interpolation = createInterpolation(value => `scale(${value})`)
25+
const interpolation = createInterpolator(value => `scale(${value})`)
2626

2727
expect(interpolation(0)).toBe('scale(0)')
2828
expect(interpolation(10.5)).toBe('scale(10.5)')
2929
})
3030

3131
it('should work with range arrays as arguments', () => {
32-
const interpolation = createInterpolation([0, 1], [100, 200])
32+
const interpolation = createInterpolator([0, 1], [100, 200])
3333

3434
expect(interpolation(0)).toBe(100)
3535
expect(interpolation(0.5)).toBe(150)
@@ -38,7 +38,7 @@ describe('Interpolation', () => {
3838
})
3939

4040
it('should work with output range', () => {
41-
const interpolation = createInterpolation({
41+
const interpolation = createInterpolator({
4242
range: [0, 1],
4343
output: [100, 200],
4444
})
@@ -50,7 +50,7 @@ describe('Interpolation', () => {
5050
})
5151

5252
it('should work with input range', () => {
53-
const interpolation = createInterpolation({
53+
const interpolation = createInterpolator({
5454
range: [100, 200],
5555
output: [0, 1],
5656
})
@@ -62,7 +62,7 @@ describe('Interpolation', () => {
6262
})
6363

6464
it('should work with keyframes without extrapolate', () => {
65-
const interpolation = createInterpolation({
65+
const interpolation = createInterpolator({
6666
range: [0, 1, 2],
6767
output: [0.2, 1, 0.2],
6868
extrapolate: 'clamp',
@@ -72,7 +72,7 @@ describe('Interpolation', () => {
7272
})
7373

7474
it('should work with output ranges as string', () => {
75-
const interpolation = createInterpolation({
75+
const interpolation = createInterpolator({
7676
range: [0, 1],
7777
output: ['rgba(0, 100, 200, 0)', 'rgba(50, 150, 250, 0.4)'],
7878
})
@@ -83,7 +83,7 @@ describe('Interpolation', () => {
8383
})
8484

8585
it('should work with output ranges as short hex string', () => {
86-
const interpolation = createInterpolation({
86+
const interpolation = createInterpolator({
8787
range: [0, 1],
8888
output: ['#024', '#9BF'],
8989
})
@@ -94,7 +94,7 @@ describe('Interpolation', () => {
9494
})
9595

9696
it('should work with output ranges as long hex string', () => {
97-
const interpolation = createInterpolation({
97+
const interpolation = createInterpolator({
9898
range: [0, 1],
9999
output: ['#FF9500', '#87FC70'],
100100
})
@@ -105,7 +105,7 @@ describe('Interpolation', () => {
105105
})
106106

107107
it('should work with output ranges with mixed hex and rgba strings', () => {
108-
const interpolation = createInterpolation({
108+
const interpolation = createInterpolator({
109109
range: [0, 1],
110110
output: ['rgba(100, 120, 140, .4)', '#87FC70'],
111111
})
@@ -116,7 +116,7 @@ describe('Interpolation', () => {
116116
})
117117

118118
it('should work with negative and decimal values in string ranges', () => {
119-
const interpolation = createInterpolation({
119+
const interpolation = createInterpolator({
120120
range: [0, 1],
121121
output: ['-100.5deg', '100deg'],
122122
})
@@ -127,7 +127,7 @@ describe('Interpolation', () => {
127127
})
128128

129129
it('should support a mix of color patterns', () => {
130-
const interpolation = createInterpolation({
130+
const interpolation = createInterpolator({
131131
range: [0, 1, 2],
132132
output: ['rgba(0, 100, 200, 0)', 'rgb(50, 150, 250)', 'red'],
133133
})

0 commit comments

Comments
 (0)