Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
isotope muffin

Oleg Galaburda burdiuz

💭
isotope muffin
View GitHub Profile
@burdiuz
burdiuz / LRUCache.js
Last active Nov 28, 2021
Simple Least Recently Used Cache implementation
View LRUCache.js
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 Nov 21, 2021
Breadth-first search algorithm to find shortest path
View README.md

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 Nov 28, 2021
Simple Queue implementation via bi-directional linked list
View queue.js
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 Nov 21, 2021
Adjacency matrix implemented using UInt8Array -- every bit represents an edge
View bit-ajacency-matrix.js
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 Nov 13, 2021
Karatsuba multiplication in JS
View karatsuba.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 Nov 1, 2021
React hook that watches for updates and returns element size.
View README.md

@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 Oct 31, 2021
React hook that returns window size and initiates render when window size is being changed.
View README.md

@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 Oct 31, 2021
Function that runs in Promise.race() your promise and a timeout, if timeout completes first, resulting promise rejects with error.
View README.md

@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,
@burdiuz
burdiuz / intercept-calls-to.js
Last active Jun 1, 2021
logAccessTo() returns proxy of an object that logs out access to its members
View intercept-calls-to.js
const interceptCallsTo = (() => {
const pad = (value) => String(value).padStart(2, '0');
const getTime = (date = new Date()) =>
`${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(
date.getSeconds(),
)}.${pad(date.getMilliseconds())}`;
return (obj, props = []) => {
const filtered = props && props.length;
@burdiuz
burdiuz / commandline.html
Last active Jun 6, 2020
Universal debug console for embedded HTML/JavaScript applications
View commandline.html
<!--
This conains an input field for a JS command and output area to displaying whatever your command returns.
To execute command press Enter key or push "Execute" button on the right(if Enter key is being captured).
-->
<div style="width: 100%; display: flex; flex-direction: column; align-items: stretch;">
<div style="display: flex; margin: 5px; align-items: center;">
<input id="console-input" style="font-family: monospace; margin-right: 5px; flex: 1;">
<button id="console-exec">Execute</button>
</div>
<div id="console-output" style="white-space: pre-line; font-family: monospace; margin: 5px; border: 1px solid #000; padding: 5px;">