Skip to content

Instantly share code, notes, and snippets.

View bguiz's full-sized avatar

Brendan Graetz bguiz

View GitHub Profile
@bguiz
bguiz / RouteListener.js
Last active July 24, 2017 23:14
onUpdate in react-router-4
import { Component } from 'react';
import PropTypes from 'prop-types';
function RouteListenerFactory(plugins = []) {
plugins.forEach((plugin, pluginIdx) => {
if (!plugin.name) {
plugin.name = `routeListenerPlugin#${pluginIdx}`;
}
if (!plugin.initialise) {
@bguiz
bguiz / npm-path-extras.sh
Created August 25, 2016 00:06
See what npm run prepends to the PATH
$ pwd
/Users/bguiz/code/scratch-project
$ echo ${PATH}
/Users/bguiz/.nvm/versions/node/v4.4.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
$ npm run print-my-path # "print-my-path": "echo ${PATH}"
> scratch-project@0.8.2 print-my-path /Users/bguiz/code/scratch-project
> echo ${PATH}
/Users/bguiz/.nvm/versions/node/v4.4.7/lib/node_modules/npm/bin/node-gyp-bin:/Users/bguiz/code/scratch-project/node_modules/.bin:/Users/bguiz/.nvm/versions/node/v4.4.7/bin:/Users/bguiz/.nvm/versions/node/v4.4.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
@bguiz
bguiz / git-dependency-install-and-build.sh
Created August 23, 2016 00:24
Installs a dependency from a git repo using info specified in pakage.json, then builds it
#!/bin/bash
# Installs a dependency from a git repo using info specified in pakage.json, then builds it
: <<"MultilineCommentDelimiter"
Place this file in ./devops/git-dependency/install-and-build.sh
Update package.json to add "config" and a a build step to "scripts"
{
"config": {
@bguiz
bguiz / CSS-Rating-Stars.markdown
Created February 27, 2014 06:57
A Pen by bguiz.
@bguiz
bguiz / catch-doesnt-stop-promise-chain.js
Last active July 1, 2016 05:37
`.catch()` doesn't stop the rest of a promise chain from executing
// returns a promise that always rejects
function myPromise() {
return new Promise((resolve, reject) => {
reject('error in myPromise');
});
}
// promise.catch(a).then(b).catch(c)
myPromise().catch(function a(err) {
console.log('a:', err);
@bguiz
bguiz / braintree-sdk.spec.js
Created June 16, 2016 01:23
Braintree SDK test demo'ing invalid nonces letting TXs through
'use strict';
const co = require('co');
const chai = require('chai');
const expect = chai.expect;
describe.only('[Braintree SDK]', function() {
this.timeout(5000);
@bguiz
bguiz / mocha-async-sequence.spec.js
Last active January 12, 2016 05:25
Proving that mocha executes async tests in sequence (and also shows how to use with generator functions)
'use strict';
let co = require('co');
let chai = require('chai');
let expect = chai.expect;
function waitFor(duration) {
return new Promise(function(resolve) {
setTimeout(resolve, duration);
});
@bguiz
bguiz / workaroundIosFixedPositionBug.js
Created November 22, 2013 03:33
Workaround for bug in iOS5+ where `position: fixed;` behaves incorrectly when virtual keyboard is opened Solution somewhat similar to: http://stackoverflow.com/a/15537846/194982 Presently simply hides/ shows anything with class 'myFixed' (e.g. a header bar), but may be modified to do fancier things like repositioning it manually with JavaScript…
function workaroundIosFixedPositionBug() {
var isIosSafari = /(iPad|iPhone|iPod)/g.test(navigator.userAgent);
if (isIosSafari) {
// workaround for bug in iOS5+ where `position: fixed;` behaves incorrectly when virtual keyboard is opened
// Solution somewhat similar to: http://stackoverflow.com/a/15537846/194982
// Presently simply hides/ shows anything with class 'myFixed' (e.g. a header bar), but may be modified to do
// fancier things like repositioning it manually with javascript, should that be a requirement
$('#main').on("focus", "input, textarea", function() {
$('.myFixed').css('display', 'none');
});
@bguiz
bguiz / silence-alerts-and-console-in-sinon-qunit.js
Last active December 27, 2015 23:39
When running unit tests using sinon-qunit, it can be rather annoying if you have to click through modal alert dialogs, and console errors can create clutter. However, failure scenarios in which these typically occur still need to be tested. Use this function to satisfy both requirements.
/*
* Use this method when we want to silence window alerts and console output within
* a test case.
* Most useful when testing error messages.
*
* Sample usage:
test("should not show any dialogs or output to console", function() {
this.silenceAlertsAndConsole();
window.alert("Hello");
console.log("World");