Skip to content

Instantly share code, notes, and snippets.

class FindAndPopCollection<T> {
private _array: T[];
constructor(_array: T[]) {
this._array = _array.slice();
}
findAndRemove(predicate: (value: T) => boolean) {
const index = this._array.findIndex(predicate);
if (index !== -1) {
import { convertLongMonthDayToShort } from "server/domain/client/convertLongMonthDayToShort";
describe("convertLongMonthDayToShort", () => {
it.each([
[undefined, undefined],
[null, null],
["1/1", "1/1"],
["8/31", "8/31"],
["3/31", "3/31"],
["4/30", "4/30"],
@reyronald
reyronald / 1 toBeAriaDisabled.ts
Last active September 7, 2023 14:07
toBeAriaDisabled
import type { MatcherFunction } from "expect";
function assertIsElement(el: unknown): asserts el is HTMLElement {
const isElement = el instanceof HTMLElement;
if (!isElement) {
throw new TypeError("Not an HTMLElement");
}
}
export const toBeAriaDisabled: MatcherFunction<[element: unknown]> = function (element) {
@reyronald
reyronald / getNockPayloadCapturer.sample.ts
Last active August 24, 2023 19:53
getNockPayloadCapturer.ts
const mockServer = nock("http://localhost", { allowUnmocked: false });
mockServer.put(`/api/clients/${client.id}/bills/bulk`).reply(200, bills);
const getCapturedPayload = getNockPayloadCapturer(mockServer);
await userEvent.click(screen.getByRole("button", { name: "Save" }));
const capturedPayload = await waitFor(getCapturedPayload);
// https://twitter.com/GabrielVergnaud/status/1622899119856525313
type Prettify<T> = {
[K in keyof T]: T[K]
} & {}
type ValidateShape<T, Shape> =
T extends Shape ?
Exclude<keyof T, keyof Shape> extends never ?
T : never : never;
type Exact<A, B> =
A extends B ?
B extends A ? A
: never
: never;
import React from "react";
import { BrowserRouter, Redirect } from "react-router-dom";
import { CompatRouter, Routes, Route } from "react-router-dom-v5-compat";
import { Internal1 } from "./Internal1";
import { Loadable } from "./Loadable";
const Loading = () => (
<div style={{ width: "100%", height: "100vh", background: "red" }}>Loading</div>
);
import { useRef, useState, useEffect } from "react";
export function useIntersectionObserver<THTMLElement extends HTMLElement>(
options?: IntersectionObserverInit,
) {
const ref = useRef<THTMLElement>(null);
const [entry, setEntry] = useState<IntersectionObserverEntry | undefined>();
const optionsRef = useRef(options);
import { useEffect } from "react";
function useEmulateMediaPrint() {
useEffect(() => {
const modifiedCSSRules: Map<CSSMediaRule, string> = new Map();
for (const styleSheet of document.styleSheets) {
for (const cssRule of styleSheet.cssRules) {
if (cssRule instanceof CSSMediaRule) {
@reyronald
reyronald / moveCursorWithinInput.js
Created April 15, 2022 16:05
This one moves the user's cursor within a text input.
export function moveCursorWithinInput(input, position) {
if (input.setSelectionRange) {
input.focus();
input.setSelectionRange(position, position);
} else if (input.createTextRange) {
var range = input.createTextRange();
range.collapse(true);
range.moveEnd('character', position);
range.moveStart('character', position);
range.select();