Skip to content

Instantly share code, notes, and snippets.

@dkarmalita
dkarmalita / index.js
Last active February 21, 2017 16:22
Simple express server with compression enabled.
/**
* Simple express server with a dynamic gz compression enabled.
*/
"use strict";
const express = require("express");
const cors = require("cors");
const compression = require('compression');
const morgan = require('morgan');
@dkarmalita
dkarmalita / app-single.jsx
Last active November 30, 2017 19:04
Example: React + Redux
import React, { Component, PropTypes } from 'react';
import { render } from 'react-dom';
import { createStore, combineReducers, bindActionCreators } from 'redux'
import { Provider, connect } from 'react-redux'
// ## Simple component
// ===================
class Root extends Component {
constructor(props) {
super(props);
@dkarmalita
dkarmalita / app-single.jsx
Last active November 30, 2017 19:06
Example: React-Intl + Redux (i18n)
import React, { Component, PropTypes } from 'react';
import { render } from 'react-dom';
import { createStore, combineReducers, bindActionCreators } from 'redux'
import { Provider, connect } from 'react-redux';
import {
FormattedDate,
FormattedTime,
FormattedRelative,
FormattedNumber,
@dkarmalita
dkarmalita / app-single.jsx
Last active November 30, 2017 19:05
Example: Redux-thunk
import React, { Component, PropTypes } from 'react';
import { render } from 'react-dom';
import { createStore, combineReducers, bindActionCreators, applyMiddleware } from 'redux'
import { Provider, connect } from 'react-redux'
import thunkMiddleware from 'redux-thunk'
// We need only two things: add 'thunk' and create async action (see bellow)
// "External" async function
// =========================
@dkarmalita
dkarmalita / stor.js
Last active November 30, 2017 19:05
Example: Cancelable Promise
// The idea is taken from: https://facebook.github.io/react/blog/2015/12/16/ismounted-antipattern.html
const makeCancelable = (promise) => {
let hasCanceled_ = false;
const wrappedPromise = new Promise((resolve, reject) => {
promise.then((val) =>
hasCanceled_ ? reject({isCanceled: true}) : resolve(val)
);
promise.catch((error) =>
hasCanceled_ ? reject({isCanceled: true}) : reject(error)
@dkarmalita
dkarmalita / filter.js
Last active November 30, 2017 19:03
react-intl: redundant console output suppressor
// this filter suppress react-intl's redundant console output while not in production
class ErrorConsoleFilter {
constructor(disabledValue) {
this.disabledValue = disabledValue;
this.originalConsoleError = console.error; // eslint-disable-line no-console
this.outputHandler = this.outputHandler.bind(this);
console.error = this.outputHandler; // eslint-disable-line no-console
}
outputHandler(...args) {
if (args[0].indexOf(this.disabledValue) === 0) return;
@dkarmalita
dkarmalita / browser.setup.js
Last active November 30, 2017 19:01
jsdom based browser environment setup for testing with mocha/chai stack
import { jsdom } from 'jsdom';
const document = jsdom('<!DOCTYPE html><head></head><body></body></html>');
const exposedProperties = ['window', 'navigator', 'document'];
global.document = document;
global.window = document.defaultView;
@dkarmalita
dkarmalita / .editorconfig
Last active February 26, 2018 20:34
Standard .editorconfig
# EditorConfig is awesome: http://EditorConfig.org
# top-most EditorConfig file
root = true
# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
charset = utf-8
indent_style = space
@dkarmalita
dkarmalita / androidSdkInstall.sh
Last active August 27, 2022 00:26
Android SDK & Emulator installer for React Native development
#! /bin/bash
# =================================
# Android SDK & Emulator installer
# For React Native development
# Platform: OSX, WIN
# Ver: 20171128
# dmitriy.karmalita@gmail.com
# Original gist: https://git.io/vbLFg
# =================================
#
@dkarmalita
dkarmalita / shellExec.js
Created November 30, 2017 18:01
async shellExec
/* eslint no-console: 0 */
'use strict'
const shellExec = (
cmd, // shell command to execute
suppressErr=false, // drop errors if any
suppressOutput=false // execute silent
) => new Promise((resolve,reject)=>{
const { spawn } = require('child_process');
const worker = spawn(cmd, { shell: true })