Skip to content

Instantly share code, notes, and snippets.

View alexreardon's full-sized avatar

Alex Reardon alexreardon

View GitHub Profile
@alexreardon
alexreardon / drag-event-polyfill-tests.spec.ts
Created December 18, 2023 00:00
DragEvent polyfill tests
/* data-transfer-item-list.spec.ts */
import invariant from 'tiny-invariant';
test('add(data, format) should add a string item', done => {
const list = new DataTransferItemList();
list.add('Hello world', 'text/plain');
const item: DataTransferItem = list[0];
@alexreardon
alexreardon / drag-event-polyfill.js
Last active February 23, 2024 03:22
DragEvent polyfill for jsdom
// This file polyfills DragEvent for jsdom
// https://github.com/jsdom/jsdom/issues/2913
// This file is in JS rather than TS, as our jsdom setup files currently need to be in JS
// Good news: DragEvents are almost the same as MouseEvents
(() => {
if (typeof window === 'undefined') {
return;
}
@alexreardon
alexreardon / helpers.ts
Created October 16, 2023 02:32
Reimplementing function type helpers
function add(a: number, b: number): number {
return a + b;
}
type MyParameters<TFunc extends (...args: any[]) => unknown> = TFunc extends (
...args: infer TArgs
) => unknown
? TArgs
: unknown;
@alexreardon
alexreardon / scroll-by-ponyfill.js
Last active October 24, 2023 22:15
Element.prototype.scrollBy ponyfill (for testing)
// This file polyfills `Element.prototype.scrollBy`
// scrollBy(x-coord, y-coord)
// scrollBy(options)
(() => {
if (typeof Element === 'undefined') {
return;
}
if (typeof Element.prototype.scrollBy !== 'undefined') {
return;
}
const interactions: number[][] = [];
function sum(values: number[]) {
return values.reduce((acc, current) => acc + current, 0);
}
function average(values: number[]): number {
if (!values.length) {
return 0;
}
export {};
const allCollectedFps: number[][] = [];
function sum(values: number[]) {
return values.reduce((acc, current) => acc + current, 0);
}
function average(values: number[]): number {
if (!values.length) {
function average(values: number[]): number {
const sum = values.reduce((acc, current) => acc + current, 0);
return sum / values.length;
}
export function standardDeviation(values: number[]): number {
/** https://www.mathsisfun.com/data/standard-deviation-formulas.html
* 1. Work out the Mean (the simple average of the numbers)
* 2. Then for each number: subtract the Mean and square the result
* 3. Then work out the mean of those squared differences.
@alexreardon
alexreardon / wait-for-frame.ts
Created March 21, 2023 00:19
a function that lets you wait for animation frames to be called before executing a function
function waitForFrames({
frames,
done,
}: {
frames: number;
done: () => void;
}): () => void {
let frameId: number | null = null;
let remainingFrames = frames;
@alexreardon
alexreardon / fix-post-drop-pointer-bug.ts
Last active April 12, 2024 16:03
Fix Chrome + Safari bug where the element under where the user started dragging (on the viewport) is entered into by the browser
import { bindAll } from 'bind-event-listener';
import type { DragLocation } from '../internal-types';
type CleanupFn = () => void;
/** Set a `style` property on a `HTMLElement`
*
* @returns a `cleanup` function to restore the `style` property to it's original state
*/
@alexreardon
alexreardon / how-this-works.md
Created August 7, 2022 00:29
How `this` works in javascript

this binding

  • this is the runtime context of a function.
  • this is determined by the call site
  • the same function can be executed with different this runtime contexts. You can think of this as another arguement to the function
  • Comparison: scopes are generally defined at compile time (exception: eval)
const person = {
 name: 'Alex',