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 * as z from 'zod' | |
function zJson <T extends z.ZodTypeAny> (schema: T) { | |
return z.string().transform((val, ctx) => { | |
try { | |
return JSON.parse(val) | |
} catch (err) { | |
ctx.addIssue({ | |
code: z.ZodIssueCode.custom, | |
message: 'Invalid JSON', |
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
type User = { | |
name: string | |
age: number | |
} | |
const users: User[] = [ | |
{ name: 'Adibou', age: 30 }, | |
{ name: 'Bounda', age: 69 }, | |
{ name: 'Adibou', age: 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 function createStructuredError< | |
ErrorCodes extends Record<any, (data: any) => string> | |
>(name: string, codes: ErrorCodes) { | |
return class StructuredError< | |
ErrorCode extends keyof ErrorCodes, | |
ErrorData extends Parameters<ErrorCodes[ErrorCode]>[0] | |
> extends Error { | |
constructor( | |
public readonly code: ErrorCode, | |
public readonly data: ErrorData |
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 Emitter<Events extends Record<any, (val: any) => any>> { | |
private observers: Map<keyof Events, Set<(val: any) => any>> = new Map() | |
on<Name extends keyof Events, Fn extends Events[Name]>(name: Name, fn: Fn) { | |
if (!this.observers.has(name)) { | |
this.observers.set(name, new Set()) | |
} | |
this.observers.get(name)!.add(fn) |
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
# Info: https://certbot.eff.org/#ubuntuxenial-nginx | |
sudo apt-get update | |
sudo apt-get install letsencrypt | |
# Generate certificate | |
letsencrypt certonly --standalone -d example.com -d www.example.com | |
# Verify that four file have been created | |
sudo ls -l /etc/letsencrypt/live/example.com |
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
def get_indefinite_article (noun_phrase) | |
chars = 'aedhilmnorsx'.split('') | |
regexp1 = /(?!FJO|[HLMNS]Y.|RY[EO]|SQU|(F[LR]?|[HL]|MN?|N|RH?|S[CHKLMNPTVW]?|X(YL)?)[AEIOU])[FHLMNRSX][A-Z]/ | |
regexp2 = [/^e[uw]/, /^onc?e\b/, /^uni([^nmd]|mo)/, /^u[bcfhjkqrst][aeiou]/] | |
regexp3 = /^U[NK][AIEO]/ | |
regexp4 = /^y(b[lor]|cl[ea]|fere|gg|p[ios]|rou|tt)/ | |
m = /\w+/.match noun_phrase | |
return "an" unless m |