Skip to content

Instantly share code, notes, and snippets.

View leontastic's full-sized avatar

Leon Li leontastic

View GitHub Profile
@leontastic
leontastic / preventDefault.ts
Created November 29, 2023 15:12
helpful utility to handle `event.preventDefault` in react
import React from 'react';
export const preventDefault = (
handler: React.EventHandler<React.SyntheticEvent>,
): React.EventHandler<React.SyntheticEvent> => (event) => {
event.preventDefault();
handler(event);
};
@leontastic
leontastic / promise.ts
Last active November 21, 2023 00:30
TS promise utils
export type PromiseFromProps<PromiseMap> = Promise<
{
[Property in keyof PromiseMap]: Awaited<PromiseMap[Property]>;
}
>;
export const fromProps = async <T>(object: T): PromiseFromProps<T> => {
// invert the object so that we get an array of promises resolving to key-value entries
const promises = Object.entries(object).map(([property, promise]) =>
Promise.resolve(promise).then((result) => [property, result]),
@leontastic
leontastic / memoizeOne.ts
Created March 29, 2019 09:24
memoizer that only remembers the most recent input
// memoizer that only remembers the most recent input
const memoizeOne = <Arguments extends any[], Output = any>(
fn: (...args: Arguments) => Output,
resolver = (...args: Arguments) => args[0],
) => {
let resolution: any;
let value: Output;
return (...args: Arguments) => {
const newResolution = resolver(...args);
if (!resolution || resolution !== newResolution) {
// ask your user questions on the command line in sequence
import readline from "readline";
let answer: Promise<string>;
const askQuestion = (question: string) =>
new Promise<string>(resolve => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
@leontastic
leontastic / README.md
Last active December 18, 2017 00:53
Memory leak profiler using `memwatch-next` and `pino`

Setup:

npm i memwatch-next pino

Usage:

require('./memory-leak-profiler')
import { EventEmitter } from 'events'
import { assign, assignIn, forEach, set, unset } from 'lodash'
export default class FirebaseReplicator extends EventEmitter {
static all (replicators) {
const emitter = new EventEmitter()
const start = () => {
return Promise.all(replicators.map((rep) => rep.start()))
}
@leontastic
leontastic / fraction.js
Last active November 28, 2016 03:15
Fraction: an ES6 class to preserve decimal precision in rational number arithmetic
/*
Fraction: an ES6 class to preserve decimal precision in rational number arithmetic
Notes:
- reliably preserves decimal precision for floats that can be represented in decimal with 16 decimal places or less
- for decimals with more than 16 decimal places, precision is preserved up to 16 decimal places
CONSTRUCTOR:
constructor([a|Number|Fraction]) => [Fraction]: returns a Fraction from a number
@leontastic
leontastic / a3q2doc
Last active August 29, 2015 14:07
Improved documentation for the commands to be used in the test harness for A3Q2 (CS 246)
### STRING CONCATENATION
s destination source1 source2 // + - [[ MUTATE ]] concatenate two sources
t destination source1 string // + - [[ MUTATE ]] concatenate a source followed by a specified string
### FIND
/ source1 source2 // / - [[ PRINT ]] print number of times source2 appears in source1
% destination source1 source2 // % - [[ MUTATE ]] removes occurrences of source2 in source1
### MEMORY MANAGEMENT
f destination // default constructor - [[ CREATE ]] create empty iString (basically, empty string)
@leontastic
leontastic / factors.rkt
Created January 26, 2014 22:58
A clone of the Racket "divisors" function available in the "math/number-theory" module.
; factors: Int -> (listof Int)
; (factors a) produces a list of all positive factors of a, including 1 and itself.
(define (factors a)
(define b (abs a))
(filter (lambda (n) (= (remainder b n) 0))
(build-list b (lambda (y) (+ 1 y)))))
@leontastic
leontastic / remove-duplicates-clone.rkt
Created January 19, 2014 09:36
A clone of the *remove-duplicates* built-in function in Racket. Uses *equal?* to determine if two elements are duplicates.
; remove-duplicates-clone: (listof Any) -> (listof Any)
; (remove-duplicates-clone loa) produces loa without duplicated elements while preserving the order of elements
(define (remove-duplicates-clone loa)
(foldr (lambda (x y) (cons x (filter (lambda (z) (not (equal? x z))) y))) empty loa))