Skip to content

Instantly share code, notes, and snippets.

@stephen-james
stephen-james / promise-patterns-loop.js
Created September 26, 2019 20:36
Promise Pattern: Loop helper function to sequentially loop through an array of promises
console.clear();
const loop = (arrayOfPromises, guard) => {
if (arrayOfPromises.length && (!guard || guard(arrayOfPromises[0]))) {
return arrayOfPromises.shift()().then(() => {
return loop(arrayOfPromises, guard);
})
}
return Promise.resolve();
@stephen-james
stephen-james / gist:19be16bb1cf39accecb1e5801167839f
Created October 31, 2018 13:57
Unwrapping Mobx Injected components
const unWrapMobxInjectedComponent = <T extends {}>(component: IWrappedComponent<T>): T => {
let last: any = component;
while (component.wrappedComponent) {
last = component.wrappedComponent;
}
return last as T;
}
@stephen-james
stephen-james / gist:7392dbb0f10b5081e74700c138602294
Created October 31, 2016 13:14
Find and Kill Process using Port
lsof -i tcp:{{port}}
// find PID from list
kill -9 {{PID}}
@stephen-james
stephen-james / material-helper.js
Created March 10, 2016 14:32
Mithril custom elements
// Primitive example of creating a helper library to describe elements
var m = require('mithril');
var materialElements = ['textfield'];
var mt = function(selector, options, children) {
if (selector === 'textfield') {
return m('div.mdl-textfield.mdl-js-textfield.mdl-textfield--floating-label', [
m('input.mdl-textfield__input', {
id: options.id,
@stephen-james
stephen-james / material-helper.js
Created March 10, 2016 14:32
Mithril custom elements
// Primitive example of creating a helper library to describe elements
var m = require('mithril');
var materialElements = ['textfield'];
var mt = function(selector, options, children) {
if (selector === 'textfield') {
return m('div.mdl-textfield.mdl-js-textfield.mdl-textfield--floating-label', [
m('input.mdl-textfield__input', {
id: options.id,
@stephen-james
stephen-james / gist:8003793
Created December 17, 2013 11:49
prototypal inheritance demo
// Crockford Shim
if (typeof Object.create !== "function") {
Object.create = function(superClassFunction) {
function subClassFunction() {}
subClassFunction.prototype = superClassFunction;
return new subClassFunction();
};
}
@stephen-james
stephen-james / mapreduceexample.js
Created November 20, 2013 13:22
Map/Reduce/Finalize in JavaScript calculating a mean
var data = [
["UK", 1000],
["UK", 100],
["UK", 10],
["FR", 2000],
["FR", 200],
["FR", 20],
["GE", 3000],
["GE", 300],
["GE", 30]
@stephen-james
stephen-james / jquery.throttledResize.js
Created October 17, 2013 16:47
throttled jquery resize, borrowing heavily from Underscore.js 's throttle method...
(function($,throttledResizeEvent){
var throttle = function (func, wait) {
var context,
args,
result,
timeout,
previous;
wait = wait || 1000 / 60;
@stephen-james
stephen-james / gothicarch.js
Created October 10, 2013 12:23
gothic arches anyone? answer to a stackoverflow question...
var GothicArchFactory = function() {
var _60degrees = 1.04719755, // radians for Math trig functions
_30degrees = 0.523598776,
_createArch = function(archWidth, archHeight) {
// this private method creates and returns the arch...
// the arch knows it's dimensions and can draw itself on
// a canvas
return {
archWidth : archWidth,
@stephen-james
stephen-james / .gitconfig
Last active December 24, 2015 16:19
using WebStorm as mergetool for command line git (updated to include vsdiffmerge as another option)
[merge]
tool = webstorm
[diff]
tool = webstorm
[difftool "webstorm"]
cmd = webstorm diff $(cd $(dirname "$LOCAL") && pwd)/$(basename "$LOCAL") $(cd $(dirname "$REMOTE") && pwd)/$(basename "$REMOTE")
trustExitCode = false
[mergetool "webstorm"]
cmd = webstorm merge $(cd $(dirname "$LOCAL") && pwd)/$(basename "$LOCAL") $(cd $(dirname "$REMOTE") && pwd)/$(basename "$REMOTE") $(cd $(dirname "$BASE") && pwd)/$(basename "$BASE") $(cd $(dirname "$MERGED") && pwd)/$(basename "$MERGED")
trustExitCode = false