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 / 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
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 August 24, 2022 16:40
Get a typed JSON from any API
export const fetchJSON = async <T>(
url: Parameters<typeof fetch>[0],
{
headers,
parser,
}: {
headers?: Record<string, string>;
parser: (data: unknown) => T | Promise<T>;
}
): Promise<T> => {
@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

@mxdvl
mxdvl / bookmarklet
Last active May 4, 2022 14:18
Ophan Component Viewer
javascript:(function()%7Bfetch(%22https%3A%2F%2Fgist.githubusercontent.com%2Fmxdvl%2Fb17f344678ed34d45683896e208ecdc1%2Fraw%2Fophan-components.css%22).then(r%20%3D%3E%20r.text()).then(css%20%3D%3E%20%7B%20const%20style%20%3D%20document.createElement(%22style%22)%3B%20style.innerHTML%20%3D%20css%3B%20document.body.appendChild(style)%3B%20%7D)%3B%7D)()%3B
@mxdvl
mxdvl / non-blocking-document.createElement.html
Last active January 22, 2024 23:16 — forked from sndrs/non-blocking-document.write.html
Override document.createElement('script') to prevent 3rd parties injecting blocking scripts
<!DOCTYPE html>
<html>
<head>
<title>Prevent non-async script</title>
<script>
(function(Document) {
const trueCreate = document.createElement.bind(document);
Document.prototype.createElement = function(tag, options) {
const script = trueCreate(tag, options);
@mxdvl
mxdvl / git.zsh
Last active July 21, 2023 19:20
Useful CLI commands
function git_remove_gone_branches() {
# from https://stackoverflow.com/a/61935602/1577447, except works with squash & merge strategy.
git fetch -p
git branch -vv | grep ': gone]' | awk '{print $1}' | xargs git branch -D
}