Skip to content

Instantly share code, notes, and snippets.

View alexreardon's full-sized avatar

Alex Reardon alexreardon

View GitHub Profile
@alexreardon
alexreardon / clear-unread-slack-notifications.js
Last active August 21, 2024 23:21
Clear all unread slack notifications
// A simple script to clear all notifications in Slack.
// 1. Open Slack on the web (not in the app as you cannot open the dev tools console)
// 2. Copy paste into your console
(async () => {
function goToNotifications() {
const button = document.querySelector('[data-qa="tab_rail_activity_button"]');
if (!(button instanceof HTMLElement)) {
throw Error('Count not find activities button');
}
button.click();
@alexreardon
alexreardon / dom-rect-polyfill.js
Created June 21, 2024 05:39
DOMRect polyfill
// This file polyfills DOMRect
// DOMRect is currently not polyfilled by jsdom
(() => {
if (typeof window === 'undefined') {
return;
}
if (window.DOMRect) {
return;
}
@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 December 11, 2024 15:14
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;