Skip to content

Instantly share code, notes, and snippets.

Avatar
🐚

Seb Insua sebinsua

🐚
View GitHub Profile
View expand.ts
type LeftRight = 'left' | 'right';
type LeftRightNet = 'left' | 'right' | 'net';
type ExpandNumericFields<Str> = Str extends `${infer Prefix}_contribution` ? `${Prefix}_contribution_${LeftRightNet}` : Str extends 'weight' ? `weight_${LeftRightNet}` : Str extends string ? `${Str}_${LeftRight}` : never;
type T1 = ExpandNumericFields<'base_contribution'>
type T2 = ExpandNumericFields<'weight'>
type T3 = ExpandNumericFields<'currency_exposure'>
@sebinsua
sebinsua / safe.js
Last active May 27, 2022
Tagged template literals that throw
View safe.js
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 Jun 21, 2022
Playwright `@testing-library/dom` custom selectors. Contributed to discussion here: https://github.com/hoverinc/playwright-testing-library/pull/330#issuecomment-956348391
View testUtils.ts
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.
View at.js
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)
View insertInterval.js
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;
View bst.js
class Node {
constructor(value, left = null, right = null) {
this.value = value;
this.left = left;
this.right = right;
}
find(value) {
let current = this;
@sebinsua
sebinsua / JsonViewer.js
Last active Mar 2, 2021
Basic JSON Viewer
View JsonViewer.js
import { useCallback, useState } from "react";
const MAXIMUM_DEPTH = 1;
function Node({
value,
depth = 1,
isExpandedDefault = depth <= MAXIMUM_DEPTH
}) {
const [isExpanded, setIsExpanded] = useState(isExpandedDefault);
View valueEquality.ts
import stringify from "fast-json-stable-stringify";
type ImmutablePrimitive =
| undefined
| null
| boolean
| string
| number
| Function;
View threeSum.js
const defaultKey = (arr) => arr.join("");
function createPermute(toKey = defaultKey) {
return function* permute(arr, length = arr.length) {
if (length > arr.length) {
throw new Error(
`The permutation size required (${length}) is greater than the size of the array supplied (${arr.length}).`
);
}
View findIndexOfMatchingParantheses.js
function isOpeningParantheses(char) {
return char === "(" || char === "[" || char === "{" || char === "<";
}
function isClosingParantheses(char) {
return char === ")" || char === "]" || char === "}" || char === ">";
}
function isParantheses(char) {
return isOpeningParantheses(char) || isClosingParantheses(char);