Skip to content

Instantly share code, notes, and snippets.

@rzane
rzane / Backoff.js
Last active May 9, 2018 18:11
Exponential backoff
const create = ({
shouldBackoff,
factor = 5,
maxDelay = 32000,
maxAttempts = 20
}) => {
const backoff = attempt => {
const randomMs = Math.floor(Math.random() * 1000);
const exponential = Math.pow(factor, attempt) + randomMs;
const delay = Math.min(exponential, maxDelay);
@rzane
rzane / index.html
Created March 2, 2018 18:56
Apollo Boost Headers Reproduction
<html>
<head>
<title>Apollo Query Reproduction</title>
<meta charset="UTF-8" />
</head>
<body>
<div id="app">
<h1>These are the headers that get sent along:</h1>
</div>
@rzane
rzane / declare-props.js
Created November 15, 2017 21:13
An interactive script to declare react props
const fs = require('fs');
const path = require('path');
const { CLIEngine } = require('eslint');
const readline = require('readline-sync');
const cli = new CLIEngine({
parser: 'babel-eslint',
useEslintrc: false,
plugins: ['react'],
rules: { 'react/prop-types': ['error', {}] }
@rzane
rzane / Field.js
Created November 14, 2017 15:12
<Field /> component that supports nested values for formik
import React from 'react';
import PropTypes from 'prop-types';
import { set, get } from 'lodash/fp';
/**
* This is a copy-pasta job of Formik's Field component. It's needed in
* order to handle nested values. Future versions of formik will support
* this.
*/
@rzane
rzane / fix_callbacks.rb
Created May 18, 2017 20:03
Fix deprecated ActiveModel::Dirty methods for Active Record 5.1
class After < Struct.new(:text)
def call
text.gsub(Model::FIXES[0], 'saved_change_to_\1?')
.gsub(Model::FIXES[1], '\1_before_last_save')
end
end
class Before < Struct.new(:text)
def call
text.gsub(Model::FIXES[0], 'will_save_change_to_\1?')
@rzane
rzane / wait.sh
Created April 4, 2017 17:54
Wait for a port to become available (useful for docker-compose)
#!/bin/bash
if [ "$#" -lt 2 ]; then
echo "Usage: wait-for-it [host] [port] [timeout(optional)]"
exit 1
fi
host="$1"
port="$2"
timeout="${3:-0}"
@rzane
rzane / pg_transfer.rb
Created April 3, 2017 21:27
Transfer everything from one postgres instance to another
#!/usr/bin/env ruby
require 'uri'
require 'delegate'
class PG < SimpleDelegator
FLAGS = ['--verbose', '--clean', '--no-owner', '--no-acl', '-j', '4']
def initialize(url)
super URI.parse(url)
@rzane
rzane / .babelrc
Last active November 22, 2016 17:06
Mobx Examples
{
"presets": [
"es2015"
],
"plugins": [
"transform-decorators-legacy",
"transform-class-properties"
]
}
@rzane
rzane / squeel_test.rb
Last active July 22, 2016 03:31
Test Squeel's behavior when dealing with associated columns
require 'bundler/inline'
require 'minitest/spec'
require 'minitest/autorun'
gemfile true do
source 'https://rubygems.org'
gem 'activerecord', '4.2.6', require: false
gem 'sqlite3'
gem 'squeel', '1.2.3', require: false
end
require 'bundler/inline'
require 'minitest/spec'
require 'minitest/autorun'
gemfile true do
source 'https://rubygems.org'
gem 'activerecord', '~> 5.0.0'
gem 'sqlite3'
gem 'baby_squeel', github: 'rzane/baby_squeel'
end