Skip to content

Instantly share code, notes, and snippets.

View mxdvl's full-sized avatar
🧭
I love me an SVG

Max Duval mxdvl

🧭
I love me an SVG
View GitHub Profile
@mxdvl
mxdvl / date.ts
Created August 17, 2026 07:45
Immutable `Date` (waiting for `Temporal`)
/** A version of `Date` that prevents inadvertent mutation, for those who can’t wait for `Temporal` */
export type ReadonlyDate = Readonly<Omit<Date, `set${string}`>>;
@mxdvl
mxdvl / fruit-basket.ts
Created August 10, 2026 14:38
Fruit Basket
type FruitBasket = ReadonlySet<'apples' | 'oranges' | 'grapes' | 'cherries'>;
const empty = new Set() satisfies FruitBasket;
const full = new Set([
'apples',
'oranges',
'grapes',
'cherries',
]) satisfies FruitBasket;
@mxdvl
mxdvl / jest.config.mjs
Last active August 18, 2025 09:45
`react-markdown` transforms for Jest
const config = {
transformIgnorePatterns: [
`node_modules/(?!${[
'react-markdown',
'(comma|space)-separated-tokens',
'(hast|mdast|estree|unist)-util-.+',
'bail',
'ccount',
'character-entities',
'decode-named-character-reference',
@mxdvl
mxdvl / declarative_map.ts
Last active July 5, 2023 09:18
Benchmarking declarative & imperative loops
import { Items } from "./items.ts";
export const declarative_map = (items: Items) =>
items.map((item) => {
switch (item.type) {
case "title":
return `# ${item.content}`;
case "body":
return item.content;
case "comment":
@mxdvl
mxdvl / resolveUserAgent.js
Last active February 28, 2023 15:57
Resolve User Agent
/////////////////////////////////////////////////////////////////////////////////
/* UAParser.js v1.1.34
Copyright © 2012-2023 Faisal Salman <f@faisalman.com>
MIT License */ /*
Detect Browser, Engine, OS, CPU, and Device type/model from User-Agent data.
Supports browser & node.js environment.
Demo : https://faisalman.github.io/ua-parser-js
Source : https://github.com/faisalman/ua-parser-js */
/////////////////////////////////////////////////////////////////////////////////
@mxdvl
mxdvl / onward-journeys.mermaid
Created August 31, 2022 09:58
Onward Journeys
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@mxdvl
mxdvl / json.ts
Last active December 8, 2025 15:43
Get a typed JSON from any API
import { go, no, type Result } from "./result.ts";
/**
* Fetch JSON data and parse it safely.
*/
export const fetchJSON = async <T>(
url: Parameters<typeof fetch>[0],
{
headers,
parser,
@mxdvl
mxdvl / options.ts
Last active June 9, 2022 14:06
Type definitions in TS + JS
// the options array exported if you want to loop over the options
const options = ["a", "b", "c"] as const;
// the type which as no runtime overhead at all
type Option = typeof options[number]; // "a" | "b" | "c"
// the predicate for filtering or other runtime checks
const isOption = (option: string): option is Option =>
(options).includes(option as Option)
@mxdvl
mxdvl / client.js
Created May 19, 2022 11:14
D3 & Deno
// @ts-check
import { scaleLinear } from "https://cdn.skypack.dev/d3-scale@4?dts";
import { select } from "https://cdn.skypack.dev/d3-selection@3?dts";
const button = document.querySelector("button");
const list = document.querySelector("ul");
const graph = select(document.querySelector("#graph"));
const [, , width, height] = graph.attr("viewBox").split(" ");
@mxdvl
mxdvl / _public.md
Last active April 27, 2022 10:51
git.io in public repos
  • File PULL_REQUEST_TEMPLATE.md from guardian/content-atom

  • git.io/v7cQshttps://github.com/guardian/content-api/blob/master/docs/adding-a-new-field-to-capi.md/

  • File index.ts from guardian/consent-management-platform

  • git.io/JUmoihttps://github.com/InteractiveAdvertisingBureau/GDPR-Transparency-and-Consent-Framework/blob/master/TCFv2/IAB%20Tech%20Lab%20-%20CMP%20API%20v2.md

  • git.io/JUmw8https://github.com/InteractiveAdvertisingBureau/GDPR-Transparency-and-Consent-Framework/blob/master/TCFv2/IAB%20Tech%20Lab%20-%20CMP%20API%20v2.md