Skip to content

Instantly share code, notes, and snippets.

View sebinsua's full-sized avatar
🐚

Seb Insua sebinsua

🐚
View GitHub Profile
class BinaryTreeNode {
constructor(value) {
this.left = null;
this.right = null;
this.values = [value];
}
get value() {
const [firstValue] = this.values;
@sebinsua
sebinsua / Queue.js
Created August 11, 2022 22:40
Implementing a basic FIFO queue using a DoublyLinkedList. See: https://gist.github.com/sebinsua/d0d15ad44b721d5210a6dc1dd3a1bf7e
class QueueItem {
constructor(value) {
this.value = value;
this.head = null;
this.tail = null;
}
valueOf() {
return this.value;
}
class DoublyLinkedListNode {
constructor(value) {
this.value = value;
this.head = null;
this.tail = null;
}
valueOf() {
return this.value;
}
class SinglyLinkedListNode {
constructor(value) {
this.value = value;
this.tail = null;
}
valueOf() {
return this.value;
}
}
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] : ''
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>) {
function defaultCreateKey(args) {
return JSON.stringify(args);
}
class LruCache {
constructor(capacity = Infinity) {
this.capacity = capacity;
this.cache = new Map();
}
type DeepFlatten<T> = T extends Array<infer Item> ? DeepFlatten<Item> : T;
function deepFlatten<T extends any[]>(arr: T): DeepFlatten<T>[] {
function recurseUntilFlat<U extends any[]>(arr: U): U {
return arr.reduce((acc, v) => {
return [...acc, ...Array.isArray(v) ? recurseUntilFlat(v) : [v]]
}, []);
}
return recurseUntilFlat(arr);
function createMultiMap(items, { getKeys } = {}) {
if (typeof getKeys !== "function") {
throw new Error(
"The `getKeys` function passed into `createMultiMap` must be a function."
);
}
function set(m, keys, value) {
if (keys.length === 0) {
return;
// A JavaScript `Date` is fundamentally specified as the number of milliseconds that have elapsed since the ECMAScript epoch,
// which is defined as January 1, 1970, UTC (equivalent to the UNIX epoch).
//
// However, depending on how you construct a `Date` the date passed in can be parsed as a date in your local time zone or a UTC date.
// For instance `"2022-08-01"` will be treated as a UTC value, but `"2022-08-01T00:00:00.000"` will be treated as if it
// is a localised date and offset before setting the underlying value of the `Date`. Similarly, there is a difference
// between passing in `(2022, 7, 1, 0, 0, 0, 0)` which is assumed to be localised and `Date.UTC(2022, 7, 1, 0, 0, 0, 0)`
// which is a unix timestamp (in milliseconds).
// Firstly, let's print our local timezone and the current timezone offset: