Skip to content

Instantly share code, notes, and snippets.

@SomeHats
Created January 11, 2018 14:04
Show Gist options
  • Save SomeHats/cec539fe9ffe42033f98ac98756f74fd to your computer and use it in GitHub Desktop.
Save SomeHats/cec539fe9ffe42033f98ac98756f74fd to your computer and use it in GitHub Desktop.
Utilities
// @flow
/// MATHSY STUFF ///
// constrain a value between min and max
export const constrain = (min: number, max: number, n: number): number =>
Math.max(min, Math.min(max, n));
// covert degrees into radians
export const degreesToRadians = (degrees: number): number =>
degrees => degrees / (180 / Math.PI);
// convert radians to degrees
export const radiansToDegrees = (radians: number): number =>
radians * (180 / Math.PI);
// linear interpolate between a and b
export const lerp = (a: number, b: number, n): number =>
a + (b - a) * n;
// adjust the range of a number.
// e.g. mapRange(0, 1, 0, 100, 0.5) === 50
// mapRange(5, 10, 0, 10, 6) === 2
export const mapRange = (min1: number, max1: number, min2: number, max2: number, n: number) => {
const progress = (n - min1) / (max1 - min1);
return lerp(min2, max2, progress);
};
// calculate the mean of an array of numbers
export const mean (arr: number[]): number => {
const sum = arr.reduce((a, b) => a + b, 0);
return sum / arr.length;
};
/// NON MATHSY STUFF ///
// remove all the null-ish values from an array
export const compact = <T>(arr: Array<?T>): Array<T> => {
// $FlowFixMe flow can't tell that flow does a type refinement
return arr.filter(item => item != null);
}
// returns a promise that will resolve after `msTimeout` milliseconds
export const delay = (msTimeout: number): Promise<void> =>
new Promise(resolve => setTimeout(resolve, msTimeout));
// a function wrapped with lazy will only ever get called once. every
// subsequent call returns a cached value
export const lazy = <T>(fn: () => T): () => T => {
let cache;
let called = false;
return () => {
if (!called) {
called = true;
cache = fn();
}
return cache;
};
};
const getNowFn = (): () => number => {
let now = () => Date.now();
if (
typeof window !== 'undefined' &&
window.performance &&
window.performance.now
) {
now = () => window.performance.now();
}
return now;
}
// gets ms time. tries to use performance.now(), but falls back to Date.now()
export const now = getNowFn();
// debounce a function to the next requestAnimationFrame call
export const rafDebounce = (fn: () => void): (() => void) => {
let called = false;
let lastArgs = [];
return (...args) => {
lastArgs = args;
if (called) return;
called = true;
window.requestAnimationFrame(() => {
called = false;
lastArgs = [];
fn(...lastArgs);
});
};
};
// call fn n times, and return an array of all the return values
export const times = <T>(n: number, fn: (number) => T): T[] => {
const results = [];
for (let i = 0; i < n; i++) {
results.push(fn(i));
}
return results;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment