Skip to content

Instantly share code, notes, and snippets.

// Based on https://blog.logrocket.com/practical-guide-typescript-decorators/
/* eslint-disable max-classes-per-file */
import { performance } from 'perf_hooks';
type Context = { kind: 'class' | 'method' | 'field'; name: string | symbol };
const mapping: Record<Context['kind'], Context['kind']> = {
class: 'class',
method: 'method',
import { beforeEach, describe, expect, jest, test } from '@jest/globals';
import { getOneTimePromise } from './getOneTimePromise';
const mockGetAnswer = jest.fn<() => Promise<number>>().mockImplementation(async () => 42);
describe('getOneTimePromise', () => {
beforeEach(() => {
mockGetAnswer.mockClear();
});
// Based on https://dev.to/peaonunes/loading-a-directory-as-a-tree-structure-in-node-52bg
import fs from 'node:fs';
type TreeNode = { path: string; children: TreeNode[] };
const createTreeNode = (path: string): TreeNode => ({ path, children: [] });
export const buildTree = (rootPath: string) => {
const root = createTreeNode(rootPath);
@McMerph
McMerph / removeConsequentSlashes.ts
Created October 26, 2023 23:12
removeConsequentSlashes
export const removeConsequentSlashes = (str: string) =>
str
.split('')
.reduce<string>((acc, char) => (acc.at(-1) === '/' && char === '/' ? acc : acc + char), '');
// https://stackoverflow.com/a/63208885
export const getNotConcurrentFetch = (fetch: typeof global.fetch): typeof global.fetch => {
const queue: Array<{
input: RequestInfo | URL;
init: RequestInit | undefined;
resolve: (value: Response | PromiseLike<Response>) => void;
reject: (reason?: unknown) => void;
}> = [];
let isPending = false;
@McMerph
McMerph / readme.md
Created August 6, 2023 13:38 — forked from Machy8/readme.md
Start Kubernetes proxy server automatically
  1. Create an empty file and paste the content bellow into it /lib/systemd/system/kubectlproxy.service
[Unit]
Description=kubectl proxy 8080
After=network.target

[Service]
User=root
ExecStart=/bin/bash -c "/usr/bin/kubectl proxy --address=127.0.0.1 --port=8080"
// https://stackoverflow.com/a/72676417
type Json =
| string
| number
| boolean
| null
| { [property: string]: Json }
| Json[];
const anonymizeJson = (input: Json): Json => {
export const invokeAsyncFnConditionally = async <T, U extends unknown[]>(
asyncFn: (...args: U) => Promise<T>,
isShouldInvoke: boolean,
...args: U
): Promise<{ wasInvoked: false } | { wasInvoked: true; result: T }> => {
if (!isShouldInvoke) return { wasInvoked: false };
return { wasInvoked: true, result: await asyncFn(...args) };
};
import { useRef } from 'react';
export const useOnlyOnceAtTheTime = (mode: 'ignore' | 'throw error') => {
const isRunning = useRef(false);
return async <T>(asyncFn: () => Promise<T>) => {
if (isRunning.current) {
if (mode === 'ignore') return undefined;
throw new Error('Already running!');
}
@McMerph
McMerph / useTimeout.ts
Created June 1, 2023 09:08
React withDelay HOC
// Based on https://overreacted.io/making-setinterval-declarative-with-react-hooks/
import { useEffect, useRef } from 'react';
const noop = () => {};
export const useTimeout = (delayInMs: number | undefined, cbFn: () => void) => {
const savedCallback = useRef(cbFn);
useEffect(() => {