Skip to content

Commit

Permalink
feat(📦): Add contains function
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `lookup` has been renamed to `find`
  • Loading branch information
wcandillon authored May 10, 2019
2 parents 5cc748b + 7c6f495 commit e8d52ae
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 84 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![CircleCI](https://circleci.com/gh/wcandillon/react-native-redash.svg?style=svg)](https://circleci.com/gh/wcandillon/react-native-redash)
[![npm version](https://badge.fury.io/js/react-native-redash.svg)](https://badge.fury.io/js/react-native-redash)

Utility library for React Native Reanimated.
Utility library for React Native Gesture Handler and Reanimated.

## Usage

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

Example usage: Look

### `lookup(nodes, index, notFound)`
### `find(nodes, index, notFound)`

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

```js
lookup(values: Node[], index: Node, notFound: Node) => Node
find(values: Node[], index: Node, notFound: Node) => Node
```


### `contains(nodes, index, notFound)`

Returns 1 if the node value is contained in the array of nodes, 0 otherwise.

```js
contains(values: Node[], value: Node) => Node
```

### `binaryInterpolation(node, from, to)`
Expand Down
34 changes: 11 additions & 23 deletions src/AnimationRunners.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Animated from "react-native-reanimated";

const {
Clock,
Value,
block,
timing,
Expand All @@ -12,24 +11,13 @@ const {
set,
startClock,
clockRunning,
add,
} = Animated;

export {
timing, spring, clockRunning, add,
};

export type SpringConfig= Parameters<typeof spring>[1];
export type TimingConfig = Parameters<typeof timing>[1];
export type Clock = Parameters<typeof clockRunning>[0];
export type Node = ReturnType<typeof add>;
export type Adaptable<T> = Node | T;

export function runDecay(
clock: Clock,
value: Adaptable<number>,
velocity: Adaptable<number>,
rerunDecaying: any,
clock: Animated.Clock,
value: Animated.Adaptable<number>,
velocity: Animated.Adaptable<number>,
rerunDecaying: Animated.Value<number>,
) {
const state = {
finished: new Value(0),
Expand Down Expand Up @@ -60,10 +48,10 @@ export function runDecay(
}

export function runSpring(
clock: Clock,
value: Adaptable<number>,
dest: Adaptable<number>,
config: SpringConfig = {
clock: Animated.Clock,
value: Animated.Adaptable<number>,
dest: Animated.Adaptable<number>,
config: Animated.SpringConfig = {
toValue: new Value(0),
damping: 7,
mass: 1,
Expand Down Expand Up @@ -96,9 +84,9 @@ export function runSpring(
}

export function runTiming(
clock: Clock,
value: Adaptable<any>,
config: TimingConfig,
clock: Animated.Clock,
value: Animated.Adaptable<any>,
config: Animated.TimingConfig,
) {
const state = {
finished: new Value(0),
Expand Down
20 changes: 10 additions & 10 deletions src/Colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,15 @@ const {
sub,
color,
Extrapolate,
Node,
} = Animated;

type Node = ReturnType<typeof add>;
type Adaptable<T> = Node | T;

interface RGBColor {
r: number;
g: number;
b: number;
}

function match(condsAndResPairs: Adaptable<number>[], offset = 0): any {
function match(condsAndResPairs: Animated.Adaptable<number>[], offset = 0): any {
if (condsAndResPairs.length - offset === 1) {
return condsAndResPairs[offset];
} if (condsAndResPairs.length - offset === 0) {
Expand All @@ -39,9 +35,9 @@ function match(condsAndResPairs: Adaptable<number>[], offset = 0): any {
}

function colorHSV(
h: Adaptable<number> /* 0 - 360 */,
s: Adaptable<number> /* 0 - 1 */,
v: Adaptable<number>, /* 0 - 1 */
h: Animated.Adaptable<number> /* 0 - 360 */,
s: Animated.Adaptable<number> /* 0 - 1 */,
v: Animated.Adaptable<number>, /* 0 - 1 */
) {
// Converts color from HSV format into RGB
// Formula explained here: https://www.rapidtables.com/convert/color/hsv-to-rgb.html
Expand All @@ -51,7 +47,11 @@ function colorHSV(

const m = sub(v, c);

const colorRGB = (r: Adaptable<number>, g: Adaptable<number>, b: Adaptable<number>) => color(
const colorRGB = (
r: Animated.Adaptable<number>,
g: Animated.Adaptable<number>,
b: Animated.Adaptable<number>,
) => color(
round(multiply(255, add(r, m))),
round(multiply(255, add(g, m))),
round(multiply(255, add(b, m))),
Expand Down Expand Up @@ -98,7 +98,7 @@ const rgbToHsv = (c: RGBColor) => {
return { h: h * 360, s, v };
};

const interpolateColors = (animationValue: Adaptable<number>, inputRange: number[], colors: RGBColor[]) => {
const interpolateColors = (animationValue: Animated.Adaptable<number>, inputRange: number[], colors: RGBColor[]) => {
const colorsAsHSV = colors.map(c => rgbToHsv(c));
const h = interpolate(animationValue, {
inputRange,
Expand Down
12 changes: 4 additions & 8 deletions src/Interactable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ const {
Value,
} = Animated;

export { add };
export type Node = ReturnType<typeof add>;
export type Adaptable<T> = Node | T;

const ANIMATOR_PAUSE_CONSECUTIVE_FRAMES = 10;
const ANIMATOR_PAUSE_ZERO_VELOCITY = 1;
const DEFAULT_SNAP_TENSION = 300;
Expand Down Expand Up @@ -146,8 +142,8 @@ function gravityBehavior(
}

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

// imperative commands
setVelocity({ x, y }: { x: Adaptable<number>, y: Adaptable<number> }) {
setVelocity({ x, y }: { x: Animated.Adaptable<number>, y: Animated.Adaptable<number> }) {
if (x !== undefined) {
this.dragging.x.setValue(1);
this.velocity.x.setValue(x);
Expand All @@ -594,7 +590,7 @@ export default class Interactable extends React.PureComponent<InteractableProps>
}
}

changePosition({ x, y }: { x: Adaptable<number>, y: Adaptable<number>}) {
changePosition({ x, y }: { x: Animated.Adaptable<number>, y: Animated.Adaptable<number>}) {
if (x !== undefined) {
this.dragging.x.setValue(1);
this.position.x.setValue(x);
Expand Down
16 changes: 6 additions & 10 deletions src/Math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,20 @@ const {
min: min2,
max: max2,
greaterOrEq,
Node,
} = Animated;

type Node = ReturnType<typeof add>;
type Adaptable<T> = Node | T;

// ## Math
export const toRad = (deg: Adaptable<number>) => multiply(deg, Math.PI / 180);
export const toDeg = (rad: Adaptable<number>) => multiply(rad, 180 / Math.PI);
export const toRad = (deg: Animated.Adaptable<number>) => multiply(deg, Math.PI / 180);
export const toDeg = (rad: Animated.Adaptable<number>) => multiply(rad, 180 / Math.PI);

export const min = (...args: Adaptable<number>[]) => args.reduce((acc, arg) => min2(acc, arg));
export const max = (...args: Adaptable<number>[]) => args.reduce((acc, arg) => max2(acc, arg));
export const min = (...args: Animated.Adaptable<number>[]) => args.reduce((acc, arg) => min2(acc, arg));
export const max = (...args: Animated.Adaptable<number>[]) => args.reduce((acc, arg) => max2(acc, arg));

export const atan = (rad: Adaptable<number>) => sub(
export const atan = (rad: Animated.Adaptable<number>) => sub(
multiply(Math.PI / 4, rad),
multiply(multiply(rad, sub(abs(rad), 1)), add(0.2447, multiply(0.0663, abs(rad)))),
);
export const atan2 = (y: Adaptable<number>, x: Adaptable<number>) => {
export const atan2 = (y: Animated.Adaptable<number>, x: Animated.Adaptable<number>) => {
const coeff1 = Math.PI / 4;
const coeff2 = 3 * coeff1;
const absY = abs(y);
Expand Down
9 changes: 1 addition & 8 deletions src/ReText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,10 @@ import * as React from "react";
import { TextInput, TextStyle } from "react-native";
import Animated from "react-native-reanimated";

const { Value, concat } = Animated;
const AnimatedTextInput = Animated.createAnimatedComponent(TextInput);

export { Value };
export type Value = typeof Value;

export { concat };
export type Node = ReturnType<typeof concat>;

interface TextProps {
text: Node;
text: Animated.Node<string>;
style?: TextStyle;
}

Expand Down
44 changes: 22 additions & 22 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export {
const {
event,
cond,
clockRunning,
Value,
add,
multiply,
Expand All @@ -24,48 +23,49 @@ const {
divide,
sub,
eq,
timing,
Node,
or,
} = Animated;

export { timing, clockRunning, add };

export type TimingConfig = Parameters<typeof timing>[1];
export type Clock = Parameters<typeof clockRunning>[0];
export type Node = ReturnType<typeof add>;
export type Adaptable<T> = Node | T;

// ## Animations
export const snapPoint = (value: Adaptable<number>, velocity: Adaptable<number>, points: number[]) => {
export const snapPoint = (
value: Animated.Adaptable<number>,
velocity: Animated.Adaptable<number>,
points: number[],
) => {
const point = add(value, multiply(0.2, velocity));
const diffPoint = (p: Adaptable<number>) => abs(sub(point, p));
const diffPoint = (p: Animated.Adaptable<number>) => abs(sub(point, p));
const deltas = points.map(p => diffPoint(p));
const minDelta = min(...deltas);
return points.reduce((acc: Node, p: number) => cond(eq(diffPoint(p), minDelta), p, acc), new Value());
return points.reduce((acc: Animated.Node<any>, p: number) => cond(eq(diffPoint(p), minDelta), p, acc), new Value());
};

export const binaryInterpolation = (
value: Adaptable<number>,
origin: Adaptable<number>,
destination: Adaptable<number>,
value: Animated.Adaptable<number>,
origin: Animated.Adaptable<number>,
destination: Animated.Adaptable<number>,
) => interpolate(value, {
inputRange: [0, 1],
outputRange: [origin, destination],
});

export const lookup = (
array: Adaptable<number>[],
index: Adaptable<number>,
notFound: Node = new Value(),
export const find = (
array: Animated.Adaptable<number>[],
index: Animated.Adaptable<number>,
notFound: Animated.Node<any> = new Value(),
) => array.reduce((acc, v, i) => cond(eq(i, index), v, acc), notFound);

export const contains = (
values: Animated.Node<number>[],
value: Animated.Node<number>,
): Animated.Node<number> => values.reduce((acc, v) => or(acc, eq(value, v)), new Value(0));

// ## Transformations
export const translateZ = (perspective: Adaptable<number>, z: Adaptable<number>) => (
export const translateZ = (perspective: Animated.Adaptable<number>, z: Animated.Adaptable<number>) => (
{ scale: divide(perspective, sub(perspective, z)) }
);

// ## Gestures
export const onScroll = (contentOffset: { x?: Node, y?: Node }) => event(
export const onScroll = (contentOffset: { x?: Animated.Node<number>, y?: Animated.Node<number> }) => event(
[
{
nativeEvent: {
Expand Down

0 comments on commit e8d52ae

Please sign in to comment.