Setup:
npm i memwatch-next pinoUsage:
require('./memory-leak-profiler')| import React from 'react'; | |
| export const preventDefault = ( | |
| handler: React.EventHandler<React.SyntheticEvent>, | |
| ): React.EventHandler<React.SyntheticEvent> => (event) => { | |
| event.preventDefault(); | |
| handler(event); | |
| }; |
| 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]), |
| // 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 |
Setup:
npm i memwatch-next pinoUsage:
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())) | |
| } |
| /* | |
| 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 |
| ### 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) |
| ; 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))))) |
| ; 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)) |