Skip to content

Instantly share code, notes, and snippets.

View disintegrator's full-sized avatar

Georges Haidar disintegrator

View GitHub Profile
@disintegrator
disintegrator / ua-quirk.ts
Last active August 12, 2024 10:20
Setting User-Agent header on the browser has surprising results
// Chromium bug: https://issues.chromium.org/issues/40450316
const headers = new Headers();
headers.set("user-agent", "fancy/1.0");
console.log(headers.get("user-agent") == null);
// Prints: false
const req = new Request('');
req.headers.set("user-agent", "fancy/1.0");
console.log(req.headers.get("user-agent") == null);
@disintegrator
disintegrator / urls.ts
Created June 7, 2024 11:12
URL helpers
export function tryParseURL(input: unknown): URL | null {
if (input instanceof URL) {
return input;
}
if (typeof input !== "string") {
return null;
}
try {
return new URL(input);
@disintegrator
disintegrator / discard.go
Created March 4, 2024 10:11
DiscardHandler for log/slog in Go
package log
import (
"context"
"log/slog"
)
// A DiscardHandler discards all log records.
type DiscardHandler struct{}
@disintegrator
disintegrator / assert.go
Last active March 1, 2024 13:04
Assertions in Go
package main
import (
"errors"
"fmt"
"runtime"
)
func Assert(group string, pairs ...any) error {
if len(pairs)%2 != 0 {
@disintegrator
disintegrator / demo.ts
Last active November 10, 2023 15:13
console.group and the `using` keyword
function logGroup(label: string) {
console.group(label)
return {
[Symbol.dispose]: () => {
console.groupEnd()
}
}
}
function demo() {
package functional
import (
"context"
"errors"
"time"
"github.com/benthosdev/benthos/v4/public/service"
)
@disintegrator
disintegrator / sample.test.ts
Last active June 19, 2023 16:52
A sample test helper used to acquire resources and automatically tear them down with the upcoming `using` keyword.
// This is untested code. More of a proof of concept for what's to come...
async function $acquire<
Resource
>(fn: () => Promise<readonly [resource: Resource, teadown?: () => void]>) {
let [resource, teardown = () => {}] = await fn();
return {
resource,
[Symbol.dispose]: teardown,
}
@disintegrator
disintegrator / debounce.go
Created December 23, 2021 18:52
A debouncer that sends pulses on a channel (Golang, Go)
package debounce
import (
"context"
"errors"
"fmt"
"sync"
"time"
)
@disintegrator
disintegrator / typescriptreact.json
Created December 18, 2020 10:11
VS Code snippets
{
"React function component": {
"prefix": "nrf",
"body": [
"import * as React from \"react\";",
"",
"export interface ${1:$TM_FILENAME_BASE}Props {}",
"",
"const ${1:$TM_FILENAME_BASE}: React.FC<${1:$TM_FILENAME_BASE}Props> = props => {",
" return <>{props.children}</>;",
@disintegrator
disintegrator / example_1.html
Created December 11, 2020 15:11
Having Fun with Markdown and Remark
<h1>I am a level 1 heading</h1>
<p class="subtitle subtitle--1">I am a level 1 subtitle</p>
<p class="subtitle subtitle--2">I am a level 2 subtitle</p>
<p class="subtitle subtitle--6">
Up to six levels are supported corresponding to heading levels
</p>