This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { orderBy } from 'lodash' | |
function chain<OriginalValue>(originalValue?: OriginalValue) { | |
let fns: Array<(val: any) => any> = [] | |
function step<CurrentValue>(currentValue: CurrentValue) { | |
const methods = { | |
do<NextValue>(fn: (val: CurrentValue) => NextValue) { | |
fns.push(fn) | |
return step<Awaited<NextValue>>( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Install brew | |
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" | |
# Install nvm | |
brew install nvm | |
# Install node 18 | |
nvm install node 18 | |
nvm alias default node 18 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class ResponseNotOkError extends Error { | |
constructor(public response: Response) { | |
super(`HTTP ${response.status}`) | |
// These lines can be added to further mimic Error magic properties. | |
// Never needed them, though. | |
this.name = this.constructor.name | |
Object.setPrototypeOf(this, ResponseNotOkError.prototype) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Serializable { | |
toString: () => string; | |
} | |
function groupBy<T>(els: T[], fn: (el: T) => Serializable) { | |
let out: Record<string, T[]> = {}; | |
for (const el of els) { | |
const k = fn(el).toString(); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
My answer to a technical JS question: build your own Promise.all | |
I decided to build my implementation, diyPromiseAll, using typescript | |
and to ensure that all type signatures are the same as the real Promise.all | |
of lib.es2015. | |
Following spec, diyPromiseAll: | |
- accepts any iterable of values of known or unknown size | |
- accepts values of mixed promises and non-promises (and thus nested instances of itself) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fs from 'node:fs' | |
export function fsReadLinesIterator(filepath: string, chunkSize: number) { | |
return { | |
async *[Symbol.asyncIterator](): AsyncGenerator<string> { | |
let line = '' | |
// Read utf8 chars in chunks of chunkSize | |
const stream = fs.createReadStream(filepath, { | |
highWaterMark: chunkSize, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ArticleManager { | |
urls: string[] = [] | |
async *[Symbol.asyncIterator](): AsyncGenerator<[string, string]> { | |
for (const url of this.urls) { | |
const html = await fetch(url).then(resp => resp.text()) | |
yield [url, html] | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function padToFit(x: number, target: number) { | |
const pad = | |
Math.floor(Math.log10(Math.max(1, Math.floor(Math.abs(target))))) + 1 | |
return x.toFixed(0).padStart(pad, '0') | |
} | |
padToFit(4, 9827) // => 0004 | |
padToFit(4, 22) // => 04 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env -S npx ts-node --transpile-only | |
console.log('hi' as string) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fractionalExp = /(?:(?:(\d+)\s+)?(\d)\/(\d))/ | |
const floatExp = /(\d+(?:\.\d+)?)/ | |
const matcherExp = new RegExp( | |
`(${fractionalExp.source}|${floatExp.source})`, | |
'g' | |
) | |
function transformAmounts(str, fn) { | |
return str.replace(matcherExp, (part) => { | |
const fractionalMatch = part.match(fractionalExp) |