Skip to content

Instantly share code, notes, and snippets.

View simonsmith's full-sized avatar

Simon Smith simonsmith

View GitHub Profile
@simonsmith
simonsmith / hotkeys.md
Created May 16, 2014 11:05
List of Bash hotkeys for Mac

Bash Keyboard Shortcuts - Mac

Enable Option Key as Meta in iTerm. Set as Esc+ - http://stackoverflow.com/a/438892

Moving the cursor

  • Ctrl + a Go to the beginning of the line (Home)
  • Ctrl + e Go to the End of the line (End)
  • Ctrl + p Previous command (Up arrow)
  • Ctrl + n Next command (Down arrow)
@simonsmith
simonsmith / retry.test.ts
Last active January 17, 2024 22:22
Retries a Promise until it resolves or the retries are exceeded
import {retry} from './retry';
jest.spyOn(global, 'setTimeout');
test('retries a function and increases the backoff for each failure', async () => {
const rejectingFunc = jest.fn(
(() => {
let failCount = 5;
return () => {
if (failCount === 0) {
@simonsmith
simonsmith / load-script.ts
Last active August 4, 2021 15:19
Simple script loader
const scripts = new Map<string, Promise<unknown>>();
export function loadScript(
src: string,
additionalScriptAttrs: Record<string, string> = {}
): Promise<unknown> {
if (scripts.has(src)) {
// TypeScript cannot narrow down type with `has`
// https://github.com/microsoft/TypeScript/issues/13086
return scripts.get(src) as Promise<unknown>;
function deparam(str) {
var o = {};
var reg = /\\?([^?=&]+)(=([^&#]*))?/g;
str.replace(reg, function($0, $1, $2, $3) {
if (typeof $3 == 'string') {
o[decodeURIComponent($1)] = decodeURIComponent($3);
}
});
import {useState} from 'react';
export const requestStatus = {
IDLE: 'IDLE',
PENDING: 'PENDING',
REJECTED: 'REJECTED',
FULFILLED: 'FULFILLED',
};
export const useRequestStatus = () => {
@simonsmith
simonsmith / amd-jquery-plugin.js
Last active April 29, 2020 15:28
AMD compatible plugin for jQuery
// UMD dance - https://github.com/umdjs/umd
!function(root, factory) {
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
} else {
factory(root.jQuery);
}
}(this, function($) {
'use strict';
@simonsmith
simonsmith / utils-size.scss
Created June 19, 2014 18:43
Sizing utility classes for Sass
/**
* Spacing classes
*
* Used to override styles on components without need for
* additional modifier classes
*
* Usage:
* <div class="u-mbZ"> // margin-bottom: 0
* <div class="u-mt20"> // margin-top: 20px
*/
const tape = require('tape');
const test = require('tape-css')(tape);
const h = require('hyperscript');
const getStyle = require('computed-style');
const $ = selector => document.querySelector(selector);
const styles = require('./my-component.css');
const dom = () => (
h('div.MyComponent',
module.exports = withAjax;
function withAjax() {
'use strict';
this.get = function(url, options) {
this.ajax(url, options, 'get');
};
this.post = function(url, options) {