Skip to content

Instantly share code, notes, and snippets.

View jonasgeiler's full-sized avatar
:shipit:
hello there

Jonas Geiler jonasgeiler

:shipit:
hello there
View GitHub Profile
@jonasgeiler
jonasgeiler / jab.ts
Last active December 25, 2023 16:24
A small try/catch alternative utility function that simplifies error handling with promises (works a bit like Rust's error handling)
export const jab = <T>(v: T) => Promise.allSettled([ v ]).then(([ r ]) => r)
@jonasgeiler
jonasgeiler / xml-stringify.ts
Created December 17, 2023 16:20
Super basic implementation of an XML equivalent of JSON.stringify in JavaScript/TypeScript.
const XML = { stringify: (value: any, tag: string) => `<${tag}>` + Object.entries(value).reduce((p, [k, v]) => p + (v && typeof v === 'object' ? XML.stringify(v, k) : `<${k}>${v}</${k}>`), '') + `</${tag}>` };
@jonasgeiler
jonasgeiler / bench.lua
Last active April 15, 2023 14:00
A super simple benchmark function in Lua
---Run a benchmark
---@param fn function
---@param outer_iter integer?
---@param inner_iter integer?
local function bench(fn, outer_iter, inner_iter)
outer_iter = outer_iter or 100
inner_iter = inner_iter or 10000
local times = {} ---@type number[]
for i = 1, outer_iter do
@jonasgeiler
jonasgeiler / fetch-timeout.ts
Last active December 17, 2023 16:37
A simple fetch() wrapper with timeout
/**
* A simple fetch() wrapper with timeout.
* @param input - Same as `fetch()`.
* @param init - Same as `fetch()`, but with an extra option `timeout` (defaults to no timeout).
* @returns - Same as `fetch()`.
*/
export const fetchWithTimeout = (input: RequestInfo, init?: RequestInit & { timeout?: number }) => {
if (typeof init?.timeout === 'number') {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(new TimeoutError()), init.timeout);
@jonasgeiler
jonasgeiler / INFO.md
Last active May 5, 2022 13:47
A good looking winston format for log messages

Preview

image

Usage

const logger = createLogger({
 	level:      'info',
 	transports: new transports.Console(),
 	format:     customFormat(),
});
@jonasgeiler
jonasgeiler / copy-fontsource-fonts.js
Created December 11, 2021 23:17
A little script which copies fonts installed with fontsource from the installation path to a local folder
const path = require('path');
const fs = require('fs/promises');
// ---------------------------------------- CONFIG ---------------------------------------- //
/**
* Add the fonts to copy here.
* Make sure you've installed the Fontsource package for the font!
* Uses the same font family format as the Google Font API.
@jonasgeiler
jonasgeiler / subscribe-all.ts
Last active December 17, 2023 16:40
A simple "subscribeAll" function for Svelte Stores
import type { Readable, Unsubscriber } from 'svelte/store';
/** List of Readable stores */
type Stores = [ Readable<any>, ...Array<Readable<any>> ] | Array<Readable<any>>;
/** Values from a list of Readable stores */
type StoresValues<T> = {
[K in keyof T]: T[K] extends Readable<infer U> ? U : never;
};
@jonasgeiler
jonasgeiler / job.ts
Created July 18, 2021 21:10
Super simple job scheduler for TypeScript/JavaScript
export function job(time: number | string, job: Function): number {
let millis: number;
if (typeof time === 'number') {
millis = time;
} else {
if (!isNaN(time)) {
millis = parseInt(time);
} else {
const num = parseInt(time.slice(0, -1));
@jonasgeiler
jonasgeiler / dungeon-solver.js
Last active March 21, 2021 04:30
Solves the "Magic Tower" levels in the game "Fancade"
const fs = require('fs');
const LEVELS = JSON.parse(fs.readFileSync('./levels.json'));
let argv = process.argv.splice(2);
const FORCE_RECALC = argv.includes('-f') || argv.includes('--force');
if (FORCE_RECALC) {
argv = argv.filter(v => v !== '-f' && v !== '--force');
@jonasgeiler
jonasgeiler / USAGE.md
Last active January 27, 2021 06:34
A tiny little logger written in JS

USAGE

// Get log level (f.e. "info")
require('dotenv').config();
const { LOG_LEVEL } = process.env;


// Register logger
require('./logger').register(LOG_LEVEL);