Skip to content

Instantly share code, notes, and snippets.

View benbenbenbenbenben's full-sized avatar
🐶

Benjamin Babik benbenbenbenbenben

🐶
View GitHub Profile
type TupleBuilderEx<Constraint = any, Tail extends [...any] = []> = {
items: Tail,
add: TupleBuilderAdd<Constraint, Tail>
}
type TupleBuilderAdd<Constraint, Tail extends [...any] = []> = <Head extends Constraint>(head: Head) => TupleBuilderEx<Constraint, [...Tail, Head]>
const tupleBuilderEx = <Constraint = any, Head extends Constraint = Constraint, Tail extends [...any] = []>(head: Head, tail?: Tail): TupleBuilderEx<Constraint, [...Tail, Head]> => {
return {
@benbenbenbenbenben
benbenbenbenbenben / api.ts
Created March 8, 2024 16:35
mutating vs cloning fluent API
type Value = {
value: number
}
type ValueWithUse = Value & {
use: (x: number) => ValueWithUse
}
// patch a fluent API to a Value object with side effects
const withMutatingUse = (thing: Value): ValueWithUse => {
// exploring vitest behaviour with --pool=forks vs --pool=threads
// spoiler alert: same module, same behaviour - server is only created *once*
import { describe, expect, it } from "vitest";
const server = {
a: 0
}
describe("some module", () => {
type Contract<V, A extends any[]> = (value: V) => (...args: A) => V;
const argsStack: any[] = [];
const Barrier = Symbol();
export const contract = <V, A extends any[]>(
f: Contract<V, A>
): ((...args: A) => V) => {
const handler = (...contractArgs: A) => {
@benbenbenbenbenben
benbenbenbenbenben / linetrim.ts
Last active October 1, 2021 10:05
trim whitespace from start and end of lines in multiline strings in js/ts
// This little function joins strings[0] with the l-r-... zip of strings.slice(1) and expr
// TypeScript
export const linetrim = (strings: TemplateStringsArray, ...expr: string[]): string => {
return strings.slice(1).reduce((str, fragment, i) => str + expr[i] + fragment, strings[0]).replace(/^.*$/gm, line => line.trim() + `\n`)
}
// JavaScript
export const linetrim = (strings, ...expr) => {
return strings.slice(1).reduce((str, fragment, i) => str + expr[i] + fragment, strings[0]).replace(/^.*$/gm, line => line.trim() + `\n`)
@benbenbenbenbenben
benbenbenbenbenben / Dockerfile
Created February 24, 2020 18:42
Dockefile for taiko
FROM node:lts
#USER node
RUN apt-get update && \
apt-get install -y git-all gconf-service libasound2 libatk1.0-0 \
libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 \
libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 \
libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 \
libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 \
libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 \
ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release \
@benbenbenbenbenben
benbenbenbenbenben / object-keys-lowercase.js
Created September 19, 2019 11:57
javascript object keys lowercaser
// lower case process.env variables:
const env = Object.keys(process.env).reduce((k,v) => Object.assign(k, {[v.toLowerCase()]:process.env[v]}), {})
package de.tsenger.u2f;
import javacard.framework.APDU;
import javacard.framework.Applet;
import javacard.framework.ISO7816;
public class AcosJPatch {
public static short getOffsetCdata(APDU apdu) {
byte[] buffer = apdu.getBuffer();
short offset = apdu.getOffsetCdata();
[2018-01-07 12:30:04.731133]: Node starting, version: 9.0
[2018-01-07 12:30:04.732634]: Work pool running 4 threads
[2018-01-07 12:30:04.734634]: Starting bootstrap attempt
[2018-01-07 12:30:04.735134]: Bootstrap stopped because there are no peers
[2018-01-07 12:30:04.735134]: Exiting bootstrap attempt
[2018-01-07 12:30:12.761601]: UPnP local address: , discovery: 0, IGD search: 3
[2018-01-07 12:30:12.762104]: UPnP device url: http://192.168.1.1:1990/d4311ffc-9c6f-4028-89f7-c49bd6e7e090/WFADevice.xml st: upnp:rootdevice usn: uuid:abebceb4-eb14-2a04-c1c4-bc92c91503fb::upnp:rootdevice
[2018-01-07 12:30:12.769102]: UPnP TCP mapping verification response: -1, external ip response: -1, external ip: , internal ip: 0.0.0.0, remaining lease:
[2018-01-07 12:30:12.777111]: UPnP UDP mapping verification response: -1, external ip response: -1, external ip: , internal ip: 0.0.0.0, remaining lease:
[2018-01-07 12:30:12.780110]: UPnP TCP port mapping response: -4, actual external port
// get a function's parameters as [...string]
(function() {
if (Function.prototype.parameters)
return;
Object.defineProperty(Function.prototype, "parameters", { get: function() {
var pattern = /^(?:(?:(?:function\s+[\w_\$][\w\d_\$]*)|function\s*|[\w_\$][\w\d_\$]*)(?:\(|[\w_\$])|\(|)(\s*(([\w_\$][\w\d_\$]*)([,\s]*)?)*)/gim
var matches = pattern.exec(this.toString())
return matches[1].split(',').map(s => s.trim()).filter(s => s.length)
}})
})()