Skip to content

Instantly share code, notes, and snippets.

View aparx's full-sized avatar

Vinzent Alexander aparx

View GitHub Profile
@aparx
aparx / RegexEscaping.java
Last active February 13, 2022 05:17
Escapes regex characters in a string relatively fast.
package io.github.sauranbone.plang;
import org.apache.commons.lang3.StringUtils;
import java.util.regex.Pattern;
/**
* @author Vinzent Zeband
* @version 06:07 CET, 13.02.2022
* @since 1.0
@aparx
aparx / ChildMap.java
Created June 10, 2022 15:42
Interface representing key-value mappings, containing a possible other map that may be respected in lookup operations.
package io.github.sauranbone.plang.core.abstraction;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
/**
@aparx
aparx / reactQueryInfiniteMapper.ts
Last active January 25, 2023 01:26
Enables individual page data remapping and removal for a set of queries using react-query (only through useInfiniteQuery)
import {Query, QueryCache, QueryClient, QueryFilters} from "@tanstack/react-query";
import {InfiniteData} from "@tanstack/query-core/build/lib/types";
export type InfiniteQueryData<U, V> = InfiniteData<U | V | undefined>;
export type InfinitePageMapperParam<OldPageT, NewPageT> = {
page: OldPageT | NewPageT,
index: number,
param?: unknown,
query?: Query<unknown, unknown, InfiniteQueryData<OldPageT, NewPageT>>,
@aparx
aparx / identity_enum.ts
Last active February 14, 2023 20:10
typescript enums without using enum
type EnumKey<EnumType> = keyof EnumType;
type IdentityEnum<T> = T extends Array<infer E extends string> ? {
readonly [P in T[keyof T] as E]: E
} : never;
type IdentityEnumConstructor = {
<T extends string[]>(...values: T): IdentityEnum<T>;
new <T extends string[]>(...values: T): IdentityEnum<T>;
};
@aparx
aparx / formidableZodParse.ts
Last active February 21, 2023 08:02
Little gist providing two functions to parse a form from an incoming message and also validate it immediately using a given Zod schema.
import formidable from 'formidable';
import { IncomingMessage } from 'http';
import { z, ZodObject, ZodType } from 'zod';
export type KnownFormFields = Record<string, ZodType>;
export type FormParseResult<TFields, TFiles> = {
error?: Error;
fields?: TFields;
files?: TFiles;
@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
@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 / 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 / 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 / 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;