Skip to content

Instantly share code, notes, and snippets.

@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) {
import {useState} from 'react';
export const requestStatus = {
IDLE: 'IDLE',
PENDING: 'PENDING',
REJECTED: 'REJECTED',
FULFILLED: 'FULFILLED',
};
export const useRequestStatus = () => {
@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>;
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',
@simonsmith
simonsmith / example.js
Created March 16, 2015 00:23
Compiling SUIT with gulp and postcss
var gulp = require('gulp');
var bemLinter = require('postcss-bem-linter');
var atImport = require('postcss-import');
var cssnext = require('cssnext');
var postcss = require('gulp-postcss');
var concat = require('gulp-concat');
var notify = require('gulp-notify');
var stylus = require('gulp-stylus');
gulp.task('css', function() {
@simonsmith
simonsmith / with-feature-detect.js
Last active August 29, 2015 14:07
Flight mixin for adding `is-` SUIT classes to a component based on Modernizr support
module.exports = withFeatureDetect;
function withFeatureDetect() {
'use strict';
this.addFeatureDetectClass = function(feature, supported) {
var prefix = (supported ? 'is-' : 'is-not-');
this.$node.addClass(prefix + feature + '-enabled');
};
@simonsmith
simonsmith / space.styl
Created September 4, 2014 12:41
SUIT CSS spacing utils in stylus
/**
* Spacing utilities
*
* 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
* <div class="u-m25"> // margin: 25px
Function.prototype.bind = (function() {
}).bind || function(b) {
if (typeof this !== "function") {
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
}
function c() {
}
var a = [].slice, f = a.call(arguments, 1), e = this, d = function() {
return e.apply(this instanceof c ? this : b || window, f.concat(a.call(arguments)));
@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
*/