Skip to content

Instantly share code, notes, and snippets.

View dr-skot's full-sized avatar

Scott Shepherd dr-skot

View GitHub Profile
@dr-skot
dr-skot / useParseQuerySafer.ts
Last active March 18, 2022 16:39
workaround for an issue with @parse/react-ssr 0.0.1.alpha-17 (https://github.com/parse-community/parse-react/issues/81)
import { EncodedParseQuery, useParseQuery } from '@parse/react-ssr';
import usePrevious from '@/hooks/usePrevious';
import { useEffect, useMemo, useRef, useState } from 'react';
import { UseParseQueryResult } from '@parse/react-base';
// this is a workaround for
// https://github.com/parse-community/parse-react/issues/81
const useParseQuerySafer: typeof useParseQuery = (query, options) => {
const didntSyncYet = useRef(false);
@dr-skot
dr-skot / waitUntil.ts
Last active August 31, 2022 17:52
javascript: wait until a condition becomes true
export function waitUntil<T>(condition: () => T, timeout = 10000, interval = 50): Promise<T> {
let result = condition();
if (result) return Promise.resolve(result);
return new Promise((resolve) => {
const finish = () => {
clearInterval(intervalId);
resolve(result);
};
const intervalId = setInterval(() => {
result = condition();
@dr-skot
dr-skot / visit-with-stubs.js
Last active September 10, 2021 12:51
Stub module imports in Cypress
// cypress/support/commands.js
// ...
Cypress.Commands.add('visitWithStubs', (url, stubs) => {
if (!stubs) return cy.visit(url);
cy.visit(url, {
onBeforeLoad(win) {
Object.entries(stubs).forEach(([name, stubFunction]) => {
let target = undefined;
// attach getters and setters for this name on window
Object.defineProperty(win, name, {
@dr-skot
dr-skot / use-async-reducer-example.jsx
Last active May 10, 2022 00:01
React hook like useReducer but can update state asynchronously
// useAsyncReducer works like useReducer, except the reducer has access to the dispatch function
// so it can change state after async operations complete
// example usage:
import useAsyncReducer from './use-async-reducer';
function reducer(state, dispatch, action, args) {
switch (action) {
case 'fetch':
@dr-skot
dr-skot / stopwatch.js
Created April 23, 2021 16:17
Basic javascript stopwatch object
function StopWatch() {
let started = Date.now(),
time = 0,
running = false;
function getTime() {
return running ? Date.now() - started : time;
}
function start() {
@dr-skot
dr-skot / Regex.swift
Last active March 9, 2016 13:02 — forked from ningsuhen/Regex.swift
Swift extension that adds convenient regex methods to String. Forked from ningsuhen & cleaned up for Swift 2.0
import Foundation
struct Regex {
var pattern: String {
didSet { updateRegex() }
}
var expressionOptions: NSRegularExpressionOptions {
didSet { updateRegex() }
}
var matchingOptions: NSMatchingOptions