This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type FetchRes<T> = Response & { | |
data: T; | |
}; | |
/** | |
* A Fetch Request that errors on non 2xx status codes | |
*/ | |
export default function betterFetch<T>( | |
input: RequestInfo, | |
init?: RequestInit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Memoize a async function (any function which returns a promise) | |
* | |
* @param {Function} func - Function which returns a promise | |
* @param {boolean} [cacheError=false] - Should cache promise rejections | |
* @param {Function} [resolver] - Resolves cache key. Uses first arg as default | |
* @returns {Function.<Promise>} - Returns memoized function | |
*/ | |
export default function memoizeAsync(func, cacheError = false, resolver) { | |
const cache = new Map(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState, useCallback, useLayoutEffect, RefObject, useMemo } from 'react'; | |
import debounce from 'debounce'; | |
export type Dimensions = [number, number]; | |
export default function useRefDimensions(ref: RefObject<HTMLElement>): Dimensions { | |
const [width, setWidth] = useState(0); | |
const [height, setHeight] = useState(0); | |
const computeDimensions = useCallback((elem: Element) => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Combines reducers with root reducer | |
* | |
* @param {function(Object, Object): Object} rootReducer | |
* @param {Object<string, function(Object, Object): Object>} [reducers={}] | |
* @returns {function(Object, Object): Object} | |
*/ | |
export default function combineReducersWithRoot(rootReducer, reducers = {}) { | |
const entries = Object.entries(reducers); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function noop() {} | |
type CatchCallback<T> = Parameters<Promise<T>['catch']>[0]; | |
class PromiseChain<T> { | |
#chain: Promise<any> = Promise.resolve(); | |
#values: Array<T> = []; | |
add(promise: Promise<T | void | undefined>, promiseCatch: CatchCallback<T> = noop) { | |
this.#chain = this.#chain | |
.then(() => promise) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const isRequestIdleCallbackSupported = | |
'requestIdleCallback' in window && | |
typeof window.requestIdleCallback === 'function'; | |
/** | |
* A higher order function that wraps passed function to run | |
* inside of a requestIdleCallback or "onload" event | |
* depending on what is supported in the browser. | |
* | |
* This is useful for running low priority app level bootstrapping events. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Converts Java SimpleDataFormat to ISO 8601 format | |
* | |
* @example | |
* simpleDateIso('2019-01-24T12:42:17:234-0700') => '2019-01-24T12:42:17.234-0700' | |
* | |
* @param {string} simpleDate - Java SimpleDataFormat string | |
* https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html | |
* @returns {string|null} | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Returns rem representation of pixel based on root size. | |
* | |
* @param {number} px - Pixel Size | |
* @param {number} [rootSize=16] - Root Font Size | |
* @returns {string} | |
*/ | |
export default function pxToRem(px: number, rootSize: number = 16): string { | |
return `${px / rootSize}rem`; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useReducer } from 'react'; | |
type Toggle = (forceToggle?: boolean) => void; | |
export default function useToggle(initial: boolean = false) { | |
return useReducer( | |
(toggledState: boolean, forceToggle?: boolean) => forceToggle || !toggledState, | |
initial | |
) as [boolean, Toggle]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Higher order function which handles | |
* key presses "onKeyDown". | |
* | |
* Passes event to function. | |
* | |
* @param {Function} fn - Function to be invoked with event | |
* @return {function(Event): any} | |
*/ | |
const clickKeyPress = (fn) => (ev) => { |
NewerOlder