Skip to content

Instantly share code, notes, and snippets.

View DimitryDushkin's full-sized avatar
😀
RxJS!

Dimitry DimitryDushkin

😀
RxJS!
View GitHub Profile
@DimitryDushkin
DimitryDushkin / let.js
Created January 18, 2017 08:16
Power or let (JS)
function letExample() {
var a = [1,2,3,4,5],
seq = Promise.resolve();
for (let b of a) {
seq = seq.then(() => new Promise(resolve => { setTimeout(()=> {console.log(b); resolve();}, 100) }));
}
}
function varExample() {
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>YAF</title>
<style type='text/css'>
body {
padding: 24px;
font-size: 18px;
}
@DimitryDushkin
DimitryDushkin / ReactTablePagination.jsx
Created January 30, 2017 11:33
ReactTablePagination
import React from 'react';
import cn from 'classnames';
import Select from 'react-islands/components/Select';
import Item from 'react-islands/components/Item';
import Input from 'react-islands/components/TextInput';
import './ReactTablePagination.styl';
export default React.createClass({
@DimitryDushkin
DimitryDushkin / react-router-queyry-utils.js
Last active March 5, 2021 13:38
React router utility functions to add and remove queries
import { browserHistory } from 'react-router';
/**
* @param {Object} query
*/
export const addQuery = (query) => {
const location = Object.assign({}, browserHistory.getCurrentLocation());
Object.assign(location.query, query);
browserHistory.push(location);
};
@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] }
@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) => {
// @flow
type Props = {
el: EventTargetWithPointerEvents,
onDrag: Function,
onDragEnd: Function,
onClick: Function
};
export type GestureEvent = {
deltaX: number
@DimitryDushkin
DimitryDushkin / generate-changelog.js
Last active July 31, 2017 13:22
Generate debian changelog via node.js script
const fs = require('fs'),
prependFile = require('prepend-file'),
moment = require('moment'),
version = getVersion(),
template =
`some-package-name (${version}) stable; urgency=low
* ${version}
-- Dmitry Dushkin <dndushkin@yandex-team.ru> ${getTime()}
@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 === '') {
@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