Skip to content

Instantly share code, notes, and snippets.

@loucadufault
loucadufault / pack.ts
Last active January 7, 2022 23:03
Recipe for adding per-user authentication for Google APIs to a Coda pack
import * as coda from "@codahq/packs-sdk";
export const pack = coda.newPack();
pack.setUserAuthentication({
type: coda.AuthenticationType.OAuth2,
authorizationUrl: "https://accounts.google.com/o/oauth2/v2/auth", // see https://developers.google.com/identity/protocols/oauth2/web-server#creatingclient
tokenUrl: "https://oauth2.googleapis.com/token", // see https://developers.google.com/identity/protocols/oauth2/web-server#exchange-authorization-code
scopes: [
// add any scopes your project requires to access user data from their Google Account
@loucadufault
loucadufault / database.js
Created January 13, 2022 02:23
A simple pattern for opening a mongoose connection to a MongoDB database while registering listeners on the connection and handling the asynchronous result of opening the connection.
import mongoose from "mongoose";
function getConnection(databaseURL, connectionListeners) {
return mongoose.connection;
}
async function openConnection(databaseURL, options) {
await mongoose.connect(databaseURL, options);
}
@loucadufault
loucadufault / example.spec.js
Last active January 30, 2022 20:51
Jest expect matcher extension for asserting whether the received object has only the properties specified via a list to the matcher
test('received response only exposes the public fields', () => {
const res = await request
.post('/items')
.send(newItem)
.expect(status.CREATED);
expect(res.body).toOnlyHaveProperties(["id", "name", "color"]);
});
test('returned object is extended', () => {
@loucadufault
loucadufault / toDTO.plugin.js
Last active August 28, 2024 23:59
Mongoose model plugin to convert documents to a JSON or DTO representation (e.g. for a ReST API), according to a customizable DTO definition that enables mapping from other properties and transforming property values.
// DTO assembler
export const toDTO = (DtoDef) => (schema) => {
let transform;
// preserve the previously defined transform, if any
if (schema.options.toJSON && schema.options.toJSON.transform) {
transform = schema.options.toJSON.transform;
}
const assembleDTO = (doc) =>
@loucadufault
loucadufault / archive_repo_state.sh
Created October 20, 2022 08:19
Bash script to snapshot current state of working directory, then make a single (squashed) commit of that snapshot in another branch for archival
#!/bin/bash
# Script to take a snapshot of current state of files in working directory, then make a single commit of that snapshot in another branch for archival
ARCHIVE_BRANCH=release
# see https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History#pretty_format
GIT_LOG_PRETTY_FORMAT_STRING="%h -%d %an <%ae> - %s (%ad)"
# e844ce9 - (HEAD -> foo, origin/myBranch, myBranch) Louca Dufault <[email protected]> - commit msg (2022-10-20)
# ensure inside git repo
git rev-parse --is-inside-work-tree 1> /dev/null
@loucadufault
loucadufault / Timer.ts
Created October 26, 2022 07:52
TypeScript timer/timing utilities for conveniently profiling code, for example to record the time taken by various function sections
export class Timer {
private static nextTimerIndex = 0;
public readonly timerIndex;
private startTime: number | undefined;
private endTime: number | undefined;
constructor(startOnInit: boolean = false) {
this.timerIndex = Timer.nextTimerIndex++;
if (startOnInit) this.start();
@loucadufault
loucadufault / array.utils.ts
Last active October 29, 2022 21:08
Collection of various utils for operating on JavaScript / TypeScript objects and arrays in ES6 with strong typing
function countItemFrequency<T extends string | number | symbol>(l: T[]): Record<T, number> {
const occurrences = {} as Record<T, number>;
l.forEach(i => {
if (!occurrences.hasOwnProperty(i)) {
occurrences[i] = 0;
}
occurrences[i]!++;
});
return occurrences;
}
@loucadufault
loucadufault / notes.md
Created November 5, 2022 06:17
Overlays three side-by-side translucent rectangles in HTML that can be colored differently with CSS to visualize how those colors appear once overlayed.
@loucadufault
loucadufault / getReservedNoiseSpace.js
Created April 30, 2023 16:36
A pattern for emulating multiple seeded gradient noise functions with a single seeded function
let globalNoiseOffset = 0
function getReservedNoiseSpace(reservedSize=1) {
const ownNoiseOffset = globalNoiseOffset
globalNoiseOffset += reservedSize + 1 + Math.random() // introduce a (random) buffer of size at least 1 to prevent continuity between spaces
return (...args) => noise(args.map(arg => arg + ownNoiseOffset)) // e.g. https://p5js.org/reference/#/p5/noise
}