UNPKG

4.07 kBJavaScriptView Raw
1"use strict";
2// Taken from https://github.com/facebook/react-native/blob/0b9ea60b4fee8cacc36e7160e31b91fc114dbc0d/Libraries/Animated/src/nodes/AnimatedInterpolation.js
3Object.defineProperty(exports, "__esModule", { value: true });
4exports.interpolate = void 0;
5function interpolateFunction(input, inputRange, outputRange, options) {
6 const { extrapolateLeft, extrapolateRight, easing } = options;
7 let result = input;
8 const [inputMin, inputMax] = inputRange;
9 const [outputMin, outputMax] = outputRange;
10 if (result < inputMin) {
11 if (extrapolateLeft === 'identity') {
12 return result;
13 }
14 if (extrapolateLeft === 'clamp') {
15 result = inputMin;
16 }
17 else if (extrapolateLeft === 'extend') {
18 // noop
19 }
20 }
21 if (result > inputMax) {
22 if (extrapolateRight === 'identity') {
23 return result;
24 }
25 if (extrapolateRight === 'clamp') {
26 result = inputMax;
27 }
28 else if (extrapolateRight === 'extend') {
29 // noop
30 }
31 }
32 if (outputMin === outputMax) {
33 return outputMin;
34 }
35 // Input Range
36 result = (result - inputMin) / (inputMax - inputMin);
37 // Easing
38 result = easing(result);
39 // Output Range
40 result = result * (outputMax - outputMin) + outputMin;
41 return result;
42}
43function findRange(input, inputRange) {
44 let i;
45 for (i = 1; i < inputRange.length - 1; ++i) {
46 if (inputRange[i] >= input) {
47 break;
48 }
49 }
50 return i - 1;
51}
52function checkValidInputRange(arr) {
53 for (let i = 1; i < arr.length; ++i) {
54 if (!(arr[i] > arr[i - 1])) {
55 throw new Error(`inputRange must be strictly monotonically non-decreasing but got [${arr.join(',')}]`);
56 }
57 }
58}
59function checkInfiniteRange(name, arr) {
60 if (arr.length < 2) {
61 throw new Error(name + ' must have at least 2 elements');
62 }
63 for (const index in arr) {
64 if (typeof arr[index] !== 'number') {
65 throw new Error(`${name} must contain only numbers`);
66 }
67 if (arr[index] === -Infinity || arr[index] === Infinity) {
68 throw new Error(`${name} must contain only finite numbers, but got [${arr.join(',')}]`);
69 }
70 }
71}
72function interpolate(input, inputRange, outputRange, options) {
73 var _a;
74 if (typeof input === 'undefined') {
75 throw new Error('input can not be undefined');
76 }
77 if (typeof inputRange === 'undefined') {
78 throw new Error('inputRange can not be undefined');
79 }
80 if (typeof outputRange === 'undefined') {
81 throw new Error('outputRange can not be undefined');
82 }
83 if (inputRange.length !== outputRange.length) {
84 throw new Error('inputRange (' +
85 inputRange.length +
86 ') and outputRange (' +
87 outputRange.length +
88 ') must have the same length');
89 }
90 checkInfiniteRange('inputRange', inputRange);
91 checkInfiniteRange('outputRange', outputRange);
92 checkValidInputRange(inputRange);
93 const easing = (_a = options === null || options === void 0 ? void 0 : options.easing) !== null && _a !== void 0 ? _a : ((num) => num);
94 let extrapolateLeft = 'extend';
95 if ((options === null || options === void 0 ? void 0 : options.extrapolateLeft) !== undefined) {
96 extrapolateLeft = options.extrapolateLeft;
97 }
98 let extrapolateRight = 'extend';
99 if ((options === null || options === void 0 ? void 0 : options.extrapolateRight) !== undefined) {
100 extrapolateRight = options.extrapolateRight;
101 }
102 if (typeof input !== 'number') {
103 throw new TypeError('Cannot interpolate an input which is not a number');
104 }
105 const range = findRange(input, inputRange);
106 return interpolateFunction(input, [inputRange[range], inputRange[range + 1]], [outputRange[range], outputRange[range + 1]], {
107 easing,
108 extrapolateLeft,
109 extrapolateRight,
110 });
111}
112exports.interpolate = interpolate;
113//# sourceMappingURL=interpolate.js.map
\No newline at end of file