Skip to content

Instantly share code, notes, and snippets.

Avatar

Babak B. babakness

View GitHub Profile
@vincentriemer
vincentriemer / usePersistedState.js
Created Aug 6, 2019
My take on a type-safe usePersistedState hook, leveraging React suspense/concurrent-mode, DOM custom events, and the kv-storage builtin module.
View usePersistedState.js
/**
* @flow
*/
import * as React from "react";
import { unstable_createResource } from "react-cache";
import * as Scheduler from "scheduler";
import { StorageArea } from "std:kv-storage";
// Creates a ref that tracks the latest value of the argument passed to it
View everything-i-know-about-routing.md

Everything I Know About UI Routing

Definitions

  1. Location - The location of the application. Usually just a URL, but the location can contain multiple pieces of information that can be used by an app
    1. pathname - The "file/directory" portion of the URL, like invoices/123
    2. search - The stuff after ? in a URL like /assignments?showGrades=1.
    3. query - A parsed version of search, usually an object but not a standard browser feature.
    4. hash - The # portion of the URL. This is not available to servers in request.url so its client only. By default it means which part of the page the user should be scrolled to, but developers use it for various things.
    5. state - Object associated with a location. Think of it like a hidden URL query. It's state you want to keep with a specific location, but you don't want it to be visible in the URL.
@KiaraGrouwstra
KiaraGrouwstra / stdlib.ts
Last active Mar 24, 2020
Type-level standard library for TypeScript
View stdlib.ts
// NOTE: code now moved to https://github.com/tycho01/typical
// older revision left here, but it no longer runs well in TS Playground
export type Obj<T> = { [k: string]: T };
export type NumObj<T> = { [k: number]: T };
// export type List = ArrayLike; // no unapplied generic types :(
export type List<T> = ArrayLike<T>;
// progress: https://github.com/Microsoft/TypeScript/issues/16392
export function force<T, V extends T>() {}
@pboyer
pboyer / dynamicArray.ts
Created Feb 12, 2017
A generic, dynamically sized TypedArray in TypeScript
View dynamicArray.ts
interface TypedArray {
readonly length : number;
readonly [n: number]: number;
BYTES_PER_ELEMENT : number;
set(n : ArrayLike<number>, offset : number) : void;
}
interface TypedArrayConstructor<T extends TypedArray> {
new (buffer: ArrayBuffer, byteOffset?: number, length?: number): T;
}
@nybblr
nybblr / 1-easy.js
Last active Jul 13, 2022
3 examples of using Async Generators and Async Iteration in JavaScript!
View 1-easy.js
// Create a Promise that resolves after ms time
var timer = function(ms) {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
};
// Repeatedly generate a number starting
// from 0 after a random amount of time
var source = async function*() {
@odigity
odigity / Firebase Database API Cheatsheet
Last active Jun 9, 2022
Firebase Database API Cheatsheet
View Firebase Database API Cheatsheet
There is no way to store an empty object/array/null value.
There are also no actual arrays. Array values get stored as objects with integer keys.
(If all keys are integers, it will be returned as an array.)
Basically, it's one giant tree of hashes with string keys.
Simply write a value to any location, and the intermediary locations will automatically come into existance.
── Classes ──
DataSnapshot : Container for a subtree of data at a particular location.
@mjackson
mjackson / resolvePromise.js
Last active Sep 9, 2018
An easy way to do async APIs in JavaScript that support both promises *and* callbacks!
View resolvePromise.js
// Here is a function that I use all the time when creating public
// async APIs in JavaScript:
const resolvePromise = (promise, callback) => {
if (callback)
promise.then(value => callback(null, value), callback)
return promise
}
// Sometimes I like to use callbacks, but other times a promise is
@jliuhtonen
jliuhtonen / Reader.elm
Created Apr 13, 2016
Reader monad in Elm for fun and profit
View Reader.elm
module Reader where
type Reader ctx a = Reader (ctx -> a)
unit: a -> Reader any a
unit x =
Reader (\_ -> x)
@Avaq
Avaq / combinators.js
Last active Sep 23, 2022
Common combinators in JavaScript
View combinators.js
const I = x => x
const K = x => y => x
const A = f => x => f (x)
const T = x => f => f (x)
const W = f => x => f (x) (x)
const C = f => y => x => f (x) (y)
const B = f => g => x => f (g (x))
const S = f => g => x => f (x) (g (x))
const S_ = f => g => x => f (g (x)) (x)
const S2 = f => g => h => x => f (g (x)) (h (x))
@lalkmim
lalkmim / codility_solutions.txt
Last active Sep 24, 2022
Codility Solutions in JavaScript
View codility_solutions.txt
Lesson 1 - Iterations
- BinaryGap - https://codility.com/demo/results/trainingU2FQPQ-7Y4/
Lesson 2 - Arrays
- OddOccurrencesInArray - https://codility.com/demo/results/trainingFN5RVT-XQ4/
- CyclicRotation - https://codility.com/demo/results/trainingSH2W5R-RP5/
Lesson 3 - Time Complexity
- FrogJmp - https://codility.com/demo/results/training6KKWUD-BXJ/
- PermMissingElem - https://codility.com/demo/results/training58W4YJ-VHA/