Skip to content

Instantly share code, notes, and snippets.

View aparx's full-sized avatar
🎯
Focusing

Vinzent Alexander aparx

🎯
Focusing
View GitHub Profile
@aparx
aparx / memoize.ts
Last active February 23, 2024 01:35
Memoization of a function call, depending on the arguments passed
export function memoize<TMemoizedFn extends (...args: any[]) => any>(
callback: TMemoizedFn,
checkArgs: boolean = true
): TMemoizedFn {
let memoized: true;
let memory: ReturnType<TMemoizedFn>;
let previousArgs: unknown[];
return function __memoized(...args: unknown[]): ReturnType<TMemoizedFn> {
if (!memoized || (checkArgs && !isSameArgs(previousArgs, args))) {
memory = callback(...args);
@aparx
aparx / stringConcat.ts
Last active February 17, 2024 21:16
Typesafe string concat
export type StringConcat<TArgs extends string[]> = _Flatten<TArgs>;
type _Flatten<TArgs extends string[], _TArg extends string = ""> =
TArgs extends [infer TCurrent extends string, ...infer TNext extends string[]]
? _Flatten<TNext, `${_TArg}${TCurrent}`>
: _TArg;
export function stringConcat<const TArgs extends string[]>(...args: TArgs): StringConcat<TArgs> {
if (args.length === 0) return "" as StringConcat<TArgs>;
return "".concat(...args) as StringConcat<TArgs>;
@aparx
aparx / joinArray.ts
Last active January 29, 2024 01:54
Typesafe string joining
type _JoinableElement = string | number | null | undefined | boolean;
type _JoinableArray = _JoinableElement[];
export type JoinArray<
TArray extends _JoinableArray,
TDelimiter extends string = " "
> = _JoinArray<TArray, TDelimiter>;
type _JoinArray<
TArray extends _JoinableArray,
@aparx
aparx / IndexMap.java
Last active December 18, 2023 10:14
Map-like implementation that associates objects to indices.
import com.google.common.base.Preconditions;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.CheckReturnValue;
import lombok.Getter;
import org.checkerframework.checker.index.qual.NonNegative;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.dataflow.qual.Pure;
import java.util.*;
@aparx
aparx / ColoredMaterial.java
Last active December 5, 2023 12:08
(Bukkit) A little utility that allows to get a colored material based off of a DyeColor input
package io.github.aparx.skywarz.utils.material;
import com.google.common.base.Preconditions;
import org.apache.commons.lang3.Validate;
import org.bukkit.DyeColor;
import org.bukkit.Material;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.EnumMap;
import java.util.function.Function;
@aparx
aparx / objectPaths.ts
Last active May 15, 2023 22:49
Fully typesafe and secure way to access properties in an object using string paths
/** @author Vinzent Zeband - you will need (./types imports)
* https://gist.github.com/zvint/a7fd6cdcb9054dd9b9d53164d0088e33 */
import type { RecursiveRecord, SplitToTuple, SplitToUnion } from './types';
// <==================================>
// MAIN OBJECT-PATH TYPES
// <==================================>
/** Non-specific property key that can be used to represent a path segment. */
export type GenericPathSegment = string | number;
@aparx
aparx / types.ts
Last active March 18, 2024 01:03
(for internal usage) a couple of typesafe typescript utilities
// prettier-config-ignore
/** Typesafe alternative to Typescript's "Extract" type-utility */
export type UnionExtract<TUnion, TKeys extends TUnion> = TUnion extends TKeys
? TUnion
: never;
// prettier-config-ignore
/** Typesafe alternative to Typescript's "Exclude" type-utility */
export type UnionExclude<TUnion, TKeys extends TUnion> = TUnion extends TKeys
? never
@aparx
aparx / unitMapping.ts
Last active March 6, 2023 15:05
Little type & runtime utility useful for styling, providing utilities to map any kind of units (such as XXL, XL, numbers and more) into a specfic unit string with custom piping to mutate given values.
/*
* Example:
*
*
*/
const ExampleUnitMap = { xl: 3.0, md: 1.0, sm: 0.5, };
type ExampleUnit = keyof typeof ExampleUnitMap;
@aparx
aparx / useDetectEventOutside.ts
Created March 3, 2023 15:31
useEffect hook that registers document events outside a specific component or its children
import { RefObject, useEffect } from 'react';
export default function useDetectEventOutside<
TNode extends Node,
TEventType extends keyof DocumentEventMap
>(
reference: RefObject<TNode>,
eventType: TEventType,
eventListener: (this: Document, ev: DocumentEventMap[TEventType]) => any
) {
@aparx
aparx / deepPartialReadonly.ts
Last active March 3, 2023 07:25
DeepPartial & DeepReadonly utility types
export type Primitives =
| number
| string
| boolean
| bigint
| symbol
| undefined
| null;
export type DeepReadonly<TInput> = TInput extends Primitives | Function