Skip to content

Instantly share code, notes, and snippets.

View etienne-dldc's full-sized avatar

Etienne Dldc etienne-dldc

View GitHub Profile
@etienne-dldc
etienne-dldc / apiFetcher.ts
Created February 16, 2022 15:06
TypeScript Typed Api Fetcher
import { z } from "zod";
type Route<Result, Params> = {
__params: Params;
path: (params: Params) => string;
schema: z.Schema<Result>;
};
type RoutesBase = Record<string, Route<any, any>>;
type Rectangle = {
kind: "rectangle";
width: number;
height: number;
};
type Circle = {
kind: "circle";
radius: number;
};
@etienne-dldc
etienne-dldc / extract-next-routes.ts
Created May 5, 2021 12:22
Extract routes in NextJS project to a TS file
import * as glob from "glob";
import * as path from "path";
import * as fse from "fs-extra";
import * as prettier from "prettier";
const PARAM_REG = /^\[(.+)\]$/;
type PageObj = { [key: string]: PageObj };
type PathItem = { type: "param" | "static"; name: string };
@etienne-dldc
etienne-dldc / pubsub.ts
Created November 1, 2020 14:51
Naive pub/sub
function Subscription() {
const subs: Array<() => void> = [];
return {
subscribe(cb: () => void) {
subs.push(cb);
// return unsub
return () => {
const index = subs.indexOf(cb);
function randomSequenceOfUnique(intermediateOffset: number, seedBase = 1) {
// from https://preshing.com/20121224/how-to-generate-a-sequence-of-unique-random-integers/
let index = permuteQPR(permuteQPR(seedBase) + 0x682f01);
function permuteQPR(x: number) {
const prime = 16777199;
const halfPrime = 8388599;
if (x >= prime) return x; // The 17 integers out of range are mapped to themselves.
// squaring can cause exceeding 2^53
async function fetchAsObjectURL(url) {
const response = await fetch(url);
const blob = await response.blob();
return URL.createObjectURL(blob);
}
fetchAsObjectURL('https://jsonplaceholder.typicode.com/todos/1');
@etienne-dldc
etienne-dldc / Invariants.ts
Created January 21, 2020 10:04
Invariant in TypeScript
function notNull<T>(val: T | null): T {
if (val === null) {
throw new Error(`Invariant: value should not be null`);
}
return val;
}
function notNil<T>(val: T | null | undefined): T {
if (val === null || val === undefined) {
throw new Error(`Invariant: value should not be null | undefined`);
const [start] = useState(Date.now());
const [, setRandomNum] = useState(0);
const diff = Date.now() - start;
useEffect(() => {
const current = diff % 1000;
const time = 1000 - current;
const timer = setTimeout(() => {
setRandomNum(Math.random());
@etienne-dldc
etienne-dldc / config.ejs
Last active January 27, 2019 22:58
CapRover redirect non-www to www
# redirect non www
<%
if (s.publicDomain.match(/^(?!(www\.)).+$/)) {
%>
server {
<%
if (!s.forceSsl) {
%>
listen 80;
<%
@etienne-dldc
etienne-dldc / do_not_reproduce_at_home.ts
Last active July 8, 2021 08:42
A calculator made with Typescript type system only, because why not ¯\_(ツ)_/¯
type Bit = 0 | 1;
type Byte = [Bit, Bit, Bit, Bit, Bit, Bit];
type Decimal =
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
| 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19
| 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29
| 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39
| 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49