Skip to content

Instantly share code, notes, and snippets.

View justsml's full-sized avatar
🔥
#BLM

Dan Levy justsml

🔥
#BLM
View GitHub Profile
@justsml
justsml / fun-with-bluebird-promise-patterns.js
Last active February 4, 2016 02:15 — forked from domenic/not-bad-code.js
Avoiding explicit promise construction antipattern. Added bluebird.js secret sauce.
// Promises are great, but bluebird covers :allthethings
var Promise = require('bluebird');
var appContext = {ready: false, error: false};
const INVALID_CTX = {ready: false, error: true, message: 'App or User not initialized'};
// Bluebird-ify around `fetch` API
function getUser(username) {return Promise.resolve('users/' + username + '.json').then(fetch);}
function initApp(username) {
// Use bluebird to do some real-world-ish code:
@justsml
justsml / insertAdjacentHTML.js
Last active February 26, 2021 10:24 — forked from eligrey/insertAdjacentHTML.js
insertAdjacentHTML polyfill
/*
* Updated w/ insertAdjacentElement
* @author Dan Levy @justsml
* 2016-06-23
*
* Credit: @lyleunderwood - afterend patch/fix
*
* ```js
* import { insertAdjacentElement } from './libs/insertAdjacentHTML.js';
* Use either:
@justsml
justsml / README.md
Created July 14, 2016 21:42 — forked from joshdover/README.md
Idiomatic React Testing Patterns

Idiomatic React Testing Patterns

Testing React components seems simple at first. Then you need to test something that isn't a pure interaction and things seem to break down. These 4 patterns should help you write readable, flexible tests for the type of component you are testing.

Setup

I recommend doing all setup in the most functional way possible. If you can avoid it, don't set variables in a beforeEach. This will help ensure tests are isolated and make things a bit easier to reason about. I use a pattern that gives great defaults for each test example but allows every example to override props when needed:

// transformy is an object mapping tool to
// transform objects to another object defaults
function transformy({ mutate = {}, input = {}, schema = {}, omit = [], loose = true }) {
return Object.keys(input).map((key) => {
const mutated = {};
const tkey = mutate.hasOwnProperty(key) ? mutate[key] : null;
if (omit.indexOf(key) !== -1) return mutated;
if (typeof input[key] !== 'undefined' && schema.hasOwnProperty(tkey)) {
mutated[tkey] = input[key];
} else if (typeof schema[key] !== 'undefined') {
@justsml
justsml / events.js
Last active October 28, 2016 03:03 — forked from dhigginbotham/events.js
const EventEmitter = require('events');
class Eventry extends EventEmitter {
constructor(type = 'none', ...args) {
super(args);
this.type = type;
}
// overload .on and .emit func to
// support special event syntax
on(ev, fn) {
const makeRequest = () => {
return getJSON()
.then(data => data.needsAnotherRequest
? makeAnotherRequest(data)
: data)
.then(data => console.log(data) || data)
}
@justsml
justsml / PinchZoomPan.js
Last active October 4, 2017 14:09 — forked from iammerrick/PinchZoomPan.js
React Pinch + Zoom + Pan
import React from 'react';
const MIN_SCALE = 1;
const MAX_SCALE = 4;
const SETTLE_RANGE = 0.001;
const ADDITIONAL_LIMIT = 0.2;
const DOUBLE_TAP_THRESHOLD = 300;
const ANIMATION_SPEED = 0.04;
const RESET_ANIMATION_SPEED = 0.08;
const INITIAL_X = 0;
dialog {
position: fixed;
top: 50%;
left: 50%;
right: auto;
padding: 30px;
transform: perspective(500px) translate(-50%, -50%);
background: linear-gradient(to bottom, #FFF, #F4F4F4) #FFF;
border: none;
border-radius: 3px;
@justsml
justsml / bluebird-concurrent-download-using-map-then.js
Last active December 2, 2017 21:29 — forked from joepie91/.js
Promise map (Bluebird)
const Bluebird = require('bluebird')
const got = require('got')
const urls = [
"http://www.google.com/",
"http://www.yahoo.com/",
"http://www.bing.com/",
];
// Bluebird is an extension of promises:
@justsml
justsml / setupOSX.sh
Last active March 21, 2024 09:28 — forked from tylerwalts/setupOSX.sh
This is a bash script to setup Mac OS X defaults on a new mac.
#!/bin/bash
#
# Set up OSX preferences
#
# Updated By: Dan Levy @justsml
#
# Inspired by: https://github.com/mathiasbynens/dotfiles/blob/master/.osx
###########################################
# CONFIG