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 type of HTML element by tag name | |
* @example | |
* type ElementType = GetElementByTagname<'div'> // HTMLDivElement | |
*/ | |
type GetElementByTagName< | |
Tagname extends keyof React.ReactHTML | |
> = React.ReactHTML[Tagname] extends React.DetailedHTMLFactory<infer A, infer E> ? E : 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
export const useDebouncedCallback = <T extends (...args: any) => void>( | |
callback: T, | |
time: number, | |
deps: React.DependencyList | |
) => { | |
const timerRef = React.useRef<number>(-1); | |
const memoizedCallback = React.useCallback<T>( | |
((...args: any): void => { | |
clearTimeout(timerRef.current); |
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 createContainer = function<Props = {}, Data = {}, Actions = {}>(callback: (props: Props) => [Data, Actions]) { | |
return (props: Props & { children: (data: Data, actions: Actions) => React.ReactNode }) => ( | |
<React.Fragment>{props.children(...callback(props))}</React.Fragment> | |
); | |
}; |
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
/** | |
* React hook to use previous data | |
*/ | |
const usePrevious = function<T>(value: T): undefined | T { | |
const ref = React.useRef<T | undefined>(undefined); | |
React.useEffect(() => { | |
ref.current = value; | |
}); |
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
/** | |
* Wraps any React component (even functional) to PureComponent class | |
*/ | |
export const pureComponent = function<P>(Component: React.ComponentType<P>) { | |
return class extends React.PureComponent<P> { | |
static displayName = `pureComponent(${Component.displayName || Component.name})`; | |
render() { | |
return <Component {...this.props} />; | |
} |
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
/// <reference path="../node_modules/@types/react/index.d.ts" /> | |
/** | |
* Enhancer of JSX attributes | |
*/ | |
type EnhanceAttributes< | |
Tagname extends keyof React.ReactHTML, | |
NewAttributes extends {}, | |
T = React.ReactHTML[Tagname] | |
> = T extends React.DetailedHTMLFactory<infer A, infer E> |
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
/** | |
* Checks text input for valid integer/float | |
* @param {string} value The input value | |
* @returns {boolean} Result of the validate | |
* | |
* @example | |
* validateInputForNumber('123') // true | |
* validateInputForNumber('123.123') // true | |
* validateInputForNumber('123.') // (!) still true | |
* |
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
Number.prototype[Symbol.iterator] = function* () { | |
if (isNaN(this) || !isFinite(this)) { | |
throw new ReferenceError('Non-iterable number') | |
} | |
const abs = Math.abs(this) | |
const isNegative = this < 0 | |
for (let i = 0; i < abs; i++) { | |
yield isNegative ? -i : i |
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
var words = { | |
'true': !![]+[], | |
'false': ![]+[], | |
'Infinity': !![]/[]+[], | |
'undefined': [][[]]+[], | |
'NaN': []/[]+[], | |
'[object Object]': []+{} | |
}; | |
var numbersSpec = { |
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 pause(milliseconds) { | |
const dt = Date.now() | |
while (Date.now() - dt <= milliseconds) {} | |
} |