Skip to content

Instantly share code, notes, and snippets.

View DimitryDushkin's full-sized avatar
😀
RxJS!

Dimitry DimitryDushkin

😀
RxJS!
View GitHub Profile
@DimitryDushkin
DimitryDushkin / curry-map.ts
Created September 27, 2018 19:34
Curry functions in map for Typescript
import {Provider, Consumer} from 'react';
// tslint:disable-next-line
type Curry<F extends Function> = F extends (arg1: infer T1, arg2: infer T2, arg3: infer T3) => any
? (arg3: T3) => ReturnType<F>
: never;
type RVSActionCreator<S, A> = (state: S, actions: A, arg: any) => any;
type ActionCreatorsMap<S, A> = {[actionName: string]: RVSActionCreator<S, A>};
@DimitryDushkin
DimitryDushkin / types-for-redux.ts
Last active October 5, 2018 21:30
Typescript + redux + redux-thunk + react-navigation typings example
import {Action as ReduxAction, Middleware as ReduxMiddleware, Store as ReduxStore} from 'redux';
import {NavigationState, NavigationScreenProp} from 'react-navigation';
import {ThunkAction, ThunkDispatch} from 'redux-thunk';
import {StateType} from 'typesafe-actions';
import {reducers} from 'store/reducers/root.reducer';
export type AppState = StateType<typeof reducers>;
export interface Action<T = any, P extends object = any, M = any> extends ReduxAction<T> {
payload?: P;
@DimitryDushkin
DimitryDushkin / values.ts
Last active November 2, 2022 14:15
$Values from Flow in Typescript
type $Values<O extends object> = O[keyof O];
const obj = {
RU: 'ru',
EN: 'en',
} as const;
type vals = $Values<typeof obj>; // vals === 'ru' | 'en';
@DimitryDushkin
DimitryDushkin / promise-retry.js
Last active April 6, 2018 11:42
Retry function that returns promise. Flowtyped.
// @flow
export async function retryPromise<T: Array<*>>(fn: (...T) => Promise<*>, args: T, tryCount: number = 3) {
try {
return await fn(...args);
} catch (e) {
console.log(`Promise retry error. Attempts left: ${tryCount}`, e);
if (tryCount > 1) {
return await retryPromise(fn, args, tryCount - 1);
} else {
@DimitryDushkin
DimitryDushkin / update-size-while-dragging.js
Last active March 4, 2018 17:48
Code from Yandex Zen narrative editor
type DraggerPosition = 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right';
const computeGeometryWhileDragging = (
position: DraggerPosition,
preserveAspectRatio: boolean,
initialGeometry: SlideElementGeometry,
currentGeometry: SlideElementGeometry,
dx: number,
dy: number
): SlideElementGeometry => {
@DimitryDushkin
DimitryDushkin / fit-with-aspect-ratio.js
Last active January 21, 2018 17:48
Get width and height of item with fixed aspect ratio fitted in arbitrary container
// @flow
type Size = { width: number, height: number };
function getFittedSlideSize(container: Size, target: Size): Size {
const targetAspectRatio = target.width / target.height;
const containerAspectRatio = container.width / container.height;
// if aspect ratio of target is "wider" then target's aspect ratio
const fit = targetAspectRatio > containerAspectRatio
? 'width' // fit by width, so target's width = container's width
: 'height'; // fit by height, so target's height = container's height
@DimitryDushkin
DimitryDushkin / getTextLinesFromElement.js
Created September 7, 2017 10:20
Get lines from dom element
function getTextLinesFromElement(target) {
const originalHtml = target.innerHTML;
const lines = [];
let currentLine = [];
let prevWordTop;
target.innerHTML = target.textContent.split(' ').map(w => `<span>${w}</span>`).join(' ');
Array.from(target.querySelectorAll('span')).forEach((span, i , spans) => {
const text = span.textContent;
if (text === '') {
// @flow
type Props = {
el: EventTargetWithPointerEvents,
onDrag: Function,
onDragEnd: Function,
onClick: Function
};
export type GestureEvent = {
deltaX: number
@DimitryDushkin
DimitryDushkin / pathToUrl.js
Last active March 21, 2017 08:58
Simple and safe code for converting express-style paths to URLs
/**
*
* @param {String} path - express-style path
* @param {Object} params
* @returns {String}
*
* @example pathToUrl('/users/:userId', { userId: 10 }) -> '/users/10'
*/
export const pathToUrl = (path, params) => {
return path.replace(/:(\w+)/g, (match, str) => {
@DimitryDushkin
DimitryDushkin / example-of-react-router-query-utils.jsx
Created February 8, 2017 15:01
Example of using react-router query utils
import Modal from 'react-modal';
import { addQuery, removeQuery } from './utils-router.js';
const OPEN_MODAL_QUERY = 'openModal';
function SomeComponent({ location }) {
return <div>
<button onClick={ () => addQuery({ OPEN_MODAL_QUERY : 1 })}>Open modal</button>
<Modal
isOpen={ location.query[OPEN_MODAL_QUERY] }