Skip to content

Instantly share code, notes, and snippets.

@AlexXanderGrib
AlexXanderGrib / user-agent-tokenizer.ts
Created December 20, 2023 17:24
User agent tokenizer
class Token {
constructor(
public readonly name: string,
public readonly version: string,
public readonly tags: string[] = []
) {}
toString() {
let string = `${this.name}/${this.version}`;
@AlexXanderGrib
AlexXanderGrib / event-bus.ts
Created August 29, 2023 00:00
Typed Event Bus
import type { MaybePromiseLike } from "monads-io";
import { parallelMap } from "./parallel.js";
type Handler<T> = (payload: T) => MaybePromiseLike<void>;
const EventType: unique symbol = Symbol("event.type");
export class Event<T = any> {
[EventType]!: T;
package fitnesse.html;
import fitnesse.responders.run.SuiteResponder;
import fitnesse.wiki.*;
public class SetupTeardownIncluder {
private PageData pageData;
private boolean isSuite;
private WikiPage testPage;
private StringBuffer newPageContent;
@AlexXanderGrib
AlexXanderGrib / bind.ts
Created May 18, 2023 05:22
Autobind methods in js
export function bind(class_: Function) {
bindDescriptors(class_);
bindDescriptors(class_.prototype);
}
const autoBindSetup = Symbol("autoBindDone");
function bindDescriptors(object: {}) {
if (autoBindSetup in object) {
return;
@AlexXanderGrib
AlexXanderGrib / zip.ts
Created April 15, 2023 04:40
Python's zip in TS
function* zip<T extends readonly unknown[]>(
...iterables: { [key in keyof T]: Iterable<T[key]> }
): Generator<T, void, void> {
const iterators = iterables.map((iterable) => iterable[Symbol.iterator]());
outer: while (iterators.length > 0) {
const values: unknown[] = [];
for (const iterator of iterators) {
const result = iterator.next();
@AlexXanderGrib
AlexXanderGrib / example.ts
Last active March 23, 2023 17:57
Snowflake ID generator in modern TS with optional stripe-like encoding
const generator = new SnowflakeGenerator({
machineId: 129
});
const encoder = new BigIntEncoder({
entityPrefix: "usr_"
});
const id = generator.generate(); //? => 7044728736365154305
@AlexXanderGrib
AlexXanderGrib / example.ts
Last active February 10, 2023 15:19
Error stack patching
import { wrapCall } from "./wrap.ts";
const call = wrapCall('username=AlexXanderGrib', () => {
throw new Error();
})
call.async().catch(error => console.log(error.stack));
// Error
// at example.ts:4:4
// at username=AlexXanderGrib
@AlexXanderGrib
AlexXanderGrib / either.ts
Created February 1, 2023 20:07
Sweet monads with better error handling
import type {
AsyncMonad,
Alternative,
Container
} from "@sweet-monads/interfaces";
const enum EitherType {
Left = "Left",
Right = "Right"
}
@AlexXanderGrib
AlexXanderGrib / example.js
Created October 5, 2022 20:22
Weird usage of Proxy
const a = new Proxy({ i: 1 }, {
get: (target) => () => target.i++,
})
console.log(a == 1, a == 2, a == 3);
// true true true
const head = <T>(list: T[]) => list[0];
const tail = <T>(list: T[]) => list.slice(1);
const merge =
<T>(a: T[]) =>
(b: T[]) =>
[...a, ...b];
const sizeOf = (arg: { length: number }) => arg.length;
type Fn<A, B> = (arg: A) => B;