Skip to content

Instantly share code, notes, and snippets.

View sebinsua's full-sized avatar
🐚

Seb Insua sebinsua

🐚
View GitHub Profile
function maxCall1(fn, n) {
let count = 0;
return function withMaxCall(...args) {
if (count + 1 > n) {
return;
}
count++;
return fn(...args);
};
}
import { z } from "zod";
/**
* Approach 1:
*
* Errors with `"Unrecognized key(s) in object: 'readonlyProperty'"`
*/
const s1 = z
.object({
@sebinsua
sebinsua / withDeepPropertyAccessLogger.ts
Created June 29, 2022 10:24
Debugging the properties in an object which are in use
import DeepProxy from 'proxy-deep';
export function withDeepPropertyAccessEmitter<T extends object>(
object: T,
emit: (info: {
path: (string | symbol)[];
target: T;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
value: any;
}) => void
import React from 'react';
// @ts-ignore
globalThis.whenCalledBy = function whenCalledBy(fnName: string, depth = Infinity) {
const [, , , ...stackTraceLines] = (new Error().stack ?? '')
.split('\n')
.map((line) => {
const matches = /^at (?<name>(?:\w+|<anonymous>)) ?/.exec(line.trim());
return matches ? matches.groups?.name : undefined;
})
type LeftRight = 'left' | 'right';
type LeftRightNet = 'left' | 'right' | 'net';
type ExpandNumericFields<Str> = Str extends `${infer Prefix}_numeric` ? `${Prefix}_numeric_${LeftRightNet}` : Str extends 'weight' ? `weight_${LeftRightNet}` : Str extends string ? `${Str}_${LeftRight}` : never;
type T1 = ExpandNumericFields<'base_numeric'>
type T2 = ExpandNumericFields<'weight'>
type T3 = ExpandNumericFields<'numeric_exposure'>
@sebinsua
sebinsua / safe.js
Last active May 27, 2022 20:55
Tagged template literals that throw
function ordinal(number) {
const englishOrdinalRules = new Intl.PluralRules("en", { type: "ordinal" });
const suffixes = {
one: "st",
two: "nd",
few: "rd",
other: "th"
};
const suffix = suffixes[englishOrdinalRules.select(number)];
@sebinsua
sebinsua / testUtils.ts
Last active June 21, 2022 12:49
Playwright `@testing-library/dom` custom selectors. Contributed to discussion here: https://github.com/hoverinc/playwright-testing-library/pull/330#issuecomment-956348391
import { test, selectors } from '@playwright/test';
import { readFile } from 'fs-extra';
import type * as TestingLibraryDom from '@testing-library/dom';
// This type was copied across from Playwright's repo.
// See: https://github.com/microsoft/playwright/blob/82ff85b106e31ffd7b3702aef260c9c460cfb10c/packages/playwright-core/src/client/types.ts#L108-L117
type SelectorEngine = {
/**
* Returns the first element matching given selector in the root's subtree.
type AtRange = number | [number, number];
const at = <T>(arr: T[], ...ranges: AtRange[]): (T | undefined)[] =>
ranges.flatMap((range) => {
if (typeof range === "number") {
return range >= 0 ? arr[range] : arr[arr.length + range - 1];
}
return range[0] >= 0
? arr.slice(range[0], range[1] + 1)
function insertValue(array, index, value) {
const newArray = [...array];
newArray.splice(index, 0, value);
return newArray;
}
function replaceValue(array, start, end, value) {
const newArray = [...array];
newArray.splice(start, 1 + (end - start), value);
return newArray;
class Node {
constructor(value, left = null, right = null) {
this.value = value;
this.left = left;
this.right = right;
}
find(value) {
let current = this;