Skip to content

Instantly share code, notes, and snippets.

View burdiuz's full-sized avatar
💭
isotope muffin

Oleg Galaburda burdiuz

💭
isotope muffin
View GitHub Profile
import { forwardRef } from "react";
import { HorizontalAlignment, VerticalAlignment } from "./alignments";
import { getAlignmentClassNames } from "./useAlignmentClassNames";
const PopoverAlignment = (
{
children = () => null,
horizontal = HorizontalAlignment.LEFT,
vertical = VerticalAlignment.BOTTOM,
},
@burdiuz
burdiuz / ByteArray.js
Last active May 29, 2022 13:08
Simple wrapper over Uint8Array with a playhead
class ByteArray {
constructor(buffer, initPosition = 0) {
this.buffer = typeof buffer === "number" ? new Uint8Array(buffer) : buffer;
this.position = initPosition;
}
get bytesAvailable() {
return this.buffer.length - this.position;
}
@burdiuz
burdiuz / LRUCache.js
Last active November 28, 2021 07:29
Simple Least Recently Used Cache implementation
const getNextIndex = (() => {
let index = 1;
const lastSafe = ((1 << 31) >>> 0) - 1;
return () => {
index += 1;
if (index >= lastSafe) {
index = 1;
}
@burdiuz
burdiuz / README.md
Last active November 21, 2021 16:00
Breadth-first search algorithm to find shortest path

BFS applied to a grid to find shortest path

Queue implementation used here is published as separate gist queue.js.

When loaded it draws path from top left to bottom right corner. White cells are empty and black -- walls. Clicking on any cell inverts it(empty > wall and wall > empty) and forces path redraw. For now or recreates Adjacency List every time. Sometimes it cannot find path from start to the end, in this case just refresh page.

@burdiuz
burdiuz / queue.js
Last active November 28, 2021 07:23
Simple Queue implementation via bi-directional linked list
class QueueNode {
constructor(value, prev = null, next = null) {
this.value = value;
this.insert(prev, next);
}
insert(prev, next) {
this.prev = prev;
this.next = next;
@burdiuz
burdiuz / bit-ajacency-matrix.js
Last active November 21, 2021 09:42
Adjacency matrix implemented using UInt8Array -- every bit represents an edge
const getPosition = (rowLength) => (col, row) => row * rowLength + col;
const getSlotIndex = (rowLength) => {
const pos = getPosition(rowLength);
return (col, row) => pos(col, row) >> 3;
};
const getInSlotPosition = (rowLength) => {
const pos = getPosition(rowLength);
return (col, row) => pos(col, row) % 8;
@burdiuz
burdiuz / karatsuba.js
Created November 13, 2021 17:25
Karatsuba multiplication in JS
const karatsuba = (x, y, l) => {
if (l <= 2) {
return x * y;
}
const l_2 = l / 2n;
const half = 10n ** l_2;
const a = x / half;
const b = x % half;
@burdiuz
burdiuz / README.md
Last active November 1, 2021 15:15
React hook that watches for updates and returns element size.

@actualwave/use-element-size

React hook that watches for changes in element size using ResizeObserver and reports updates forcing component to render. There are two hooks in this package

  • useResizeObserver(handlerFn, elementRef) -- Calls handlerFn() when element size changed with instance of DOMRectReadOnly.
  • useElementSize(elementRef, reinitInEffect = true) -- Returns element size or null if elementRef.current does not contain reference to HTMLElement. reinitInEffect forces immediate re-render to apply element sizes, expected to have a valid reference at the time of re-render.

Example on codesandbox.io.

@burdiuz
burdiuz / README.md
Last active October 31, 2021 22:11
React hook that returns window size and initiates render when window size is being changed.

@actualwave/use-window-size

This package is more of an experiment, if you need such hook, better look for alternatives.

React hook useWindowSize() that returns Window's innerWidth and innerHeight properties, and initiates render when window is being resized. It uses single "resize" listener and relies on WeakSet and WeakRef, so be sure you can use it. Here is an example on CodeSandbox.

@burdiuz
burdiuz / README.md
Last active October 31, 2021 22:01
Function that runs in Promise.race() your promise and a timeout, if timeout completes first, resulting promise rejects with error.

@actualwave/resolve-or-timeout

Function that runs in Promise.race() your promise and a timeout, if timeout completes first, resulting promise rejects with error.

export const resolveOrTimeout = <T = unknown>(
  
  // Promise or a function that will be passed to a Promise object
  promiseOrHandler:
    | Promise<T>
    | ((
        resolve: (data: T) => void,