Skip to content

Instantly share code, notes, and snippets.

Avatar
🐚

Seb Insua sebinsua

🐚
View GitHub Profile
View clojure.core.js
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) {
View TicTacToe.js
import { useState } from "react";
import "./styles.css";
function createGrid(n = 3) {
return Array(n)
.fill(() => Array(n).fill(null))
.map((createRow) => createRow());
}
View ShoppingCart.js
import {
useCallback,
useEffect,
useMemo,
useReducer,
useRef,
useState
} from "react";
import { createPortal } from "react-dom";
View BinaryTree.js
class BinaryTreeNode {
constructor(value) {
this.left = null;
this.right = null;
this.values = [value];
}
get value() {
const [firstValue] = this.values;
@sebinsua
sebinsua / Queue.js
Created Aug 11, 2022
Implementing a basic FIFO queue using a DoublyLinkedList. See: https://gist.github.com/sebinsua/d0d15ad44b721d5210a6dc1dd3a1bf7e
View Queue.js
class QueueItem {
constructor(value) {
this.value = value;
this.head = null;
this.tail = null;
}
valueOf() {
return this.value;
}
View DoublyLinkedList.js
class DoublyLinkedListNode {
constructor(value) {
this.value = value;
this.head = null;
this.tail = null;
}
valueOf() {
return this.value;
}
View SinglyLinkedList.js
class SinglyLinkedListNode {
constructor(value) {
this.value = value;
this.tail = null;
}
valueOf() {
return this.value;
}
}
View join.ts
type GetTail<Items extends string[]> = Items extends [any, ...infer Tail]
? Tail
: string[];
type HasTail<Items extends string[]> = Items extends [any, any, ...any[]]
? true
: false;
type Join<Strs extends string[], Delimiter extends string = ""> = HasTail<Strs> extends false
? Strs[0] extends string ? Strs[0] : ''
View bring-your-own-form-library.ts
import "./styles.css";
import type { FormEvent } from "react";
function getFormValues(formElement: HTMLFormElement) {
return Object.fromEntries(new FormData(formElement).entries());
}
function Form({ validator = {}, handleSubmit, children }) {
function handleBlurs(e: FormEvent<HTMLFormElement>) {
View memoize-with-lru-cache.js
function defaultCreateKey(args) {
return JSON.stringify(args);
}
class LruCache {
constructor(capacity = Infinity) {
this.capacity = capacity;
this.cache = new Map();
}