Skip to content

Instantly share code, notes, and snippets.

name description
scientific-method
Explore and refine engineering ideas using an iterative scientific method: reason from first principles, analyze relevant code and context, propose candidate ideas, implement coherent change sets, and validate each iteration with the checks required for the task (runtime behavior, tests, and/or types). Use primarily for simplification, refactoring, API design, and debugging, and more generally for uncertain changes where one-shot reasoning is likely to miss better solutions.

Scientific Method

Overview

Use an evidence-driven loop instead of attempting one-shot solutions. Keep iterating through testable attempts until the best solution is found within constraints.

export function formatFormula(formula: string): string {
const INDENT = ' ' // 2 spaces
let out = ''
let depth = 0
/** Stack of the current call-chain. */
const stack: Array<{ fn: string; argCount: number }> = []
/** Helper → current indentation string */
const indent = () => INDENT.repeat(depth)
@gustavopch
gustavopch / firestore-rest.ts
Last active February 29, 2024 23:13
Using Firestore via its REST API.
import { type CacheEntry, cachified } from '@epic-web/cachified'
import { distanceBetween, geohashQueryBounds } from 'geofire-common'
import * as jose from 'jose'
import { ofetch } from 'ofetch'
import { type Primitive } from 'type-fest'
import { env } from './env.js'
type UpdateData<T> = T extends Primitive
? T
@gustavopch
gustavopch / server.ts
Created September 21, 2023 12:14
Remix + Firebase Auth
import { createRequestHandler } from '@remix-run/express'
import { broadcastDevReady } from '@remix-run/node'
import cookie from 'cookie'
import express from 'express'
import { getApp as getAdminApp } from 'firebase-admin/app'
import { getAuth as getAdminAuth } from 'firebase-admin/auth'
import { type FirebaseApp, deleteApp, initializeApp } from 'firebase/app'
import { getAuth, signInWithCustomToken } from 'firebase/auth'
import { LRUCache } from 'lru-cache'
@gustavopch
gustavopch / gesture.ts
Created September 9, 2023 19:04
Gesture handler
export type GestureHandler = (
delta: { panX: number; panY: number; scale: number },
event: PointerEvent,
) => { recalculate?: boolean } | void
export const gesture = (
gestureArea: HTMLElement,
{
activatableArea = gestureArea,
onEvent,
import { Client, SearchClient } from 'typesense'
const TYPESENSE_HOST = ''
const TYPESENSE_PORT = 0
const TYPESENSE_ADMIN_KEY = ''
const TYPESENSE_SEARCH_KEY = ''
const client = new Client({
nodes: [
{
@gustavopch
gustavopch / .eslintrc.js
Last active February 6, 2021 22:22
ESLint + Svelte + TS (start with this template https://github.com/NicoCevallos/svelte-template which contains an important patch to eslint-plugin-svelte3). Kudos to https://github.com/Sxxov.
const { join } = require('path')
const eslintSveltePreprocess = require('./eslint-svelte-preprocess')
module.exports = {
root: true,
env: {
node: true,
browser: true,
},
@gustavopch
gustavopch / README.md
Created May 17, 2020 14:54
TypeScript with project references and incremental build in a monorepo

So, in summary, we have:

packages/
  app/
    tsconfig.json
  shared/
    tsconfig.json
tsconfig.base.json
tsconfig.json
@gustavopch
gustavopch / mongo-cursor-pagination.ts
Created April 29, 2019 14:09
MongoDB cursor pagination
import base64Url from 'base64-url'
import delve from 'dlv'
import { Collection, ObjectId } from 'mongodb'
// @ts-ignore
import * as EJSON from 'mongodb-extjson'
const DEFAULT_LIMIT = 25
type CursorObject = {
readonly id: ObjectId
@gustavopch
gustavopch / get-shared-connection.spec.ts
Last active April 18, 2019 17:25
A function that provides a shared/cached connection to prevent creating new connections every time on Now 2.0
import { MongoClient } from 'mongodb'
import MongoMemoryServer from 'mongodb-memory-server'
import { config } from '../src/config'
import { getSharedConnection } from '../src/infra/database'
import { spinUpInMemoryMongo } from './spin-up-mongo'
let mongod: MongoMemoryServer
beforeAll(async () => {
mongod = await spinUpInMemoryMongo()