Skip to content

Instantly share code, notes, and snippets.

@danakt
danakt / getElementByTagName.ts
Created June 27, 2019 17:25
Returns type of HTML element by tag name
/**
* 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;
@danakt
danakt / useDebouncedCallback.ts
Created March 19, 2019 16:10
React hook for creating debounced callback
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);
@danakt
danakt / containerCreator.tsx
Last active February 11, 2019 14:39
Creates react container with render props pattern
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>
);
};
@danakt
danakt / usePrev.ts
Created December 3, 2018 13:09
React hook to use previous data
/**
* 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;
});
@danakt
danakt / pureComponent.ts
Created October 31, 2018 09:08
Pure Component HOC
/**
* 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} />;
}
@danakt
danakt / enhanceJSXAttributes.d.ts
Last active August 30, 2018 16:28
Expanding attributes of JSX element
/// <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>
@danakt
danakt / validateInputForNumber.js
Last active February 27, 2018 17:05
Validate text input for numer
/**
* 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
*
@danakt
danakt / iterableNumber.js
Last active October 25, 2017 14:28
Make number iterable
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
@danakt
danakt / char-without-char.js
Last active June 21, 2017 12:00
Getting chars without using alphanumeric chars
var words = {
'true': !![]+[],
'false': ![]+[],
'Infinity': !![]/[]+[],
'undefined': [][[]]+[],
'NaN': []/[]+[],
'[object Object]': []+{}
};
var numbersSpec = {
@danakt
danakt / pause.js
Last active November 25, 2017 20:40
Pause script
function pause(milliseconds) {
const dt = Date.now()
while (Date.now() - dt <= milliseconds) {}
}