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
// Replace items in `as` with a matching `id` with the | |
// respective item in `bs`, otherwise append new items. | |
const abs = Object.fromEntries([ | |
...as.map(a => [a.id, a]), | |
...bs.map(b => [b.id, b]) | |
]); | |
// The 'trick' is that if you have two entries with the same id | |
// (in this case, implying that the value was updated within `bs`) | |
// then with this method the value of the right-most entry will be used. |
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* getOddPalindromesOutwardsFromCentre(str, index) { | |
let left = index - 1; | |
let right = index + 1; | |
let current = str[index]; | |
yield current; | |
while (left >= 0 && right < str.length && str[left] === str[right]) { | |
current = `${str[left]}${current}${str[right]}`; | |
yield current; |
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 CreateArray<V extends number, Arr extends unknown[] = []> = | |
Arr extends { length: V } ? Arr: CreateArray<V, [...Arr, 1]>; | |
type Add<A extends number, B extends number> = [...CreateArray<A>, ...CreateArray<B>]['length']; | |
type V = Add<2, 3>; | |
type Sub<A extends number, B extends number> = CreateArray<A> extends [...CreateArray<B>, ...infer Els] ? Els['length'] : never; | |
type V2 = Sub<10, 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
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
function* unroll(matrix) { | |
const height = matrix.length; | |
const width = matrix[0].length; | |
for (let y = 0; y < height; y++) { | |
for (let x = 0; x < width; x++) { | |
yield { y, x }; | |
} | |
} | |
} |
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 createGenerator(maybeIterable) { | |
const isGenerator = (maybeGenerator) => | |
"next" in maybeGenerator && | |
typeof maybeGenerator["next"] === "function" && | |
"throw" in maybeGenerator && | |
typeof maybeGenerator["throw"] === "function" && | |
"return" in maybeGenerator && | |
typeof maybeGenerator["return"] === "function"; | |
function* generatorFrom(maybeIterable) { |
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 { | |
useCallback, | |
useEffect, | |
useMemo, | |
useReducer, | |
useRef, | |
useState | |
} from "react"; | |
import { createPortal } from "react-dom"; |
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 { useState } from "react"; | |
import "./styles.css"; | |
function createGrid(n = 3) { | |
return Array(n) | |
.fill(() => Array(n).fill(null)) | |
.map((createRow) => createRow()); | |
} |
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 defaultCreateKey(args) { | |
return JSON.stringify(args); | |
} | |
class LruCache { | |
constructor(capacity = Infinity) { | |
this.capacity = capacity; | |
this.cache = new Map(); | |
} |
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 SinglyLinkedListNode { | |
constructor(value) { | |
this.value = value; | |
this.tail = null; | |
} | |
valueOf() { | |
return this.value; | |
} | |
} |