Skip to content

Instantly share code, notes, and snippets.

View lichrot's full-sized avatar
🤡
Lagging

Lich lichrot

🤡
Lagging
View GitHub Profile
@lichrot
lichrot / GeneratorFunction.ts
Created May 2, 2023 22:02
Concurrency for Generator Functions, Promise style
interface GeneratorFunction extends Function {
all: typeof all,
allSettled: typeof allSettled,
any: typeof any,
race: typeof race,
resolve: typeof resolve,
reject: typeof reject,
}
type GeneratorYieldType<TGenerator extends Generator> = TGenerator extends Generator<infer TYield, any, any> ? TYield : unknown;
type GeneratorReturnType<TGenerator extends Generator> = TGenerator extends Generator<any, infer TReturn, any> ? TReturn : unknown;
@lichrot
lichrot / IDBFactory.databases.js
Last active February 26, 2024 14:20
Quick-n-dirty IDBFactory.prototype.databases (a.k.a. indexedDB.databases) polyfill for Firefox to get all the IndexedDB database names
if (!globalThis.IDBFactory.prototype.databases) {
const KEY = 'IDB_DATABASES_POLYFILL';
globalThis.IDBFactory.prototype.databases = function databases() {
const serialized = globalThis.localStorage.getItem(KEY);
return Promise.resolve(serialized ? JSON.parse(serialized) : []);
};
const listener = async (event) => {
const { name, version } = event.target.result;
@lichrot
lichrot / get_distance_benchmark.c
Created September 17, 2022 23:20
Get edit distance between two strings, with or without allocation
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
char* get_substring(char* string, int start_idx, int end_idx) {
int total_len = end_idx - start_idx;
char* result = malloc(sizeof(char) * total_len);
@lichrot
lichrot / alloc_concat_strings.c
Last active August 10, 2022 14:33
Variadic string concatenation in C with allocation (you most likely don't wanna use it)
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
int get_string_length(char* string) {
int size = 0;
while (*string != '\0') {
size += 1;
string += 1;
@lichrot
lichrot / default_db_init
Last active January 21, 2021 19:28
Small PostgreSQL script to quickly initialize an isolated (i.e. with private schema) database for a small project with a minimal administration layout (to prevent abusing default superuser)
-- Courtesy of Erwin Brandstetter
-- https://dba.stackexchange.com/a/117661
--
-- new_db = new solitary database
-- new_sch = private schema for new database
-- db_admin = db_mng privileges + create/drop tables privileges
-- db_mng = db_user privileges + insert/update/delete privileges
-- db_user = select privilege only
--
-- psql -U postgres