Skip to content

Commit e8d52ae

Browse files
authored
feat(📦): Add contains function
BREAKING CHANGE: `lookup` has been renamed to `find`
2 parents 5cc748b + 7c6f495 commit e8d52ae

File tree

7 files changed

+66
-84
lines changed

7 files changed

+66
-84
lines changed

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![CircleCI](https://circleci.com/gh/wcandillon/react-native-redash.svg?style=svg)](https://circleci.com/gh/wcandillon/react-native-redash)
44
[![npm version](https://badge.fury.io/js/react-native-redash.svg)](https://badge.fury.io/js/react-native-redash)
55

6-
Utility library for React Native Reanimated.
6+
Utility library for React Native Gesture Handler and Reanimated.
77

88
## Usage
99

@@ -130,12 +130,21 @@ runDecay(clock: Clock, value: Node, velocity: Node, rerunDecaying: Node): Node
130130

131131
Example usage: Look
132132

133-
### `lookup(nodes, index, notFound)`
133+
### `find(nodes, index, notFound)`
134134

135135
Returns the node from the list of nodes at the specified index. If not, it returns the notFound node.
136136

137137
```js
138-
lookup(values: Node[], index: Node, notFound: Node) => Node
138+
find(values: Node[], index: Node, notFound: Node) => Node
139+
```
140+
141+
142+
### `contains(nodes, index, notFound)`
143+
144+
Returns 1 if the node value is contained in the array of nodes, 0 otherwise.
145+
146+
```js
147+
contains(values: Node[], value: Node) => Node
139148
```
140149

141150
### `binaryInterpolation(node, from, to)`

src/AnimationRunners.ts

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import Animated from "react-native-reanimated";
22

33
const {
4-
Clock,
54
Value,
65
block,
76
timing,
@@ -12,24 +11,13 @@ const {
1211
set,
1312
startClock,
1413
clockRunning,
15-
add,
1614
} = Animated;
1715

18-
export {
19-
timing, spring, clockRunning, add,
20-
};
21-
22-
export type SpringConfig= Parameters<typeof spring>[1];
23-
export type TimingConfig = Parameters<typeof timing>[1];
24-
export type Clock = Parameters<typeof clockRunning>[0];
25-
export type Node = ReturnType<typeof add>;
26-
export type Adaptable<T> = Node | T;
27-
2816
export function runDecay(
29-
clock: Clock,
30-
value: Adaptable<number>,
31-
velocity: Adaptable<number>,
32-
rerunDecaying: any,
17+
clock: Animated.Clock,
18+
value: Animated.Adaptable<number>,
19+
velocity: Animated.Adaptable<number>,
20+
rerunDecaying: Animated.Value<number>,
3321
) {
3422
const state = {
3523
finished: new Value(0),
@@ -60,10 +48,10 @@ export function runDecay(
6048
}
6149

6250
export function runSpring(
63-
clock: Clock,
64-
value: Adaptable<number>,
65-
dest: Adaptable<number>,
66-
config: SpringConfig = {
51+
clock: Animated.Clock,
52+
value: Animated.Adaptable<number>,
53+
dest: Animated.Adaptable<number>,
54+
config: Animated.SpringConfig = {
6755
toValue: new Value(0),
6856
damping: 7,
6957
mass: 1,
@@ -96,9 +84,9 @@ export function runSpring(
9684
}
9785

9886
export function runTiming(
99-
clock: Clock,
100-
value: Adaptable<any>,
101-
config: TimingConfig,
87+
clock: Animated.Clock,
88+
value: Animated.Adaptable<any>,
89+
config: Animated.TimingConfig,
10290
) {
10391
const state = {
10492
finished: new Value(0),

src/Colors.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,15 @@ const {
1313
sub,
1414
color,
1515
Extrapolate,
16-
Node,
1716
} = Animated;
1817

19-
type Node = ReturnType<typeof add>;
20-
type Adaptable<T> = Node | T;
21-
2218
interface RGBColor {
2319
r: number;
2420
g: number;
2521
b: number;
2622
}
2723

28-
function match(condsAndResPairs: Adaptable<number>[], offset = 0): any {
24+
function match(condsAndResPairs: Animated.Adaptable<number>[], offset = 0): any {
2925
if (condsAndResPairs.length - offset === 1) {
3026
return condsAndResPairs[offset];
3127
} if (condsAndResPairs.length - offset === 0) {
@@ -39,9 +35,9 @@ function match(condsAndResPairs: Adaptable<number>[], offset = 0): any {
3935
}
4036

4137
function colorHSV(
42-
h: Adaptable<number> /* 0 - 360 */,
43-
s: Adaptable<number> /* 0 - 1 */,
44-
v: Adaptable<number>, /* 0 - 1 */
38+
h: Animated.Adaptable<number> /* 0 - 360 */,
39+
s: Animated.Adaptable<number> /* 0 - 1 */,
40+
v: Animated.Adaptable<number>, /* 0 - 1 */
4541
) {
4642
// Converts color from HSV format into RGB
4743
// Formula explained here: https://www.rapidtables.com/convert/color/hsv-to-rgb.html
@@ -51,7 +47,11 @@ function colorHSV(
5147

5248
const m = sub(v, c);
5349

54-
const colorRGB = (r: Adaptable<number>, g: Adaptable<number>, b: Adaptable<number>) => color(
50+
const colorRGB = (
51+
r: Animated.Adaptable<number>,
52+
g: Animated.Adaptable<number>,
53+
b: Animated.Adaptable<number>,
54+
) => color(
5555
round(multiply(255, add(r, m))),
5656
round(multiply(255, add(g, m))),
5757
round(multiply(255, add(b, m))),
@@ -98,7 +98,7 @@ const rgbToHsv = (c: RGBColor) => {
9898
return { h: h * 360, s, v };
9999
};
100100

101-
const interpolateColors = (animationValue: Adaptable<number>, inputRange: number[], colors: RGBColor[]) => {
101+
const interpolateColors = (animationValue: Animated.Adaptable<number>, inputRange: number[], colors: RGBColor[]) => {
102102
const colorsAsHSV = colors.map(c => rgbToHsv(c));
103103
const h = interpolate(animationValue, {
104104
inputRange,

src/Interactable.tsx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ const {
3131
Value,
3232
} = Animated;
3333

34-
export { add };
35-
export type Node = ReturnType<typeof add>;
36-
export type Adaptable<T> = Node | T;
37-
3834
const ANIMATOR_PAUSE_CONSECUTIVE_FRAMES = 10;
3935
const ANIMATOR_PAUSE_ZERO_VELOCITY = 1;
4036
const DEFAULT_SNAP_TENSION = 300;
@@ -146,8 +142,8 @@ function gravityBehavior(
146142
}
147143

148144
function bounceBehavior(target: any, obj: any, area: any, bounce: number = 0) {
149-
const xnodes: Node[] = [];
150-
const ynodes: Node[] = [];
145+
const xnodes: Animated.Node<number>[] = [];
146+
const ynodes: Animated.Node<number>[] = [];
151147
const flipx = set(obj.vx, multiply(-1, obj.vx, bounce));
152148
const flipy = set(obj.vy, multiply(-1, obj.vy, bounce));
153149
if (area.left !== undefined) {
@@ -567,7 +563,7 @@ export default class Interactable extends React.PureComponent<InteractableProps>
567563
}
568564

569565
// imperative commands
570-
setVelocity({ x, y }: { x: Adaptable<number>, y: Adaptable<number> }) {
566+
setVelocity({ x, y }: { x: Animated.Adaptable<number>, y: Animated.Adaptable<number> }) {
571567
if (x !== undefined) {
572568
this.dragging.x.setValue(1);
573569
this.velocity.x.setValue(x);
@@ -594,7 +590,7 @@ export default class Interactable extends React.PureComponent<InteractableProps>
594590
}
595591
}
596592

597-
changePosition({ x, y }: { x: Adaptable<number>, y: Adaptable<number>}) {
593+
changePosition({ x, y }: { x: Animated.Adaptable<number>, y: Animated.Adaptable<number>}) {
598594
if (x !== undefined) {
599595
this.dragging.x.setValue(1);
600596
this.position.x.setValue(x);

src/Math.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,20 @@ const {
1111
min: min2,
1212
max: max2,
1313
greaterOrEq,
14-
Node,
1514
} = Animated;
1615

17-
type Node = ReturnType<typeof add>;
18-
type Adaptable<T> = Node | T;
19-
2016
// ## Math
21-
export const toRad = (deg: Adaptable<number>) => multiply(deg, Math.PI / 180);
22-
export const toDeg = (rad: Adaptable<number>) => multiply(rad, 180 / Math.PI);
17+
export const toRad = (deg: Animated.Adaptable<number>) => multiply(deg, Math.PI / 180);
18+
export const toDeg = (rad: Animated.Adaptable<number>) => multiply(rad, 180 / Math.PI);
2319

24-
export const min = (...args: Adaptable<number>[]) => args.reduce((acc, arg) => min2(acc, arg));
25-
export const max = (...args: Adaptable<number>[]) => args.reduce((acc, arg) => max2(acc, arg));
20+
export const min = (...args: Animated.Adaptable<number>[]) => args.reduce((acc, arg) => min2(acc, arg));
21+
export const max = (...args: Animated.Adaptable<number>[]) => args.reduce((acc, arg) => max2(acc, arg));
2622

27-
export const atan = (rad: Adaptable<number>) => sub(
23+
export const atan = (rad: Animated.Adaptable<number>) => sub(
2824
multiply(Math.PI / 4, rad),
2925
multiply(multiply(rad, sub(abs(rad), 1)), add(0.2447, multiply(0.0663, abs(rad)))),
3026
);
31-
export const atan2 = (y: Adaptable<number>, x: Adaptable<number>) => {
27+
export const atan2 = (y: Animated.Adaptable<number>, x: Animated.Adaptable<number>) => {
3228
const coeff1 = Math.PI / 4;
3329
const coeff2 = 3 * coeff1;
3430
const absY = abs(y);

src/ReText.tsx

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,10 @@ import * as React from "react";
22
import { TextInput, TextStyle } from "react-native";
33
import Animated from "react-native-reanimated";
44

5-
const { Value, concat } = Animated;
65
const AnimatedTextInput = Animated.createAnimatedComponent(TextInput);
76

8-
export { Value };
9-
export type Value = typeof Value;
10-
11-
export { concat };
12-
export type Node = ReturnType<typeof concat>;
13-
147
interface TextProps {
15-
text: Node;
8+
text: Animated.Node<string>;
169
style?: TextStyle;
1710
}
1811

src/index.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export {
1515
const {
1616
event,
1717
cond,
18-
clockRunning,
1918
Value,
2019
add,
2120
multiply,
@@ -24,48 +23,49 @@ const {
2423
divide,
2524
sub,
2625
eq,
27-
timing,
28-
Node,
26+
or,
2927
} = Animated;
3028

31-
export { timing, clockRunning, add };
32-
33-
export type TimingConfig = Parameters<typeof timing>[1];
34-
export type Clock = Parameters<typeof clockRunning>[0];
35-
export type Node = ReturnType<typeof add>;
36-
export type Adaptable<T> = Node | T;
37-
3829
// ## Animations
39-
export const snapPoint = (value: Adaptable<number>, velocity: Adaptable<number>, points: number[]) => {
30+
export const snapPoint = (
31+
value: Animated.Adaptable<number>,
32+
velocity: Animated.Adaptable<number>,
33+
points: number[],
34+
) => {
4035
const point = add(value, multiply(0.2, velocity));
41-
const diffPoint = (p: Adaptable<number>) => abs(sub(point, p));
36+
const diffPoint = (p: Animated.Adaptable<number>) => abs(sub(point, p));
4237
const deltas = points.map(p => diffPoint(p));
4338
const minDelta = min(...deltas);
44-
return points.reduce((acc: Node, p: number) => cond(eq(diffPoint(p), minDelta), p, acc), new Value());
39+
return points.reduce((acc: Animated.Node<any>, p: number) => cond(eq(diffPoint(p), minDelta), p, acc), new Value());
4540
};
4641

4742
export const binaryInterpolation = (
48-
value: Adaptable<number>,
49-
origin: Adaptable<number>,
50-
destination: Adaptable<number>,
43+
value: Animated.Adaptable<number>,
44+
origin: Animated.Adaptable<number>,
45+
destination: Animated.Adaptable<number>,
5146
) => interpolate(value, {
5247
inputRange: [0, 1],
5348
outputRange: [origin, destination],
5449
});
5550

56-
export const lookup = (
57-
array: Adaptable<number>[],
58-
index: Adaptable<number>,
59-
notFound: Node = new Value(),
51+
export const find = (
52+
array: Animated.Adaptable<number>[],
53+
index: Animated.Adaptable<number>,
54+
notFound: Animated.Node<any> = new Value(),
6055
) => array.reduce((acc, v, i) => cond(eq(i, index), v, acc), notFound);
6156

57+
export const contains = (
58+
values: Animated.Node<number>[],
59+
value: Animated.Node<number>,
60+
): Animated.Node<number> => values.reduce((acc, v) => or(acc, eq(value, v)), new Value(0));
61+
6262
// ## Transformations
63-
export const translateZ = (perspective: Adaptable<number>, z: Adaptable<number>) => (
63+
export const translateZ = (perspective: Animated.Adaptable<number>, z: Animated.Adaptable<number>) => (
6464
{ scale: divide(perspective, sub(perspective, z)) }
6565
);
6666

6767
// ## Gestures
68-
export const onScroll = (contentOffset: { x?: Node, y?: Node }) => event(
68+
export const onScroll = (contentOffset: { x?: Animated.Node<number>, y?: Animated.Node<number> }) => event(
6969
[
7070
{
7171
nativeEvent: {

0 commit comments

Comments
 (0)