This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function maxCall1(fn, n) { | |
let count = 0; | |
return function withMaxCall(...args) { | |
if (count + 1 > n) { | |
return; | |
} | |
count++; | |
return fn(...args); | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { z } from "zod"; | |
/** | |
* Approach 1: | |
* | |
* Errors with `"Unrecognized key(s) in object: 'readonlyProperty'"` | |
*/ | |
const s1 = z | |
.object({ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node { | |
constructor(value, left = null, right = null) { | |
this.value = value; | |
this.left = left; | |
this.right = right; | |
} | |
find(value) { | |
let current = this; |