• API
• FAQ
• Tools
• Archive
daily pastebin goal
47%
SHARE
TWEET

# Untitled

a guest Jan 20th, 2018 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. // @flow
2.
3. /// MATHSY STUFF ///
4. // constrain a value between min and max
5. export const constrain = (min: number, max: number, n: number): number =>
6.   Math.max(min, Math.min(max, n));
7.
8. // covert degrees into radians
9. export const degreesToRadians = (degrees: number): number =>
10.   degrees => degrees / (180 / Math.PI);
11.
12. // convert radians to degrees
14.   radians * (180 / Math.PI);
15.
16. // linear interpolate between a and b
17. export const lerp = (a: number, b: number, n): number =>
18.   a + (b - a) * n;
19.
20. // adjust the range of a number.
21. // e.g. mapRange(0, 1, 0, 100, 0.5) === 50
22. //      mapRange(5, 10, 0, 10, 6) === 2
23. export const mapRange = (min1: number, max1: number, min2: number, max2: number, n: number) => {
24.   const progress = (n - min1) / (max1 - min1);
25.   return lerp(min2, max2, progress);
26. };
27.
28.
29. // calculate the mean of an array of numbers
30. export const mean (arr: number[]): number => {
31.   const sum = arr.reduce((a, b) => a + b, 0);
32.   return sum / arr.length;
33. };
34.
35. /// NON MATHSY STUFF ///
36.
37. // remove all the null-ish values from an array
38. export const compact = <T>(arr: Array<?T>): Array<T> => {
39.   // \$FlowFixMe flow can't tell that flow does a type refinement
40.   return arr.filter(item => item != null);
41. }
42.
43. // returns a promise that will resolve after `msTimeout` milliseconds
44. export const delay = (msTimeout: number): Promise<void> =>
45.   new Promise(resolve => setTimeout(resolve, msTimeout));
46.
47. // a function wrapped with lazy will only ever get called once. every
48. // subsequent call returns a cached value
49. export const lazy = <T>(fn: () => T): () => T => {
50.   let cache;
51.   let called = false;
52.
53.   return () => {
54.     if (!called) {
55.       called = true;
56.       cache = fn();
57.     }
58.
59.     return cache;
60.   };
61. };
62.
63. const getNowFn = (): () => number => {
64.   let now = () => Date.now();
65.
66.   if (
67.     typeof window !== 'undefined' &&
68.     window.performance &&
69.     window.performance.now
70.   ) {
71.     now = () => window.performance.now();
72.   }
73.
74.   return now;
75. }
76.
77. // gets ms time. tries to use performance.now(), but falls back to Date.now()
78. export const now = getNowFn();
79.
80. // debounce a function to the next requestAnimationFrame call
81. export const rafDebounce = (fn: () => void): (() => void) => {
82.   let called = false;
83.   let lastArgs = [];
84.
85.   return (...args) => {
86.     lastArgs = args;
87.
88.     if (called) return;
89.     called = true;
90.     window.requestAnimationFrame(() => {
91.       called = false;
92.       lastArgs = [];
93.       fn(...lastArgs);
94.     });
95.   };
96. };
97.
98. // call fn n times, and return an array of all the return values
99. export const times = <T>(n: number, fn: (number) => T): T[] => {
100.   const results = [];
101.
102.   for (let i = 0; i < n; i++) {
103.     results.push(fn(i));
104.   }
105.
106.   return results;
107. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top