Skip to content

Instantly share code, notes, and snippets.

Avatar
🐚

Seb Insua sebinsua

🐚
View GitHub Profile
@sebinsua
sebinsua / testUtils.ts
Last active Nov 1, 2021
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);
View nCoding.js
function letterFrequency(str) {
const uniques = {};
for (let char of str) {
uniques[char] = uniques[char] ? ++uniques[char] : 1;
}
return uniques;
}
function letterFrequencyTemplate(uniques) {
@sebinsua
sebinsua / isTriangleString.js
Last active Oct 23, 2020
Check if triangle strings are valid (e.g. abb, abbccc, abbcccdddd, xyyzzz).
View isTriangleString.js
// Check if triangle strings are valid.
//
// e.g.
//
// Valid: abb, abbccc, abbcccdddd, xyyzzz
// Invalid: ab, abbbccc, abbcccddddd, abbccddd
function isTriangleString(str) {
// An empty string or a single character are valid.
if (str.length === 0 || str.length === 1) {
return true;